public void OptionFoldBackWhenSome() { int initial = 25; int state = 10; int expected = state + initial; Option <int> optionValue = initial; Option <int> optionResult = OptionModule.FoldBack((value, _state) => _state + value, optionValue, state); 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 <see cref="Option{T}.Some(T)"/> option value and <paramref name="state"/>. /// 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 <see cref="Option{T}.Some(T)"/> option value and <paramref name="state"/>. /// Otherwise returns the <paramref name="state"/> itself. /// </returns> public static TState FoldBack <T, TState>(this Option <T> option, Func <T, TState, TState> folder, TState state) => OptionModule.FoldBack(folder, option, state);