public static async ValueTask <Fin <B> > Traverse <A, B>(this Fin <ValueTask <A> > ma, Func <A, B> f) { if (ma.IsFail) { return(ma.Cast <B>()); } return(Fin <B> .Succ(f(await ma.Value.ConfigureAwait(false)))); }
public static Option <Fin <B> > Traverse <A, B>(this Fin <Option <A> > ma, Func <A, B> f) { if (ma.IsFail) { return(Some <Fin <B> >(ma.Cast <B>())); } else if (ma.Value.IsNone) { return(None); } else { return(Some(FinSucc(f(ma.Value.Value)))); } }
public static Try <Fin <B> > Traverse <A, B>(this Fin <Try <A> > ma, Func <A, B> f) => () => { if (ma.IsFail) { return(new Result <Fin <B> >(ma.Cast <B>())); } var mr = ma.Value(); if (mr.IsBottom) { return(new Result <Fin <B> >(BottomException.Default)); } if (mr.IsFaulted) { return(new Result <Fin <B> >(mr.Exception)); } return(new Result <Fin <B> >(Fin <B> .Succ(f(mr.Value)))); };
public static Validation <Fail, Fin <B> > Traverse <Fail, A, B>(this Fin <Validation <Fail, A> > ma, Func <A, B> f) { if (ma.IsFail) { return(Validation <Fail, Fin <B> > .Success(ma.Cast <B>())); } else { var mb = (Validation <Fail, A>)ma; if (mb.IsFail) { return(Validation <Fail, Fin <B> > .Fail((Seq <Fail>) mb)); } else { return(Validation <Fail, Fin <B> > .Success(f((A)mb))); } } }
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 TryAsync <Fin <B> > Traverse <A, B>(this Fin <TryAsync <A> > ma, Func <A, B> f) { return(ToTry(() => Go(ma, f))); async Task <Result <Fin <B> > > Go(Fin <TryAsync <A> > ma, Func <A, B> f) { if (ma.IsFail) { return(new Result <Fin <B> >(ma.Cast <B>())); } var rb = await ma.Value.Try().ConfigureAwait(false); if (rb.IsFaulted) { return(new Result <Fin <B> >(rb.Exception)); } return(new Result <Fin <B> >(Fin <B> .Succ(f(rb.Value)))); } }
public static Validation <MonoidFail, Fail, Fin <B> > Traverse <MonoidFail, Fail, A, B>(this Fin <Validation <MonoidFail, Fail, A> > ma, Func <A, B> f) where MonoidFail : struct, Monoid <Fail>, Eq <Fail> { if (ma.IsFail) { return(Validation <MonoidFail, Fail, Fin <B> > .Success(ma.Cast <B>())); } else { var mb = (Validation <MonoidFail, Fail, A>)ma; if (mb.IsFail) { return(Validation <MonoidFail, Fail, Fin <B> > .Fail(mb.FailValue)); } else { return(Validation <MonoidFail, Fail, Fin <B> > .Success(f((A)mb))); } } }
public static EitherAsync <L, Fin <B> > Traverse <L, A, B>(this Fin <EitherAsync <L, A> > ma, Func <A, B> f) { return(new EitherAsync <L, Fin <B> >(Go(ma, f))); async Task <EitherData <L, Fin <B> > > Go(Fin <EitherAsync <L, A> > ma, Func <A, B> f) { if (ma.IsFail) { return(EitherData.Right <L, Fin <B> >(ma.Cast <B>())); } var da = await ma.Value.Data.ConfigureAwait(false); if (da.State == EitherStatus.IsBottom) { return(EitherData <L, Fin <B> > .Bottom); } if (da.State == EitherStatus.IsLeft) { return(EitherData.Left <L, Fin <B> >(da.Left)); } return(EitherData.Right <L, Fin <B> >(f(da.Right))); } }