Exemple #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))));
 }
Exemple #2
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())));
        }
Exemple #3
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)));
     }
 }
Exemple #4
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)));
        }
Exemple #5
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))));
 };
Exemple #6
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))));
            }
        }
Exemple #7
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> >();
Exemple #8
0
 public static Fin <A> FinSucc <A>(A value) =>
 Fin <A> .Succ(value);