Share via


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

Wertet die Entsprechung von List.forall für einen Optionstyp aus.

Namespace/Modulpfad: Microsoft.FSharp.Core.Option

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

// Signature:
forall : ('T -> bool) -> 'T option -> bool

// Usage:
forall predicate option

Parameter

  • predicate
    Typ: 'T -> bool

    Eine Funktion, die einen booleschen Wert ergibt, wenn ein Wert vom Optionstyp angegeben ist.

  • option
    Typ: 'T option

    Die Eingabeoption.

Rückgabewert

true, wenn die Option None ist, andernfalls wird das Ergebnis der Anwendung des Prädikats auf die Wertoption zurückgegeben.

Hinweise

Der Ausdruck forall p inp wird zu match inp with None -> true | Some x -> p x ausgewertet.

Der Name dieser Funktion in kompilierten Assemblys lautet ForAll. 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.forall.

let isEven opt =
    Option.forall (fun elem -> elem % 2 = 0) opt
printfn "%b" <| isEven (Some(2))
printfn "%b" <| isEven None
printfn "%b" <| isEven (Some(1))

// Use this function with an array of int options.
let forAllOptions function1 = List.forall (fun opt -> Option.forall function1 opt)
let list1 = [ for i in 1 .. 10 do yield Some(i) ]
let list2 = [ for i in 1 .. 10 do yield if (i % 2) = 0 then Some(i) else None ]
let list3 = [ for i in 1 .. 10 do yield if (i % 2) = 1 then Some(i) else None ]
let evalList list = printfn "%b" <| forAllOptions (fun value -> value % 2 = 0) list
let lists = [ list1; list2; list3 ]
List.iter evalList lists

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.