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); }
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); }
/// <summary> /// Creates a new <see cref="Either{TLeft, TRight}"/> whose value is the result of applying the given mapping functions. /// </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> /// <typeparam name="TRightResult">The type of the right value returned by mapping functions</typeparam> /// <param name="mappingWhenLeft">The function to transform either value when <see cref="Either{TLeft, TRight}.IsLeft"/>.</param> /// <param name="mappingWhenRight">The function to transform either value when <see cref="Either{TLeft, TRight}.IsRight"/>.</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, TRightResult> Map <TLeft, TRight, TLeftResult, TRightResult>( this Either <TLeft, TRight> either, Func <TRight, TRightResult> mappingWhenRight, Func <TLeft, TLeftResult> mappingWhenLeft) => EitherModule.Map(mappingWhenRight, mappingWhenLeft, either);