public static EitherUnsafe <L, TryOption <B> > Traverse <L, A, B>(this TryOption <EitherUnsafe <L, A> > ma, Func <A, B> f) { var tres = ma.Try(); if (tres.IsBottom) { return(EitherUnsafe <L, TryOption <B> > .Bottom); } else if (tres.IsFaulted) { return(Right(TryOptionFail <B>(tres.Exception))); } else if (tres.IsNone) { return(Right(TryOptional <B>(None))); } else if (tres.Value.Value.IsLeft) { return(EitherUnsafe <L, TryOption <B> > .Left((L)tres.Value.Value)); } else { return(EitherUnsafe <L, TryOption <B> > .Right(TryOption(f((A)tres.Value.Value)))); } }
public static EitherUnsafe <L, Identity <B> > Traverse <L, A, B>(this Identity <EitherUnsafe <L, A> > ma, Func <A, B> f) { if (ma.Value.IsLeft) { return(EitherUnsafe <L, Identity <B> > .Left((L)ma.Value)); } else { return(EitherUnsafe <L, Identity <B> > .Right(new Identity <B>(f((A)ma.Value)))); } }
public static async ValueTask <EitherUnsafe <L, B> > Traverse <L, A, B>(this EitherUnsafe <L, ValueTask <A> > ma, Func <A, B> f) { if (ma.IsBottom) { return(EitherUnsafe <L, B> .Bottom); } else if (ma.IsLeft) { return(EitherUnsafe <L, B> .Left(ma.LeftValue)); } return(EitherUnsafe <L, B> .Right(f(await ma.RightValue.ConfigureAwait(false)))); }
public static Fin <EitherUnsafe <L, B> > Traverse <L, A, B>(this EitherUnsafe <L, Fin <A> > ma, Func <A, B> f) { if (ma.IsLeft) { return(FinSucc(EitherUnsafe <L, B> .Left(ma.LeftValue))); } else if (ma.RightValue.IsFail) { return(ma.RightValue.Cast <EitherUnsafe <L, B> >()); } else { return(Fin <EitherUnsafe <L, B> > .Succ(f(ma.RightValue.Value))); } }
public static OptionUnsafe <EitherUnsafe <L, B> > Traverse <L, A, B>(this EitherUnsafe <L, OptionUnsafe <A> > ma, Func <A, B> f) { if (ma.IsLeft) { return(SomeUnsafe(EitherUnsafe <L, B> .Left(ma.LeftValue))); } else if (ma.RightValue.IsNone) { return(None); } else { return(OptionUnsafe <EitherUnsafe <L, B> > .Some(f(ma.RightValue.Value))); } }
public static EitherUnsafe <L, IEnumerable <B> > Traverse <L, A, B>(this IEnumerable <EitherUnsafe <L, A> > ma, Func <A, B> f) { var res = new List <B>(); foreach (var x in ma) { if (x.IsLeft) { return(EitherUnsafe <L, IEnumerable <B> > .Left((L)x)); } else { res.Add(f((A)x)); } } return(Seq.FromArray <B>(res.ToArray())); }
public static EitherUnsafe <L, EitherUnsafe <L, B> > Traverse <L, A, B>(this EitherUnsafe <L, EitherUnsafe <L, A> > ma, Func <A, B> f) { if (ma.IsLeft) { return(Right(EitherUnsafe <L, B> .Left((L)ma))); } else { var mb = (EitherUnsafe <L, A>)ma; if (mb.IsLeft) { return(EitherUnsafe <L, EitherUnsafe <L, B> > .Left((L)mb)); } else { return(EitherUnsafe <L, EitherUnsafe <L, B> > .Right(f((A)mb))); } } }
public static EitherUnsafe <L, Stck <B> > Traverse <L, A, B>(this Stck <EitherUnsafe <L, A> > ma, Func <A, B> f) { var res = new B[ma.Count]; var ix = ma.Count - 1; foreach (var x in ma) { if (x.IsLeft) { return(EitherUnsafe <L, Stck <B> > .Left((L)x)); } else { res[ix] = f((A)x); ix--; } } return(new Stck <B>(res)); }
public static EitherUnsafe <L, Fin <B> > Traverse <L, A, B>(this Fin <EitherUnsafe <L, A> > ma, Func <A, B> f) { if (ma.IsFail) { return(Right(ma.Cast <B>())); } else { var mb = (EitherUnsafe <L, A>)ma; if (mb.IsLeft) { return(EitherUnsafe <L, Fin <B> > .Left((L)mb)); } else { return(EitherUnsafe <L, Fin <B> > .Right(f((A)mb))); } } }
public static EitherUnsafe <L, HashSet <B> > Traverse <L, A, B>(this HashSet <EitherUnsafe <L, A> > ma, Func <A, B> f) { var res = new B[ma.Count]; var ix = 0; foreach (var x in ma) { if (x.IsLeft) { return(EitherUnsafe <L, HashSet <B> > .Left((L)x)); } else { res[ix] = f((A)x); ix++; } } return(new HashSet <B>(res)); }
public static EitherUnsafe <L, Validation <Fail, B> > Traverse <Fail, L, A, B>(this Validation <Fail, EitherUnsafe <L, A> > ma, Func <A, B> f) { if (ma.IsFail) { return(Right(Validation <Fail, B> .Fail(ma.FailValue))); } else { var mb = ma.SuccessValue; if (mb.IsLeft) { return(EitherUnsafe <L, Validation <Fail, B> > .Left((L)mb)); } else { return(EitherUnsafe <L, Validation <Fail, B> > .Right(f((A)mb))); } } }
public static EitherUnsafe <Fail, Validation <MonoidFail, Fail, B> > Traverse <MonoidFail, Fail, A, B>(this Validation <MonoidFail, Fail, EitherUnsafe <Fail, A> > ma, Func <A, B> f) where MonoidFail : struct, Monoid <Fail>, Eq <Fail> { if (ma.IsFail) { return(Right(Validation <MonoidFail, Fail, B> .Fail(ma.FailValue))); } else { var mb = ma.SuccessValue; if (mb.IsLeft) { return(EitherUnsafe <Fail, Validation <MonoidFail, Fail, B> > .Left((Fail)mb)); } else { return(EitherUnsafe <Fail, Validation <MonoidFail, Fail, B> > .Right(f((A)mb))); } } }
public static EitherUnsafe <L, Eff <B> > Traverse <L, A, B>(this Eff <EitherUnsafe <L, A> > ma, Func <A, B> f) { var tres = ma.Run(); if (tres.IsBottom) { return(EitherUnsafe <L, Eff <B> > .Bottom); } else if (tres.IsFail) { return(RightUnsafe(FailEff <B>(tres.Error))); } else if (tres.Value.IsLeft) { return(EitherUnsafe <L, Eff <B> > .Left((L)tres.Value)); } else { return(EitherUnsafe <L, Eff <B> > .Right(SuccessEff(f((A)tres.Value)))); } }
public static Lst <EitherUnsafe <L, B> > Traverse <L, A, B>(this EitherUnsafe <L, Lst <A> > ma, Func <A, B> f) => ma.MatchUnsafe( Left: e => List(EitherUnsafe <L, B> .Left(e)), Right: xs => xs.Map(x => RightUnsafe <L, B>(f(x))));
public static Que <EitherUnsafe <L, B> > Traverse <L, A, B>(this EitherUnsafe <L, Que <A> > ma, Func <A, B> f) => ma.MatchUnsafe( Left: ex => Queue(EitherUnsafe <L, B> .Left(ex)), Right: xs => new Que <EitherUnsafe <L, B> >(xs.Map(x => RightUnsafe <L, B>(f(x)))));
public static Stck <EitherUnsafe <L, B> > Traverse <L, A, B>(this EitherUnsafe <L, Stck <A> > ma, Func <A, B> f) => ma.MatchUnsafe( Left: ex => Stack(EitherUnsafe <L, B> .Left(ex)), Right: xs => toStack(xs.Map(x => RightUnsafe <L, B>(f(x)))));
public EitherUnsafe <L, Ret> MapUnsafe <Ret>(Func <R, Ret> mapper) => IsRight ? mapper(RightValue) : EitherUnsafe <L, Ret> .Left(LeftValue);
/// <summary> /// Either constructor /// Constructs an Either in a Left state /// </summary> /// <typeparam name="L">Left</typeparam> /// <typeparam name="R">Right</typeparam> /// <param name="value">Left value</param> /// <returns>A new Either instance</returns> public static EitherUnsafe <L, R> LeftUnsafe <L, R>(L value) => EitherUnsafe <L, R> .Left(value);
public static Identity <EitherUnsafe <L, B> > Traverse <L, A, B>(this EitherUnsafe <L, Identity <A> > ma, Func <A, B> f) => ma.MatchUnsafe( Right: x => new Identity <EitherUnsafe <L, B> >(f(x.Value)), Left: e => new Identity <EitherUnsafe <L, B> >(EitherUnsafe <L, B> .Left(e)));
public EitherUnsafe <L, Ret> BindUnsafe <Ret>(Func <R, EitherUnsafe <L, Ret> > binder) => IsRight ? binder(RightValue) : EitherUnsafe <L, Ret> .Left(LeftValue);
public static EitherUnsafe <R, L> LeftUnsafe <R, L>(L value) => EitherUnsafe <R, L> .Left(value);