Beispiel #1
0
 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))));
 }
Beispiel #2
0
 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));
 }
Beispiel #3
0
 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));
 }
Beispiel #4
0
 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));
 }
Beispiel #5
0
        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())));
        }
Beispiel #6
0
 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)));
     }
 }
Beispiel #7
0
 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))));
     }
 }
Beispiel #8
0
        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)));
        }
Beispiel #9
0
 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)));
         }
     }
 }
Beispiel #12
0
        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)));
            }
        }
Beispiel #14
0
 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))));
Beispiel #15
0
 public static Eff <RT, Fin <A> > Sequence <RT, A>(this Fin <Eff <RT, A> > mma) where RT : struct =>
 mma.Traverse(identity);
Beispiel #16
0
 public static Validation <Fail, Fin <B> > Sequence <Fail, A, B>(this Fin <A> ta, Func <A, Validation <Fail, B> > f) =>
 ta.Map(f).Sequence();
Beispiel #17
0
 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))));
Beispiel #18
0
 public static Seq <Fin <A> > Sequence <A>(this Fin <Seq <A> > ta) =>
 ta.Traverse(identity);
Beispiel #19
0
 public static Fin <Fin <B> > Traverse <A, B>(this Fin <Fin <A> > ma, Func <A, B> f) =>
 ma.Bind(a => a.Map(f));
Beispiel #20
0
 public static TryAsync <Fin <A> > Sequence <A>(this Fin <TryAsync <A> > ta) =>
 ta.Traverse(identity);
Beispiel #21
0
 public static EitherAsync <L, Fin <B> > Sequence <L, A, B>(this Fin <A> ta, Func <A, EitherAsync <L, B> > f) =>
 ta.Map(f).Sequence();
Beispiel #22
0
 public static Aff <Fin <B> > Sequence <A, B>(this Fin <A> ta, Func <A, Aff <B> > f) =>
 ta.Map(f).Sequence();
Beispiel #23
0
 public static TryAsync <Fin <B> > Sequence <A, B>(this Fin <A> ta, Func <A, TryAsync <B> > f) =>
 ta.Map(f).Sequence();
Beispiel #24
0
 public static Lst <Fin <A> > Sequence <A>(this Fin <Lst <A> > ta) =>
 ta.Traverse(identity);
Beispiel #25
0
 public static EitherUnsafe <L, Fin <A> > Sequence <L, A>(this Fin <EitherUnsafe <L, A> > ta) =>
 ta.Traverse(identity);
Beispiel #26
0
 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> >();
Beispiel #27
0
 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();
Beispiel #28
0
 public static Que <Fin <B> > Sequence <A, B>(this Fin <A> ta, Func <A, Que <B> > f) =>
 ta.Map(f).Traverse(Prelude.identity);
Beispiel #29
0
 public static Eff <Fin <A> > Sequence <A>(this Fin <Eff <A> > mma) =>
 mma.Traverse(identity);
Beispiel #30
0
 public static Task <Fin <B> > Sequence <A, B>(this Fin <A> ta, Func <A, Task <B> > f) =>
 ta.Map(f).Sequence();