共用方式為


Option.count<'T> 函式 (F#)

評估與選項相等的 Set.count

**命名空間/模組路徑:**Microsoft.FSharp.Core.Option

組件:FSharp.Core (在 FSharp.Core.dll 中)

// Signature:
count : 'T option -> int

// Usage:
count option

參數

  • option
    型別:'T option

    輸入選項。

傳回值

如果選項為 None 則為零,否則為 1。

備註

運算式 count inp 會評估為 match inp with None -> 0 | Some _ -> 1。

這個函式在已編譯的組件中名為 Count。 如果您是透過 F# 以外的語言,或是透過反映來存取函式,請使用這個名稱。

範例

下列程式碼說明如何使用 Option.count

let opt1 = Some("test")
let opt2 = None
printfn "%A %A" (Option.count opt1) (Option.count opt2)

// Use Option.count to count the number of Some values in
// an array of options.
let getCount (array1 : int option array) =
    Array.fold (fun state elem -> state + Option.count elem) 0 array1
let testArray1 = [| Some(10); None; Some(1); None; None; Some(56) |]
let testArray2 = [| for i in 1 .. 10 do if i % 2 = 0 then yield Some(i) else yield None |]
printfn "%d" <| getCount testArray1
printfn "%d" <| getCount testArray2

Output

  

平台

Windows 8 中, Windows 7, Windows Server 2012 上, Windows Server 2008 R2

版本資訊

F# 核心程式庫版本

支援版本:2.0, 4.0,可攜式執行檔 (PE)。

請參閱

參考

Core.Option 模組 (F#)

Microsoft.FSharp.Core 命名空間 (F#)