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