public static EitherAsync <L, OptionAsync <B> > Traverse <L, A, B>(this OptionAsync <EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, OptionAsync <B> >(Go(ma, f)));

            async Task <EitherData <L, OptionAsync <B> > > Go(OptionAsync <EitherAsync <L, A> > ma, Func <A, B> f)
            {
                var(isSome, value) = await ma.Data.ConfigureAwait(false);

                if (!isSome)
                {
                    return(EitherData.Right <L, OptionAsync <B> >(OptionAsync <B> .None));
                }
                var db = await value.Data.ConfigureAwait(false);

                if (db.State == EitherStatus.IsBottom)
                {
                    return(EitherData <L, OptionAsync <B> > .Bottom);
                }
                if (db.State == EitherStatus.IsLeft)
                {
                    return(EitherData.Left <L, OptionAsync <B> >(db.Left));
                }
                return(EitherData.Right <L, OptionAsync <B> >(OptionAsync <B> .Some(f(db.Right))));
            }
        }
        public static EitherAsync <L, TryOptionAsync <B> > Traverse <L, A, B>(this TryOptionAsync <EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, TryOptionAsync <B> >(Go(ma, f)));

            async Task <EitherData <L, TryOptionAsync <B> > > Go(TryOptionAsync <EitherAsync <L, A> > ma, Func <A, B> f)
            {
                var result = await ma.Try().ConfigureAwait(false);

                if (result.IsBottom)
                {
                    return(EitherData <L, TryOptionAsync <B> > .Bottom);
                }
                if (result.IsFaulted)
                {
                    return(EitherData.Right <L, TryOptionAsync <B> >(TryOptionAsyncFail <B>(result.Exception)));
                }
                if (result.IsNone)
                {
                    return(EitherData.Right <L, TryOptionAsync <B> >(TryOptionAsync <B>(None)));
                }
                var db = await result.Value.Value.Data.ConfigureAwait(false);

                if (db.State == EitherStatus.IsBottom)
                {
                    return(EitherData <L, TryOptionAsync <B> > .Bottom);
                }
                if (db.State == EitherStatus.IsLeft)
                {
                    return(EitherData.Left <L, TryOptionAsync <B> >(db.Left));
                }
                return(EitherData.Right <L, TryOptionAsync <B> >(TryOptionAsync <B>(f(db.Right))));
            }
        }
        public static EitherAsync <L, Aff <B> > Traverse <L, A, B>(this Aff <EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, Aff <B> >(Go(ma, f)));

            async Task <EitherData <L, Aff <B> > > Go(Aff <EitherAsync <L, A> > ma, Func <A, B> f)
            {
                var result = await ma.Run().ConfigureAwait(false);

                if (result.IsBottom)
                {
                    return(EitherData <L, Aff <B> > .Bottom);
                }
                if (result.IsFail)
                {
                    return(EitherData.Right <L, Aff <B> >(FailAff <B>(result.Error)));
                }
                var db = await result.Value.Data.ConfigureAwait(false);

                if (db.State == EitherStatus.IsBottom)
                {
                    return(EitherData <L, Aff <B> > .Bottom);
                }
                if (db.State == EitherStatus.IsLeft)
                {
                    return(EitherData.Left <L, Aff <B> >(db.Left));
                }
                return(EitherData.Right <L, Aff <B> >(SuccessAff <B>(f(db.Right))));
            }
        }
        public static EitherAsync <L, Seq <B> > TraverseSerial <L, A, B>(this Seq <EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, Seq <B> >(Go(ma, f)));

            async Task <EitherData <L, Seq <B> > > Go(Seq <EitherAsync <L, A> > ma, Func <A, B> f)
            {
                var rb = new B[ma.Count];
                var ix = 0;

                foreach (var a in ma)
                {
                    var mb = await a;
                    if (mb.IsBottom)
                    {
                        return(EitherData.Bottom <L, Seq <B> >());
                    }
                    if (mb.IsLeft)
                    {
                        return(EitherData.Left <L, Seq <B> >(mb.LeftValue));
                    }
                    rb[ix] = f(mb.RightValue);
                    ix++;
                }

                return(EitherData.Right <L, Seq <B> >(Seq.FromArray <B>(rb)));
            };
        }
        public static EitherAsync <L, EitherUnsafe <L, B> > Traverse <L, A, B>(this EitherUnsafe <L, EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, EitherUnsafe <L, B> >(Go(ma, f)));

            async Task <EitherData <L, EitherUnsafe <L, B> > > Go(EitherUnsafe <L, EitherAsync <L, A> > ma, Func <A, B> f)
            {
                if (ma.IsBottom)
                {
                    return(EitherData <L, EitherUnsafe <L, B> > .Bottom);
                }
                if (ma.IsLeft)
                {
                    return(EitherData.Right <L, EitherUnsafe <L, B> >(Left(ma.LeftValue)));
                }
                var da = await ma.RightValue.Data.ConfigureAwait(false);

                if (da.State == EitherStatus.IsBottom)
                {
                    return(EitherData <L, EitherUnsafe <L, B> > .Bottom);
                }
                if (da.State == EitherStatus.IsLeft)
                {
                    return(EitherData.Left <L, EitherUnsafe <L, B> >(da.Left));
                }
                return(EitherData.Right <L, EitherUnsafe <L, B> >(f(da.Right)));
            }
        }
        public static EitherAsync <L, IEnumerable <B> > TraverseSerial <L, A, B>(this IEnumerable <EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, IEnumerable <B> >(Go(ma, f)));

            async Task <EitherData <L, IEnumerable <B> >?> Go(IEnumerable <EitherAsync <L, A> > ma, Func <A, B> f)
            {
                var rb = new List <B>();

                foreach (var a in ma)
                {
                    var mb = await a;
                    if (mb.IsBottom)
                    {
                        return(EitherData <L, IEnumerable <B> > .Bottom);
                    }
                    if (mb.IsLeft)
                    {
                        return(EitherData.Left <L, IEnumerable <B> >(mb.LeftValue));
                    }
                    rb.Add(f(mb.RightValue));
                }

                return(EitherData.Right <L, IEnumerable <B> >(rb));
            };
        }
Example #7
0
        //
        // Async types
        //

        public static EitherAsync <L, EitherAsync <L, B> > Traverse <L, A, B>(this EitherAsync <L, EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, EitherAsync <L, B> >(Go(ma, f)));

            async Task <EitherData <L, EitherAsync <L, B> > > Go(EitherAsync <L, EitherAsync <L, A> > ma, Func <A, B> f)
            {
                var da = await ma.Data;

                if (da.State == EitherStatus.IsBottom)
                {
                    return(EitherData <L, EitherAsync <L, B> > .Bottom);
                }
                if (da.State == EitherStatus.IsLeft)
                {
                    return(EitherData.Right <L, EitherAsync <L, B> >(EitherAsync <L, B> .Left(da.Left)));
                }
                var db = await da.Right.Data;

                if (db.State == EitherStatus.IsBottom)
                {
                    return(EitherData <L, EitherAsync <L, B> > .Bottom);
                }
                if (db.State == EitherStatus.IsLeft)
                {
                    return(EitherData.Left <L, EitherAsync <L, B> >(db.Left));
                }
                return(EitherData.Right <L, EitherAsync <L, B> >(EitherAsync <L, B> .Right(f(db.Right))));
            }
        }
Example #8
0
        public static EitherAsync <L, Seq <B> > TraverseParallel <L, A, B>(this Seq <EitherAsync <L, A> > ma, int windowSize, Func <A, B> f)
        {
            return(new EitherAsync <L, Seq <B> >(Go(ma, f)));

            async Task <EitherData <L, Seq <B> > > Go(Seq <EitherAsync <L, A> > ma, Func <A, B> f)
            {
                var rb = await ma.Map(a => a.Map(f).Data).WindowMap(windowSize, identity);

                return(rb.Exists(d => d.State == EitherStatus.IsLeft)
                     ? rb.Filter(d => d.State == EitherStatus.IsLeft).Map(d => EitherData.Left <L, Seq <B> >(d.Left)).Head()
                     : EitherData.Right <L, Seq <B> >(Seq.FromArray(rb.Map(d => d.Right).ToArray())));
            }
        }
        public static EitherAsync <L, HashSet <B> > Traverse <L, A, B>(this HashSet <EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, HashSet <B> >(Go(ma, f)));

            async Task <EitherData <L, HashSet <B> > > Go(HashSet <EitherAsync <L, A> > ma, Func <A, B> f)
            {
                var rb = await Task.WhenAll(ma.Map(a => a.Map(f).Data)).ConfigureAwait(false);

                return(rb.Exists(d => d.State == EitherStatus.IsLeft)
                    ? rb.Filter(d => d.State == EitherStatus.IsLeft).Map(d => EitherData.Left <L, HashSet <B> >(d.Left)).Head()
                    : EitherData.Right <L, HashSet <B> >(new HashSet <B>(rb.Map(d => d.Right))));
            }
        }
Example #10
0
        public static EitherAsync <L, IEnumerable <B> > TraverseParallel <L, A, B>(this IEnumerable <EitherAsync <L, A> > ma, int windowSize, Func <A, B> f)
        {
            return(new EitherAsync <L, IEnumerable <B> >(Go(ma, f)));

            async Task <EitherData <L, IEnumerable <B> > > Go(IEnumerable <EitherAsync <L, A> > ma, Func <A, B> f)
            {
                var rb = await ma.Map(a => a.Map(f).Data).WindowMap(windowSize, identity).ConfigureAwait(false);

                return(rb.Exists(d => d.State == EitherStatus.IsLeft)
                    ? rb.Filter(d => d.State == EitherStatus.IsLeft).Map(d => EitherData.Left <L, IEnumerable <B> >(d.Left)).Head()
                    : EitherData.Right <L, IEnumerable <B> >(rb.Map(d => d.Right)));
            }
        }
Example #11
0
        public static EitherAsync <L, Task <B> > Traverse <L, A, B>(this Task <EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, Task <B> >(Go(ma, f)));

            async Task <EitherData <L, Task <B> > > Go(Task <EitherAsync <L, A> > ma, Func <A, B> f)
            {
                var result = await ma;
                var db     = await result.Data;

                if (db.State == EitherStatus.IsBottom)
                {
                    return(EitherData <L, Task <B> > .Bottom);
                }
                if (db.State == EitherStatus.IsLeft)
                {
                    return(EitherData.Left <L, Task <B> >(db.Left));
                }
                return(EitherData.Right <L, Task <B> >(f(db.Right).AsTask()));
            }
        }
Example #12
0
        public static EitherAsync <L, ValueTask <B> > Traverse <L, A, B>(this ValueTask <EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, ValueTask <B> >(Go(ma, f).AsTask()));

            async ValueTask <EitherData <L, ValueTask <B> > > Go(ValueTask <EitherAsync <L, A> > ma, Func <A, B> f)
            {
                var result = await ma.ConfigureAwait(false);

                var db = await result.Data.ConfigureAwait(false);

                if (db.State == EitherStatus.IsBottom)
                {
                    return(EitherData <L, ValueTask <B> > .Bottom);
                }
                if (db.State == EitherStatus.IsLeft)
                {
                    return(EitherData.Left <L, ValueTask <B> >(db.Left));
                }
                return(EitherData.Right <L, ValueTask <B> >(f(db.Right).AsValueTask()));
            }
        }
Example #13
0
        public static EitherAsync <L, TryOption <B> > Traverse <L, A, B>(this TryOption <EitherAsync <L, A> > ma, Func <A, B> f)
        {
            try
            {
                return(new EitherAsync <L, TryOption <B> >(Go(ma, f)));

                async Task <EitherData <L, TryOption <B> > > Go(TryOption <EitherAsync <L, A> > ma, Func <A, B> f)
                {
                    var ra = ma.Try();

                    if (ra.IsBottom)
                    {
                        return(EitherData <L, TryOption <B> > .Bottom);
                    }
                    if (ra.IsNone)
                    {
                        return(EitherData.Right <L, TryOption <B> >(TryOptional <B>(None)));
                    }
                    if (ra.IsFaulted)
                    {
                        return(EitherData.Right <L, TryOption <B> >(TryOptionFail <B>(ra.Exception)));
                    }
                    var da = await ra.Value.Value.Data.ConfigureAwait(false);

                    if (da.State == EitherStatus.IsBottom)
                    {
                        return(EitherData <L, TryOption <B> > .Bottom);
                    }
                    if (da.State == EitherStatus.IsLeft)
                    {
                        return(EitherData.Left <L, TryOption <B> >(da.Left));
                    }
                    return(EitherData.Right <L, TryOption <B> >(TryOption <B>(f(da.Right))));
                }
            }
            catch (Exception e)
            {
                return(TryOption <B>(e));
            }
        }
Example #14
0
        public static EitherAsync <L, Validation <Fail, B> > Traverse <Fail, L, A, B>(this Validation <Fail, EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, Validation <Fail, B> >(Go(ma, f)));

            async Task <EitherData <L, Validation <Fail, B> > > Go(Validation <Fail, EitherAsync <L, A> > ma, Func <A, B> f)
            {
                if (ma.IsFail)
                {
                    return(EitherData.Right <L, Validation <Fail, B> >(Fail <Fail, B>(ma.FailValue)));
                }
                var da = await ma.SuccessValue.Data.ConfigureAwait(false);

                if (da.State == EitherStatus.IsBottom)
                {
                    return(EitherData <L, Validation <Fail, B> > .Bottom);
                }
                if (da.State == EitherStatus.IsLeft)
                {
                    return(EitherData.Left <L, Validation <Fail, B> >(da.Left));
                }
                return(EitherData.Right <L, Validation <Fail, B> >(f(da.Right)));
            }
        }
Example #15
0
        public static EitherAsync <L, OptionUnsafe <B> > Traverse <L, A, B>(this OptionUnsafe <EitherAsync <L, A> > ma, Func <A, B> f)
        {
            return(new EitherAsync <L, OptionUnsafe <B> >(Go(ma, f)));

            async Task <EitherData <L, OptionUnsafe <B> > > Go(OptionUnsafe <EitherAsync <L, A> > ma, Func <A, B> f)
            {
                if (ma.IsNone)
                {
                    return(EitherData.Right <L, OptionUnsafe <B> >(None));
                }
                var da = await ma.Value.Data;

                if (da.State == EitherStatus.IsBottom)
                {
                    return(EitherData <L, OptionUnsafe <B> > .Bottom);
                }
                if (da.State == EitherStatus.IsLeft)
                {
                    return(EitherData.Left <L, OptionUnsafe <B> >(da.Left));
                }
                return(EitherData.Right <L, OptionUnsafe <B> >(f(da.Right)));
            }
        }
Example #16
0
        public static EitherAsync <L, Eff <B> > Traverse <L, A, B>(this Eff <EitherAsync <L, A> > ma, Func <A, B> f)
        {
            try
            {
                return(new EitherAsync <L, Eff <B> >(Go(ma, f)));

                async Task <EitherData <L, Eff <B> > > Go(Eff <EitherAsync <L, A> > ma, Func <A, B> f)
                {
                    var ra = ma.Run();

                    if (ra.IsBottom)
                    {
                        return(EitherData <L, Eff <B> > .Bottom);
                    }
                    if (ra.IsFail)
                    {
                        return(EitherData.Right <L, Eff <B> >(FailEff <B>(ra.Error)));
                    }
                    var da = await ra.Value.Data.ConfigureAwait(false);

                    if (da.State == EitherStatus.IsBottom)
                    {
                        return(EitherData <L, Eff <B> > .Bottom);
                    }
                    if (da.State == EitherStatus.IsLeft)
                    {
                        return(EitherData.Left <L, Eff <B> >(da.Left));
                    }
                    return(EitherData.Right <L, Eff <B> >(SuccessEff <B>(f(da.Right))));
                }
            }
            catch (Exception e)
            {
                return(FailEff <B>(e));
            }
        }
Example #17
0
        public static EitherAsync <L, Validation <MonoidFail, Fail, B> > Traverse <MonoidFail, Fail, L, A, B>(this Validation <MonoidFail, Fail, EitherAsync <L, A> > ma, Func <A, B> f)
            where MonoidFail : struct, Monoid <Fail>, Eq <Fail>
        {
            return(new EitherAsync <L, Validation <MonoidFail, Fail, B> >(Go(ma, f)));

            async Task <EitherData <L, Validation <MonoidFail, Fail, B> > > Go(Validation <MonoidFail, Fail, EitherAsync <L, A> > ma, Func <A, B> f)
            {
                if (ma.IsFail)
                {
                    return(EitherData.Right <L, Validation <MonoidFail, Fail, B> >(Fail <MonoidFail, Fail, B>(ma.FailValue)));
                }
                var da = await ma.SuccessValue.Data;

                if (da.State == EitherStatus.IsBottom)
                {
                    return(EitherData <L, Validation <MonoidFail, Fail, B> > .Bottom);
                }
                if (da.State == EitherStatus.IsLeft)
                {
                    return(EitherData.Left <L, Validation <MonoidFail, Fail, B> >(da.Left));
                }
                return(EitherData.Right <L, Validation <MonoidFail, Fail, B> >(f(da.Right)));
            }
        }
Example #18
0
 protected override void Add(EitherData <TLeft, TRight>[] collection, int index, EitherData <TLeft, TRight> value, MessagePackSerializerOptions options)
 {
     collection[index] = value;
 }