Apply
Creates a new Option<T>
whose value is the result of applying the given applying
function to Option<T>.Some
value when both applying
and option
are IsSome
.
Otherwise returns an Option<T>.None()
.
Parameters
Returns
Option<Func<T, TResult>> applying
Option<T> option
Option<TResult>
Usage
This function is usually used to modify an option value by using an option function.
It works like a Map
function, but in this case both value and the function are option values.
It's possible to use Apply
function with two different syntaxes.
Apply<T, TResult>(function)
: in this case thefunction
can be a regular function;Apply(optionFunction)
: in this case thefunction
needs to be a option function.
Expliciting an optional function
Func<int, int> multiply2 = value => value * 2;
Option<Func<int, int>> optionFunction = multiply2;
Option<int> optionValue = 4;
Option<int> result = optionValue.Apply(optionFunction);
//result.IsSome = true
//result.Some = 8
Using a regular function
Func<int, int> multiply2 = value => value * 2;
Option<int> optionValue = 4;
Option<int> result = optionValue.Apply<int,int>(multiply2);
//result.IsSome = true
//result.Some = 8
When the option function IsNone
Option<Func<int, int>> optionFunction =
Option<Func<int, int>>.None();
Option<int> optionValue = 4;
Option<int> result = optionValue.Apply(optionFunction);
//result.IsSome = false
//result.IsNone = true
When the option value IsNone
Func<int, int> multiply2 = value => value * 2;
Option<int> optionValue = Option<int>.None();
Option<int> result = optionValue.Apply<int,int>(multiply2);
//result.IsSome = false
//result.IsNone = true
Last updated
Was this helpful?