Option.forall<'T> Function (F#)

Evaluates the equivalent of List.forall for an option type.

Namespace/Module Path: Microsoft.FSharp.Core.Option

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

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

// Usage:
forall predicate option

predicate

Type: 'T -> bool

A function that evaluates to a Boolean when given a value from the option type.

option

Type: 'T option

The input option.

true if the option is None, otherwise it returns the result of applying the predicate to the option value.

The expression forall p inp evaluates to match inp with None -> true | Some x -> p x.

This function is named ForAll in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

The following code illustrates the use of 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

true
true
false
false
true
false

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

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft