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); }
/// <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);