public void EitherToOptionLeftWhenLeft() { string expected = "Hello World"; Either <string, int> either = expected; Option <string> optionResult = EitherModule.ToOptionLeft(either); string result = optionResult.Match(value => value, () => string.Empty); Assert.AreEqual(expected, result); }
public void EitherExistsRightFalseWhenLeft() { bool expected = false; Either <string, int> either = "Hello"; bool result = EitherModule.ExistsRight( left => left == 10 , either); Assert.AreEqual(expected, result); }
public void EitherSwapWhenRight2() { string expected = string.Empty; Either <string, int> either = 10; Either <int, string> swappedEither = EitherModule.Swap(either); string result = swappedEither.Match(right => right, left => string.Empty); Assert.AreEqual(expected, result); }
public void EitherToOptionRightWhenLeft() { int expected = 0; Either <string, int> either = "Hello World"; Option <int> optionResult = EitherModule.ToOptionRight(either); int result = optionResult.Match(value => value, () => 0); Assert.AreEqual(expected, result); }
public void EitherToTupleWhenNoneLeft() { string expected = string.Empty; Either <string, int> either = 10; (Option <string> Left, Option <int> Right)tuple = EitherModule.ToTuple(either); string result = tuple.Left.Match(value => value, () => string.Empty); Assert.AreEqual(expected, result); }
public void EitherSwapWhenLeft() { int expected = 0; Either <string, int> either = "Hello"; Either <int, string> swappedEither = EitherModule.Swap(either); int result = swappedEither.Match(right => 0, left => left); Assert.AreEqual(expected, result); }
public void EitherToTupleWhenNoneRight() { int expected = 0; Either <string, int> either = "Test"; (Option <string> Left, Option <int> Right)tuple = EitherModule.ToTuple(either); int result = tuple.Right.Match(value => value, () => 0); Assert.AreEqual(expected, result); }
public void EitherExistsLeftFalseWhenRight() { bool expected = false; Either <string, int> either = 10; bool result = EitherModule.ExistsLeft( right => right == "Hello" , either); Assert.AreEqual(expected, result); }
public void EitherExistsLeftTrueWhenLeft() { bool expected = true; Either <string, int> either = "Hello"; bool result = EitherModule.ExistsLeft( right => right == "Hello" , either); Assert.AreEqual(expected, result); }
public void EitherExistsRightTrueWhenRight() { bool expected = true; Either <string, int> either = 10; bool result = EitherModule.ExistsRight( left => left == 10 , either); Assert.AreEqual(expected, result); }
public void EitherIterateLeftWhenLeft() { string expected = "Hello World"; string result = "World"; Either <string, int> either = "Hello "; EitherModule.IterateLeft( left => result = string.Concat(left, result) , either); Assert.AreEqual(expected, result); }
public void EitherIterateRightWhenRight() { int expected = 20; int result = 10; Either <string, int> either = 10; EitherModule.IterateRight( right => result += right , either); Assert.AreEqual(expected, result); }
public void EitherExistsFalseWhenLeft() { bool expected = false; Either <string, int> either = "Hello World"; bool result = EitherModule.Exists( right => right == 10, left => left == "Test" , either); Assert.AreEqual(expected, result); }
public void EitherFoldBackRightWhenLeft() { int state = 10; int expected = 10; Either <string, int> either = "10"; int result = EitherModule.FoldBackRight( (right, _state) => right + _state, either, state); Assert.AreEqual(expected, result); }
public void EitherFoldBackLeftWhenLeft() { string state = "Hello"; string expected = "Hello World"; Either <string, int> either = " World"; string result = EitherModule.FoldBackLeft( (left, _state) => string.Concat(_state, left), either, state); Assert.AreEqual(expected, result); }
public void EitherFoldLeftWhenRight() { string state = "Hello"; string expected = "Hello"; Either <string, int> either = 10; string result = EitherModule.FoldLeft( (_state, left) => string.Concat(_state, left), state, either); Assert.AreEqual(expected, result); }
public void EitherFoldRightWhenRight() { int state = 10; int expected = 20; Either <string, int> either = 10; int result = EitherModule.FoldRight( (_state, right) => right + _state, state, either); Assert.AreEqual(expected, result); }
public void EitherIterateWhenRight() { int expected = 20; int result = 10; Either <string, int> either = 10; EitherModule.Iterate( right => result += right, left => string.Concat(left, left) , either); Assert.AreEqual(expected, result); }
public void EitherMapLeftWhenLeft() { double expected = 10; Either <string, int> either = "10"; Either <double, int> eitherResult = EitherModule.MapLeft( left => Convert.ToDouble(left), either); double result = eitherResult.Match(right => 0, left => left); Assert.AreEqual(expected, result); }
public void EitherMapRightWhenLeft() { bool expected = false; Either <string, int> either = "10"; Either <string, bool> eitherResult = EitherModule.MapRight( _isEven, either); bool result = eitherResult.Match(right => right, left => false); Assert.AreEqual(expected, result); }
public void EitherFoldWhenLeft() { int state = 10; int expected = 10; Either <string, int> either = "10"; int result = EitherModule.Fold( (_state, right) => right + _state, (_state, left) => _state, state, either); Assert.AreEqual(expected, result); }
public void EitherMapWhenRight() { bool expected = true; Either <string, int> either = 10; Either <int, bool> eitherResult = EitherModule.Map( _isEven, left => Convert.ToInt32(left), either); bool result = eitherResult.Match(right => right, left => false); Assert.AreEqual(expected, result); }
public void EitherMapWhenLeft() { int expected = 10; Either <string, int> either = "10"; Either <int, bool> eitherResult = EitherModule.Map( _isEven, left => Convert.ToInt32(left), either); int result = eitherResult.Match(right => 0, left => left); Assert.AreEqual(expected, result); }
/// <summary> /// Returns true if the given predicate functions return true when applied to either value. /// Otherwise, returns false. /// </summary> /// <typeparam name="TLeft">The type of the left value.</typeparam> /// <typeparam name="TRight">The type of the right value.</typeparam> /// <param name="predicateWhenLeft">A function that evaluates whether the left value contained in the option is valid or not.</param> /// <param name="predicateWhenRight">A function that evaluates whether the right value contained in the option is valid or not.</param> /// <param name="either">the input either.</param> /// <returns> /// Returns true if the given predicate functions return true when applied to either value. /// </returns> public static bool Exists <TLeft, TRight>(this Either <TLeft, TRight> either, Func <TRight, bool> predicateWhenRight, Func <TLeft, bool> predicateWhenLeft) => EitherModule.Exists(predicateWhenRight, predicateWhenLeft, either);
/// <summary> /// Creates a new <see cref="Either{TLeft, TRight}"/> whose value is the result of applying the given <paramref name="mapping"/> function when <see cref="Either{TLeft, TRight}.IsLeft"/>. /// Otherwise, returns itself. /// </summary> /// <typeparam name="TLeft">The type of the left value.</typeparam> /// <typeparam name="TRight">The type of the right value.</typeparam> /// <typeparam name="TLeftResult">The type of the left value returned by mapping functions.</typeparam> /// <param name="mapping">The function to transform either value when <see cref="Either{TLeft, TRight}.IsLeft"/>.</param> /// <param name="either">the input either.</param> /// <returns> /// Returns a new <see cref="Either{TLeft, TRight}"/> whose value is the result of applying the given mapping functions. /// </returns> public static Either <TLeftResult, TRight> MapLeft <TLeft, TRight, TLeftResult>( this Either <TLeft, TRight> either, Func <TLeft, TLeftResult> mapping) => EitherModule.MapLeft(mapping, either);
/// <summary> /// Returns true when <paramref name="either"/> <see cref="Either{TLeft, TRight}.IsRight"/> and given <paramref name="predicate"/> function applied to <paramref name="either"/> return true. /// Otherwise, returns false. /// </summary> /// <typeparam name="TLeft">The type of the left value.</typeparam> /// <typeparam name="TRight">The type of the right value.</typeparam> /// <param name="predicate">A function that evaluates whether the right value contained in the option is valid or not.</param> /// <param name="either">the input either.</param> /// <returns> /// Returns true if the given predicate functions return true when applied to either value. /// </returns> public static bool ExistsRight <TLeft, TRight>(this Either <TLeft, TRight> either, Func <TRight, bool> predicate) => EitherModule.ExistsRight(predicate, either);
/// <summary>Applies the given functions to <see cref="Either{TLeft, TRight}"/> value depends on its state.</summary> /// <typeparam name="TLeft">The type of the left value.</typeparam> /// <typeparam name="TRight">The type of the right value.</typeparam> /// <param name="actionWhenLeft">The action to apply to left value of input <paramref name="either"/>.</param> /// <param name="actionWhenRight">The action to apply to right value of input <paramref name="either"/>.</param> /// <param name="either">the input either.</param> public static void Iterate <TLeft, TRight>(this Either <TLeft, TRight> either, Action <TRight> actionWhenRight, Action <TLeft> actionWhenLeft) => EitherModule.Iterate(actionWhenRight, actionWhenLeft, either);
/// <summary> /// Convert the <see cref="Either{TLeft, TRight}"/> to a <see cref="Option{T}"/> where T is <typeparamref name="TRight"/>. /// </summary> /// <typeparam name="TLeft">The type of the left value.</typeparam> /// <typeparam name="TRight">The type of the right value.</typeparam> /// <param name="either">The input either.</param> /// <returns> /// Returns an <see cref="Option{T}"/> where T is <typeparamref name="TRight"/>. /// The option value state is <see cref="Option{T}.Some(T)"/> when <paramref name="either"/> <see cref="Either{TLeft, TRight}.IsRight"/>, otherwise is <see cref="Option{T}.None"/> /// </returns> public static Option <TRight> ToOptionRight <TLeft, TRight>(this Either <TLeft, TRight> either) => EitherModule.ToOptionRight(either);
/// <summary> /// Convert the <see cref="Either{TLeft, TRight}"/> to a <see cref="Tuple{T1,T2}"/> where T1 and T2 are <see cref="Option{T}"/> value of <typeparamref name="TLeft"/> and <typeparamref name="TRight"/> respectively. /// </summary> /// <typeparam name="TLeft">The type of the left value.</typeparam> /// <typeparam name="TRight">The type of the right value.</typeparam> /// <param name="either">The input either.</param> /// <returns> /// Returns a <see cref="Tuple{T1,T2}"/> where T1 and T2 are <see cref="Option{T}"/> value of <typeparamref name="TLeft"/> and <typeparamref name="TRight"/> respectively. /// The Tuple left value (Item1) is <see cref="Option{T}.Some(T)"/> when <paramref name="either"/> <see cref="Either{TLeft, TRight}.IsLeft"/>, otherwise is <see cref="Option{T}.None"/> /// The Tuple right value (Item2) is <see cref="Option{T}.Some(T)"/> when <paramref name="either"/> <see cref="Either{TLeft, TRight}.IsRight"/>, otherwise is <see cref="Option{T}.None"/> /// </returns> public static (Option <TLeft>, Option <TRight>) ToTuple <TLeft, TRight>(this Either <TLeft, TRight> either) => EitherModule.ToTuple(either);
/// <summary> /// Creates a new <see cref="Either{TLeft, TRight}"/> value by swapping <see cref="Either{TLeft, TRight}.Left"/> and <see cref="Either{TLeft, TRight}.Right"/> values. /// </summary> /// <typeparam name="TLeft">The type of the left value.</typeparam> /// <typeparam name="TRight">The type of the right value.</typeparam> /// <param name="either">the input either.</param> /// <returns> /// Returns a new <see cref="Either{TLeft, TRight}"/> value by swapping <see cref="Either{TLeft, TRight}.Left"/> and <see cref="Either{TLeft, TRight}.Right"/> values. /// </returns> public static Either <TRight, TLeft> Swap <TLeft, TRight>(this Either <TLeft, TRight> either) => EitherModule.Swap(either);