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 Fin <Func <B, C> > apply <A, B, C>(Func <A, Func <B, C> > fabc, Fin <A> fa) { if (fa.IsFail) { return(Fin <Func <B, C> > .Fail(fa.Error)); } return(fabc(fa.Value)); }
public static Fin <B> apply <A, B>(Fin <Func <A, B> > fab, Fin <A> fa) { if (fab.IsFail) { return(Fin <B> .Fail(fab.Error)); } if (fa.IsFail) { return(Fin <B> .Fail(fa.Error)); } return(fab.Value(fa.Value)); }
public static Fin <C> apply <A, B, C>(Func <A, B, C> fabc, Fin <A> fa, Fin <B> fb) { if (fa.IsFail) { return(Fin <C> .Fail(fa.Error)); } if (fb.IsFail) { return(Fin <C> .Fail(fb.Error)); } return(fabc(fa.Value, fb.Value)); }
public static Fin <IEnumerable <B> > Traverse <A, B>(this IEnumerable <Fin <A> > ma, Func <A, B> f) { var res = new List <B>(); foreach (var xs in ma) { if (xs.IsFail) { return(xs.Cast <IEnumerable <B> >()); } res.Add(f(xs.Value)); } return(Fin <IEnumerable <B> > .Succ(Seq.FromArray <B>(res.ToArray()))); }
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 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 Fin <Lst <B> > Traverse <A, B>(this Lst <Fin <A> > ma, Func <A, B> f) { var res = new B[ma.Count]; var ix = 0; foreach (var xs in ma) { if (xs.IsFail) { return(xs.Cast <Lst <B> >()); } res[ix] = f(xs.Value); ix++; } return(Fin <Lst <B> > .Succ(new Lst <B>(res))); }
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 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))); } }
public static Lst <Fin <B> > Traverse <A, B>(this Fin <Lst <A> > ma, Func <A, B> f) => ma.Match( Fail: er => List(Fin <B> .Fail(er)), Succ: xs => xs.Map(x => FinSucc(f(x))));
public static Eff <RT, Fin <A> > Sequence <RT, A>(this Fin <Eff <RT, A> > mma) where RT : struct => mma.Traverse(identity);
public static Validation <Fail, Fin <B> > Sequence <Fail, A, B>(this Fin <A> ta, Func <A, Validation <Fail, B> > f) => ta.Map(f).Sequence();
public static Que <Fin <B> > Traverse <A, B>(this Fin <Que <A> > ma, Func <A, B> f) => ma.Match( Fail: er => Queue(FinFail <B>(er)), Succ: xs => xs.Map(x => FinSucc(f(x))));
public static Seq <Fin <A> > Sequence <A>(this Fin <Seq <A> > ta) => ta.Traverse(identity);
public static Fin <Fin <B> > Traverse <A, B>(this Fin <Fin <A> > ma, Func <A, B> f) => ma.Bind(a => a.Map(f));
public static TryAsync <Fin <A> > Sequence <A>(this Fin <TryAsync <A> > ta) => ta.Traverse(identity);
public static EitherAsync <L, Fin <B> > Sequence <L, A, B>(this Fin <A> ta, Func <A, EitherAsync <L, B> > f) => ta.Map(f).Sequence();
public static Aff <Fin <B> > Sequence <A, B>(this Fin <A> ta, Func <A, Aff <B> > f) => ta.Map(f).Sequence();
public static TryAsync <Fin <B> > Sequence <A, B>(this Fin <A> ta, Func <A, TryAsync <B> > f) => ta.Map(f).Sequence();
public static Lst <Fin <A> > Sequence <A>(this Fin <Lst <A> > ta) => ta.Traverse(identity);
public static EitherUnsafe <L, Fin <A> > Sequence <L, A>(this Fin <EitherUnsafe <L, A> > ta) => ta.Traverse(identity);
public static Fin <Identity <B> > Traverse <A, B>(this Identity <Fin <A> > ma, Func <A, B> f) => ma.Value.IsSucc ? Fin <Identity <B> > .Succ(new Identity <B>(f(ma.Value.Value))) : ma.Value.Cast <Identity <B> >();
public static Eff <RT, Fin <B> > Sequence <RT, A, B>(this Fin <A> ta, Func <A, Eff <RT, B> > f) where RT : struct => ta.Map(f).Sequence();
public static Que <Fin <B> > Sequence <A, B>(this Fin <A> ta, Func <A, Que <B> > f) => ta.Map(f).Traverse(Prelude.identity);
public static Eff <Fin <A> > Sequence <A>(this Fin <Eff <A> > mma) => mma.Traverse(identity);
public static Task <Fin <B> > Sequence <A, B>(this Fin <A> ta, Func <A, Task <B> > f) => ta.Map(f).Sequence();