Contains operations for working with options.
Function or value | Description |
|
Example
let tryParse (input: string) = match System.Int32.TryParse input with | true, v -> Some v | false, _ -> None None |> Option.bind tryParse // evaluates to None Some "42" |> Option.bind tryParse // evaluates to Some 42 Some "Forty-two" |> Option.bind tryParse // evaluates to None |
|
Example
(99, None) ||> Option.contains // evaluates to false (99, Some 99) ||> Option.contains // evaluates to true (99, Some 100) ||> Option.contains // evaluates to false |
|
Example
None |> Option.count // evaluates to 0 Some 99 |> Option.count // evaluates to 1 |
Full Usage:
Option.defaultValue value option
Parameters:
'T
-
The specified default value.
option : 'T option
-
The input option.
Returns: 'T
The option if the option is Some, else the default value.
|
Identical to the built-in defaultArg operator, except with the arguments swapped.
Example
(99, None) ||> Option.defaultValue // evaluates to 99 (99, Some 42) ||> Option.defaultValue // evaluates to 42 |
|
![]() ![]() ![]() ![]() ![]() ![]()
Gets the value of the option if the option is
defThunk is not evaluated unless option is
Example
None |> Option.defaultWith (fun () -> 99) // evaluates to 99 Some 42 |> Option.defaultWith (fun () -> 99) // evaluates to 42 |
Full Usage:
Option.exists predicate option
Parameters:
'T -> bool
-
A function that evaluates to a boolean when given a value from the option type.
option : 'T option
-
The input option.
Returns: bool
False if the option is None, otherwise it returns the result of applying the predicate
to the option value.
|
Example
None |> Option.exists (fun x -> x >= 5) // evaluates to false Some 42 |> Option.exists (fun x -> x >= 5) // evaluates to true Some 4 |> Option.exists (fun x -> x >= 5) // evaluates to false |
Full Usage:
Option.filter predicate option
Parameters:
'T -> bool
-
A function that evaluates whether the value contained in the option should remain, or be filtered out.
option : 'T option
-
The input option.
Returns: 'T option
The input if the predicate evaluates to true; otherwise, None.
|
Example
None |> Option.filter (fun x -> x >= 5) // evaluates to None Some 42 |> Option.filter (fun x -> x >= 5) // evaluates to Some 42 Some 4 |> Option.filter (fun x -> x >= 5) // evaluates to None |
|
Example
(None: int option option) |> Option.flatten // evaluates to None (Some ((None: int option))) |> Option.flatten // evaluates to None (Some (Some 42)) |> Option.flatten // evaluates to Some 42 |
Full Usage:
Option.fold folder state option
Parameters:
'State -> 'T -> 'State
-
A function to update the state data when given a value from an option.
state : 'State
-
The initial state.
option : 'T option
-
The input option.
Returns: 'State
The original state if the option is None, otherwise it returns the updated state with the folder
and the option value.
|
Example
(0, None) ||> Option.fold (fun accum x -> accum + x * 2) // evaluates to 0 (0, Some 1) ||> Option.fold (fun accum x -> accum + x * 2) // evaluates to 2 (10, Some 1) ||> Option.fold (fun accum x -> accum + x * 2) // evaluates to 12 |
Full Usage:
Option.foldBack folder option state
Parameters:
'T -> 'State -> 'State
-
A function to update the state data when given a value from an option.
option : 'T option
-
The input option.
state : 'State
-
The initial state.
Returns: 'State
The original state if the option is None, otherwise it returns the updated state with the folder
and the option value.
|
Example
(None, 0) ||> Option.foldBack (fun x accum -> accum + x * 2) // evaluates to 0 (Some 1, 0) ||> Option.foldBack (fun x accum -> accum + x * 2) // evaluates to 2 (Some 1, 10) ||> Option.foldBack (fun x accum -> accum + x * 2) // evaluates to 12 |
Full Usage:
Option.forall predicate option
Parameters:
'T -> bool
-
A function that evaluates to a boolean when given a value from the option type.
option : 'T option
-
The input option.
Returns: bool
True if the option is None, otherwise it returns the result of applying the predicate
to the option value.
|
Example
None |> Option.forall (fun x -> x >= 5) // evaluates to true Some 42 |> Option.forall (fun x -> x >= 5) // evaluates to true Some 4 |> Option.forall (fun x -> x >= 5) // evaluates to false |
Full Usage:
Option.get option
Parameters:
'T option
-
The input option.
Returns: 'T
The value within the option.
|
Example
Some 42 |> Option.get // evaluates to 42 (None: int option) |> Option.get // throws exception! |
|
Example
None |> Option.isNone // evaluates to true Some 42 |> Option.isNone // evaluates to false |
|
Example
None |> Option.isSome // evaluates to false Some 42 |> Option.isSome // evaluates to true |
|
Example
None |> Option.iter (printfn "%s") // does nothing Some "Hello world" |> Option.iter (printfn "%s") // prints "Hello world" |
|
Example
None |> Option.map (fun x -> x * 2) // evaluates to None Some 42 |> Option.map (fun x -> x * 2) // evaluates to Some 84 |
Full Usage:
Option.map2 mapping option1 option2
Parameters:
'T1 -> 'T2 -> 'U
-
A function to apply to the option values.
option1 : 'T1 option
-
The first option.
option2 : 'T2 option
-
The second option.
Returns: 'U option
An option of the input values after applying the mapping function, or None if either input is None.
|
![]() ![]() ![]() ![]() ![]() ![]()
Example
(None, None) ||> Option.map2 (fun x y -> x + y) // evaluates to None (Some 5, None) ||> Option.map2 (fun x y -> x + y) // evaluates to None (None, Some 10) ||> Option.map2 (fun x y -> x + y) // evaluates to None (Some 5, Some 10) ||> Option.map2 (fun x y -> x + y) // evaluates to Some 15 |
Full Usage:
Option.map3 mapping option1 option2 option3
Parameters:
'T1 -> 'T2 -> 'T3 -> 'U
-
A function to apply to the option values.
option1 : 'T1 option
-
The first option.
option2 : 'T2 option
-
The second option.
option3 : 'T3 option
-
The third option.
Returns: 'U option
An option of the input values after applying the mapping function, or None if any input is None.
|
![]() ![]() ![]() ![]() ![]() ![]()
Example
(None, None, None) |||> Option.map3 (fun x y z -> x + y + z) // evaluates to None (Some 100, None, None) |||> Option.map3 (fun x y z -> x + y + z) // evaluates to None (None, Some 100, None) |||> Option.map3 (fun x y z -> x + y + z) // evaluates to None (None, None, Some 100) |||> Option.map3 (fun x y z -> x + y + z) // evaluates to None (Some 5, Some 100, Some 10) |||> Option.map3 (fun x y z -> x + y + z) // evaluates to Some 115 |
|
Example
System.Nullable<int>() |> Option.ofNullable // evaluates to None System.Nullable(42) |> Option.ofNullable // evaluates to Some 42 |
Full Usage:
Option.ofObj value
Parameters:
'T
-
The input value.
Returns: 'T option
The result option.
|
Example
(null: string) |> Option.ofObj // evaluates to None "not a null string" |> Option.ofObj // evaluates to (Some "not a null string") |
|
Example
((None: int Option), None) ||> Option.orElse // evaluates to None (Some 99, None) ||> Option.orElse // evaluates to Some 99 (None, Some 42) ||> Option.orElse // evaluates to Some 42 (Some 99, Some 42) ||> Option.orElse // evaluates to Some 42 |
|
ifNoneThunk is not evaluated unless option is
Example
(None: int Option) |> Option.orElseWith (fun () -> None) // evaluates to None None |> Option.orElseWith (fun () -> (Some 99)) // evaluates to Some 99 Some 42 |> Option.orElseWith (fun () -> None) // evaluates to Some 42 Some 42 |> Option.orElseWith (fun () -> (Some 99)) // evaluates to Some 42 |
Full Usage:
Option.toArray option
Parameters:
'T option
-
The input option.
Returns: 'T[]
The result array.
|
Example
(None: int option) |> Option.toArray // evaluates to [||] Some 42 |> Option.toArray // evaluates to [|42|] |
|
Example
(None: int option) |> Option.toList // evaluates to [] Some 42 |> Option.toList // evaluates to [42] |
|
Example
(None: int option) |> Option.toNullable // evaluates to new System.Nullable<int>() Some 42 |> Option.toNullable // evaluates to new System.Nullable(42) |
Full Usage:
Option.toObj value
Parameters:
'T option
-
The input value.
Returns: 'T
The result value, which is null if the input was None.
|
Example
(None: string option) |> Option.toObj // evaluates to null Some "not a null string" |> Option.toObj // evaluates to "not a null string" |