Fold

Creates a new TState value by applying the given folder function to state and Option<T>.Some option value. Otherwise returns the state itself.

Parameters

Returns

Func<TState, T, TState> folder

TState state

Option<T> option

TState

Usage

This function applies the folder function to the Option<T> value and to the state.

When the optional value IsNone the folder function won't be executed and the state is returned as a result.

When the option value IsSome

string state = "The number is: "
Option<int> optionValue = 10;
string result = optionValue.Fold(
                    state,
                    (_state, value) => string.Concat(_state, value) );

//result = "The number is: 10"

When the option value IsNone

string state = "The number is: "
Option<int> optionValue = Option<int>.None();
string result = optionValue.Fold(
                    state,
                    (_state, value) => string.Concat(_state, value) );

//result = "The number is: "

When the option value IsSome

int state = 30
Option<int> optionValue = 10;
int result = optionValue.Fold(
                 state,
                 (_state, value) => _state + value );

//result = 40

When the option value IsNone

int state = 30
Option<int> optionValue = Option<int>.None();
int result = optionValue.Fold(
                 state,
                 (_state, value) => _state + value );

//result = 30

Last updated