public void OptionFoldWhenSome() { int initial = 25; int state = 10; int expected = state + initial; Option <int> optionValue = initial; Option <int> optionResult = OptionModule.Fold((_state, value) => _state + value, state, optionValue); int result = optionResult.Match(value => value, () => 0); Assert.AreEqual(expected, result); }
/// <summary> /// Creates a new <typeparamref name="TState"/> value by applying the given <paramref name="folder"/> function /// to <paramref name="state"/> and <see cref="Option{T}.Some(T)"/> option value. /// Otherwise returns the <paramref name="state"/> itself. /// </summary> /// <typeparam name="T">The type of the option value.</typeparam> /// <typeparam name="TState">The type of initial and final states</typeparam> /// <param name="folder">The function to transform option value from the input option to a new state value.</param> /// <param name="state">The initial state.</param> /// <param name="option">The input option.</param> /// <returns> /// Returns a new <typeparamref name="TState"/> value by applying the given <paramref name="folder"/> function /// to <paramref name="state"/> and <see cref="Option{T}.Some(T)"/> option value. /// Otherwise returns the <paramref name="state"/> itself. /// </returns> public static TState Fold <T, TState>(this Option <T> option, TState state, Func <TState, T, TState> folder) => OptionModule.Fold(folder, state, option);