Example #1
0
        public static TryOptionAsync <Eff <B> > Traverse <A, B>(this Eff <TryOptionAsync <A> > ma, Func <A, B> f)
        {
            return(ToTry(() => Go(ma, f)));

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

                if (ra.IsFail)
                {
                    return(new OptionalResult <Eff <B> >(FailEff <B>(ra.Error)));
                }
                var rb = await ra.Value.Try().ConfigureAwait(false);

                if (rb.IsFaulted)
                {
                    return(new OptionalResult <Eff <B> >(rb.Exception));
                }
                if (rb.IsNone)
                {
                    return(OptionalResult <Eff <B> > .None);
                }
                return(OptionalResult <Eff <B> > .Some(SuccessEff <B>(f(rb.Value.Value))));
            }
        }
Example #2
0
 public static TryOption <Eff <B> > Traverse <A, B>(this Eff <TryOption <A> > ma, Func <A, B> f) => () =>
 {
     var mb = ma.Run();
     if (mb.IsBottom)
     {
         return(OptionalResult <Eff <B> > .None);
     }
     if (mb.IsFail)
     {
         return(new OptionalResult <Eff <B> >(FailEff <B>(mb.Error)));
     }
     var mr = mb.Value.Try();
     if (mr.IsBottom)
     {
         return(OptionalResult <Eff <B> > .None);
     }
     if (mr.IsFaulted)
     {
         return(new OptionalResult <Eff <B> >(mr.Exception));
     }
     if (mr.IsNone)
     {
         return(OptionalResult <Eff <B> > .None);
     }
     return(new OptionalResult <Eff <B> >(SuccessEff <B>(f(mr.Value.Value))));
 };
Example #3
0
        public static async ValueTask <Eff <B> > Traverse <A, B>(this Eff <ValueTask <A> > ma, Func <A, B> f)
        {
            var mr = ma.Run();

            if (mr.IsBottom)
            {
                return(FailEff <B>(BottomException.Default));
            }
            else if (mr.IsFail)
            {
                return(FailEff <B>(mr.Error));
            }
            return(SuccessEff <B>(f(await mr.Value.ConfigureAwait(false))));
        }
        public static Validation <Fail, Eff <B> > Traverse <Fail, A, B>(this Eff <Validation <Fail, A> > ma, Func <A, B> f)
        {
            var tres = ma.Run();

            if (tres.IsBottom || tres.IsFail)
            {
                return(Validation <Fail, Eff <B> > .Success(FailEff <B>(tres.Error)));
            }
            else if (tres.Value.IsFail)
            {
                return(Validation <Fail, Eff <B> > .Fail((Seq <Fail>) tres.Value));
            }
            else
            {
                return(Validation <Fail, Eff <B> > .Success(SuccessEff(f((A)tres.Value))));
            }
        }
Example #5
0
 /// <summary>
 /// Safely use a disposable resource
 /// </summary>
 /// <param name="Acq">Acquire resource</param>
 /// <param name="Use">Use resource</param>
 public static Eff <R> use <H, R>(Eff <H> Acq, Func <H, Eff <R> > Use) where H : IDisposable =>
 EffMaybe(() =>
 {
     var h = Acq.Run();
     if (h.IsFail)
     {
         return(h.Cast <R>());
     }
     try
     {
         return(Use(h.Value).Run());
     }
     finally
     {
         h.Value?.Dispose();
     }
 });
Example #6
0
 /// <summary>
 /// Safely use a disposable resource
 /// </summary>
 /// <param name="Acq">Acquire resource</param>
 /// <param name="Use">Use resource</param>
 public static Aff <R> use <H, R>(Eff <H> Acq, Func <H, Aff <R> > Use) where H : IDisposable =>
 AffMaybe(async() =>
 {
     var h = Acq.Run();
     if (h.IsFail)
     {
         return(h.Cast <R>());
     }
     try
     {
         return(await Use(h.Value).Run().ConfigureAwait(false));
     }
     finally
     {
         h.Value?.Dispose();
     }
 });
Example #7
0
        public static Validation <MonoidFail, Fail, Eff <B> > Traverse <MonoidFail, Fail, A, B>(this Eff <Validation <MonoidFail, Fail, A> > ma, Func <A, B> f)
            where MonoidFail : struct, Monoid <Fail>, Eq <Fail>
        {
            var tres = ma.Run();

            if (tres.IsBottom || tres.IsFail)
            {
                return(Validation <MonoidFail, Fail, Eff <B> > .Success(FailEff <B>(tres.Error)));
            }
            else if (tres.Value.IsFail)
            {
                return(Validation <MonoidFail, Fail, Eff <B> > .Fail(tres.Value.FailValue));
            }
            else
            {
                return(Validation <MonoidFail, Fail, Eff <B> > .Success(SuccessEff(f((A)tres.Value))));
            }
        }
Example #8
0
 /// <summary>
 /// Safely use a disposable resource
 /// </summary>
 /// <param name="Acq">Acquire resource</param>
 /// <param name="Use">Use resource</param>
 public static Aff <RT, R> use <RT, H, R>(Eff <H> Acq, Func <H, Aff <RT, R> > Use)
     where RT : struct, HasCancel <RT>
     where H : IDisposable =>
 AffMaybe <RT, R>(async env =>
 {
     var h = Acq.Run();
     if (h.IsFail)
     {
         return(h.Cast <R>());
     }
     try
     {
         return(await Use(h.Value).Run(env).ConfigureAwait(false));
     }
     finally
     {
         h.Value?.Dispose();
     }
 });
Example #9
0
        public static EitherUnsafe <L, Eff <B> > Traverse <L, A, B>(this Eff <EitherUnsafe <L, A> > ma, Func <A, B> f)
        {
            var tres = ma.Run();

            if (tres.IsBottom)
            {
                return(EitherUnsafe <L, Eff <B> > .Bottom);
            }
            else if (tres.IsFail)
            {
                return(RightUnsafe(FailEff <B>(tres.Error)));
            }
            else if (tres.Value.IsLeft)
            {
                return(EitherUnsafe <L, Eff <B> > .Left((L)tres.Value));
            }
            else
            {
                return(EitherUnsafe <L, Eff <B> > .Right(SuccessEff(f((A)tres.Value))));
            }
        }
Example #10
0
        public static Option <Eff <B> > Traverse <A, B>(this Eff <Option <A> > ma, Func <A, B> f)
        {
            var tres = ma.Run();

            if (tres.IsBottom)
            {
                return(None);
            }
            else if (tres.IsFail)
            {
                return(Some(FailEff <B>(tres.Error)));
            }
            else if (tres.Value.IsNone)
            {
                return(None);
            }
            else
            {
                return(Some(SuccessEff(f(tres.Value.Value))));
            }
        }
Example #11
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));
            }
        }