Freigeben über


Option.count<'T>-Funktion (F#)

Wertet die Entsprechung von Set.count für eine Option aus.

Namespace/Modulpfad: Microsoft.FSharp.Core.Option

Assembly: FSharp.Core (in FSharp.Core.dll)

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

// Usage:
count option

Parameter

  • option
    Typ: 'T option

    Die Eingabeoption.

Rückgabewert

Eine Null, wenn die Option None ist, andernfalls 1.

Hinweise

Der Ausdruck count inp wird zu match inp with None -> 0 | Some _ -> 1 ausgewertet.

Der Name dieser Funktion in kompilierten Assemblys lautet Count. Verwenden Sie diesen Namen, wenn Sie in einer anderen .NET-Sprache als F# oder durch Reflektion auf die Funktion zugreifen.

Beispiel

Das folgende Codebeispiel veranschaulicht die Verwendung von 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

  

Plattformen

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Versionsinformationen

F#-Runtime

Unterstützt in: 2.0, 4.0

Silverlight

Unterstützt in: 3

Siehe auch

Weitere Ressourcen

Core.Option-Modul (F#)

Microsoft.FSharp.Core-Namespace (F#)

Änderungsprotokoll

Datum

Versionsgeschichte

Grund

Mai 2010

Codebeispiel hinzugefügt.

Informationsergänzung.