/// <summary> /// Map to a new type. /// </summary> /// <typeparam name="TLeft">The type of the left.</typeparam> /// <typeparam name="TLeftResult">The type of the left result.</typeparam> /// <typeparam name="TRight">The type of the right.</typeparam> /// <typeparam name="TRightResult">The type of the right result.</typeparam> /// <param name="source">The source.</param> /// <param name="selectLeft">The select left.</param> /// <param name="selectRight">The select right.</param> /// <returns></returns> public static Either <TLeftResult, TRightResult> Map <TLeft, TLeftResult, TRight, TRightResult>( this IEither <TLeft, TRight> source, Func <TLeft, TLeftResult> selectLeft, Func <TRight, TRightResult> selectRight) { return(source.Match( onLeft: leftValue => new Either <TLeftResult, TRightResult>(selectLeft(leftValue)), onRight: rightValue => new Either <TLeftResult, TRightResult>(selectRight(rightValue)))); }
/// <summary> /// Performs an given Action based on whether the IEither(TLeft, TRight) contains a Left or /// Right value. /// </summary> /// <typeparam name="TLeft">The type of a potential Left value.</typeparam> /// <typeparam name="TRight">The type of a potential Right value.</typeparam> /// <param name="either">An IEither(TLeft, TRight) instance.</param> /// <param name="leftAction">Action used for a Left value.</param> /// <param name="rightAction">Action used for a Right value.</param> public static void Match <TLeft, TRight>( this IEither <TLeft, TRight> either, Action <TLeft> leftAction, Action <TRight> rightAction) { either.Match <object>( left => { leftAction(left); return(null); // We're not using the result of the match, so just return null. }, right => { rightAction(right); return(null); }); }
public EitherEnumerable <TLeft, TResult> Bind <TResult>(Func <TRight, EitherEnumerable <TLeft, TResult> > f) { return(new EitherEnumerable <TLeft, TResult>(Out.Match( right: xs => xs.Select(x => f(x).Out).Sequence().Select(ys => ys.SelectMany(BasicFunctions.Identity)), left: l => l.AsLeft <TLeft, IEnumerable <TResult> >()))); }
public static IMaybe <TRight> ToMaybe <TLeft, TRight>(this IEither <TLeft, TRight> m) => m.Match(_ => (IMaybe <TRight>) new Nothing <TRight>(), r => new Just <TRight>(r));
/// <summary> /// Applies the <paramref name="selector" /> to the right side and flattens. /// </summary> /// <param name="this">The <see cref="IEither{TL,TR}" /> to select on.</param> /// <param name="selector">The function to apply to the right side of the <see cref="IEither{TL,TR}" />.</param> /// <typeparam name="TL">The left type of the <see cref="Either{TL,TR}" />.</typeparam> /// <typeparam name="TR">The right type of the <see cref="Either{TL,TR}" />.</typeparam> /// <typeparam name="TOut">The output type of the <paramref name="selector" />.</typeparam> /// <returns>An <see cref="IEither{TL,TR}" />.</returns> public static IEither <TL, TOut> SelectMany <TL, TR, TOut>(this IEither <TL, TR> @this, Func <TR, IEither <TL, TOut> > selector) => @this.Match(x => OfLeft <TL, TOut>(x), selector);