public static EitherUnsafe <Exception, A> ToEitherUnsafe <A>(this Try <A> self) { var res = self.Try(); return(res.IsFaulted ? EitherUnsafe <Exception, A> .Left(res.Exception) : EitherUnsafe <Exception, A> .Right(res.Value)); }
public static EitherUnsafe <L, A> ToEitherUnsafe <A, L>(this Try <A> self, Func <Exception, L> Fail) { var res = self.Try(); return(res.IsFaulted ? EitherUnsafe <L, A> .Left(Fail(res.Exception)) : EitherUnsafe <L, A> .Right(res.Value)); }
/// <summary> /// Filter the Either /// This may give unpredictable results for a filtered value. The Either won't /// return true for IsLeft or IsRight. IsBottom is True if the value is filterd and that /// should be checked. /// </summary> /// <typeparam name="L">Left</typeparam> /// <typeparam name="R">Right</typeparam> /// <param name="self">Either to filter</param> /// <param name="pred">Predicate function</param> /// <returns>If the Either is in the Left state it is returned as-is. /// If in the Left state the predicate is applied to the Left value. /// If the predicate returns True the Either is returned as-is. /// If the predicate returns False the Either is returned in a 'Bottom' state.</returns> public static EitherUnsafe <L, R> Filter <L, R>(this EitherUnsafe <L, R> self, Func <L, bool> pred) => self.IsBottom ? self : self.MatchUnsafe( Right: (R r) => EitherUnsafe <L, R> .Right(r), Left: (L t) => pred(t) ? EitherUnsafe <L, R> .Left(t) : new EitherUnsafe <L, R>(true) );
public static EitherUnsafe <L, VR> SelectMany <L, TR, UR, VR>(this EitherUnsafe <L, TR> self, Func <TR, EitherUnsafe <L, UR> > bind, Func <TR, UR, VR> project ) => matchUnsafe(self, Right: t => matchUnsafe(bind(t), Right: u => EitherUnsafe <L, VR> .Right(project(t, u)), Left: l => EitherUnsafe <L, VR> .Left(l) ), Left: l => EitherUnsafe <L, VR> .Left(l) );
public static EitherUnsafe <L, V> SelectMany <L, T, U, V>(this EitherUnsafe <L, T> self, Func <T, EitherUnsafe <L, U> > bind, Func <T, U, V> project) { if (self.IsBottom) { return(new EitherUnsafe <L, V>(true)); } if (self.IsLeft) { return(EitherUnsafe <L, V> .Left(self.LeftValue)); } var u = bind(self.RightValue); if (u.IsBottom) { return(new EitherUnsafe <L, V>(true)); } if (u.IsLeft) { return(EitherUnsafe <L, V> .Left(u.LeftValue)); } return(project(self.RightValue, u.RightValue)); }
public static EitherUnsafe <L, UR> Select <L, TR, UR>(this EitherUnsafe <L, TR> self, Func <TR, UR> map) => matchUnsafe(self, Right: t => EitherUnsafe <L, UR> .Right(map(t)), Left: l => EitherUnsafe <L, UR> .Left(l) );
public EitherUnsafe <L2, R2> BiMap(EitherUnsafe <L, R> ma, Func <L, L2> fa, Func <R, R2> fb) => default(MEitherUnsafe <L, R>).MatchUnsafe(ma, Left: a => EitherUnsafe <L2, R2> .Left(Check.NullReturn(fa(a))), Right: b => EitherUnsafe <L2, R2> .Right(Check.NullReturn(fb(b))), Bottom: () => EitherUnsafe <L2, R2> .Bottom);
public static Task <EitherUnsafe <L, A> > ToEitherUnsafe <A, L>(this TryAsync <A> self, Func <Exception, L> Fail) => self.Match( Succ: v => EitherUnsafe <L, A> .Right(v), Fail: x => EitherUnsafe <L, A> .Left(Fail(x)));
public static Task <EitherUnsafe <Exception, A> > ToEitherUnsafe <A>(this TryAsync <A> self) => self.Match( Succ: v => EitherUnsafe <Exception, A> .Right(v), Fail: x => EitherUnsafe <Exception, A> .Left(x));
public EitherUnsafe <L, R> Fail(object err) => err is L ? EitherUnsafe <L, R> .Left((L)err) : EitherUnsafe <L, R> .Bottom;
/// <summary> /// Filter the Either /// This may give unpredictable results for a filtered value. The Either won't /// return true for IsLeft or IsRight. IsBottom is True if the value is filterd and that /// should be checked. /// </summary> /// <typeparam name="L">Left</typeparam> /// <typeparam name="R">Right</typeparam> /// <param name="self">Either to filter</param> /// <param name="pred">Predicate function</param> /// <returns>If the Either is in the Left state it is returned as-is. /// If in the Right state the predicate is applied to the Right value. /// If the predicate returns True the Either is returned as-is. /// If the predicate returns False the Either is returned in a 'Bottom' state. IsLeft will return True, but the value /// of Left = default(L)</returns> public static EitherUnsafe <L, R> Filter <L, R>(this EitherUnsafe <L, R> self, Func <R, bool> pred) => self.IsBottom ? self : matchUnsafe(self, Right: t => pred(t) ? EitherUnsafe <L, R> .Right(t) : new EitherUnsafe <L, R>(true), Left: l => EitherUnsafe <L, R> .Left(l));
/// <summary> /// Monadic bind function /// https://en.wikipedia.org/wiki/Monad_(functional_programming) /// </summary> /// <typeparam name="L">Left</typeparam> /// <typeparam name="R">Right</typeparam> /// <typeparam name="Ret"></typeparam> /// <param name="self"></param> /// <param name="binder"></param> /// <returns>Bound Either</returns> public static EitherUnsafe <L, Ret> Bind <L, R, Ret>(this EitherUnsafe <L, R> self, Func <R, EitherUnsafe <L, Ret> > binder) => self.IsBottom ? new EitherUnsafe <L, Ret>(true) : self.IsRight ? binder(self.RightValue) : EitherUnsafe <L, Ret> .Left(self.LeftValue);
/// <summary> /// Bi-filter the Either /// </summary> /// <remarks> /// This may give unpredictable results for a filtered value. The Either won't /// return true for IsLeft or IsRight. IsBottom is True if the value is filtered and that /// should be checked for. /// </remarks> /// <typeparam name="L">Left</typeparam> /// <typeparam name="R">Right</typeparam> /// <param name="self">Either to filter</param> /// <param name="pred">Predicate function</param> /// <returns> /// If the Either is in the Left state then the Left predicate is run against it. /// If the Either is in the Right state then the Right predicate is run against it. /// If the predicate returns False the Either is returned in a 'Bottom' state.</returns> public static EitherUnsafe <L, R> Filter <L, R>(this EitherUnsafe <L, R> self, Func <R, bool> Right, Func <L, bool> Left) => self.IsBottom ? self : matchUnsafe(self, Right: r => Right(r) ? EitherUnsafe <L, R> .Right(r) : new EitherUnsafe <L, R>(true), Left: l => Left(l) ? EitherUnsafe <L, R> .Left(l) : new EitherUnsafe <L, R>(true));