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))));
            }
        }
Exemple #2
0
        public static TryOptionAsync <Aff <B> > Traverse <A, B>(this Aff <TryOptionAsync <A> > ma, Func <A, B> f)
        {
            return(ToTry(() => Go(ma, f)));

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

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

                if (rb.IsFaulted)
                {
                    return(new OptionalResult <Aff <B> >(rb.Exception));
                }
                if (rb.IsNone)
                {
                    return(OptionalResult <Aff <B> > .None);
                }
                return(new OptionalResult <Aff <B> >(SuccessAff <B>(f(rb.Value.Value))));
            }
        }
Exemple #3
0
 /// <summary>
 /// Create a new cancellation context and run the provided Aff in that context
 /// </summary>
 /// <param name="ma">Operation to run in the next context</param>
 /// <typeparam name="RT">Runtime environment</typeparam>
 /// <typeparam name="A">Bound value type</typeparam>
 /// <returns>An asynchronous effect that captures the operation running in context</returns>
 public static Aff <RT, A> localCancel <RT, A>(Aff <RT, A> ma) where RT : struct, HasCancel <RT> =>
 AffMaybe <RT, A>(async rt =>
 {
     var rt1 = rt.LocalCancel;
     using (rt1.CancellationTokenSource)
     {
         return(await ma.ReRun(rt1).ConfigureAwait(false));
     }
 });
Exemple #4
0
        public static async ValueTask <Aff <B> > Traverse <A, B>(this Aff <ValueTask <A> > ma, Func <A, B> f)
        {
            var da = await ma.Run().ConfigureAwait(false);

            if (da.IsBottom)
            {
                throw new BottomException();
            }
            if (da.IsFail)
            {
                return(FailAff <B>(da.Error));
            }
            var a = await da.Value.ConfigureAwait(false);

            return(SuccessAff(f(a)));
        }
 /// <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>(Aff <H> Acq, Func <H, Eff <R> > Use) where H : IDisposable =>
 AffMaybe(async() =>
 {
     var h = await Acq.ReRun().ConfigureAwait(false);
     if (h.IsFail)
     {
         return(h.Cast <R>());
     }
     try
     {
         return(Use(h.Value).Run());
     }
     finally
     {
         h.Value?.Dispose();
     }
 });
 /// <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>(Aff <H> Acq, Func <H, Eff <RT, R> > Use)
     where RT : struct, HasCancel <RT>
     where H : IDisposable =>
 AffMaybe <RT, R>(async env =>
 {
     var h = await Acq.ReRun().ConfigureAwait(false);
     if (h.IsFail)
     {
         return(h.Cast <R>());
     }
     try
     {
         return(Use(h.Value).Run(env));
     }
     finally
     {
         h.Value?.Dispose();
     }
 });
 /// <summary>
 /// Catch an error if the error `Code` matches the `errorCode` argument provided
 /// </summary>
 public static AffCatch <RT, A> @catch <RT, A>(int errorCode, Aff <RT, A> Fail) where RT : struct, HasCancel <RT> =>
 matchError(e => e.Code == errorCode, _ => Fail);
 /// <summary>
 /// Catch an error if the error message matches the `errorText` argument provided
 /// </summary>
 public static AffCatch <RT, A> @catch <RT, A>(string errorText, Aff <RT, A> Fail) where RT : struct, HasCancel <RT> =>
 matchError(e => e.Message == errorText, _ => Fail);
 /// <summary>
 /// Catch an error if the error matches the argument provided
 /// </summary>
 public static AffCatch <RT, A> @catch <RT, A>(Error error, Aff <RT, A> Fail) where RT : struct, HasCancel <RT> =>
 matchError(e => e == error, _ => Fail);
 /// <summary>
 /// Catch an error if it's of a specific exception type
 /// </summary>
 public static AffCatch <RT, A> @catch <RT, A>(Func <Exception, bool> predicate, Aff <RT, A> Fail) where RT : struct, HasCancel <RT> =>
 matchError(e => e.Exception.Map(predicate).IfNone(false), e => Fail);
Exemple #11
0
 /// <summary>
 /// Catch an error if the error `Code` matches the `errorCode` argument provided
 /// </summary>
 public static Aff <A> Catch <A>(this Aff <A> ma, int errorCode, Func <Error, Aff <A> > Fail) =>
 ma | matchError(e => e.Code == errorCode, Fail);
Exemple #12
0
 /// <summary>
 /// Catch an error if the error matches the argument provided
 /// </summary>
 public static Aff <A> Catch <A>(this Aff <A> ma, Error error, Aff <A> Fail) =>
 ma | matchError(e => e == error, _ => Fail);
 /// <summary>
 /// Catch an error if the error matches the argument provided
 /// </summary>
 public static Aff <A> Catch <A>(this Aff <A> ma, Error error, Func <Error, Aff <A> > Fail) =>
 ma | matchError(e => e.Is(error), Fail);
 /// <summary>
 /// Catch an error if the error matches the argument provided
 /// </summary>
 public static Aff <RT, A> Catch <RT, A>(this Aff <RT, A> ma, Error error, Func <Error, Aff <A> > Fail) where RT : struct, HasCancel <RT> =>
 ma | matchError(e => e.Is(error), Fail);
Exemple #15
0
 public static TryAsync <Aff <B> > Sequence <A, B>(this Aff <A> ta, Func <A, TryAsync <B> > f) =>
 ta.Map(f).Traverse(Prelude.identity);
Exemple #16
0
 /// <summary>
 /// Catch all errors
 /// </summary>
 public static Aff <RT, A> Catch <RT, A>(this Aff <RT, A> ma, Aff <RT, A> Fail) where RT : struct, HasCancel <RT> =>
 ma | matchError(static _ => true, e => Fail);
Exemple #17
0
 /// <summary>
 /// Catch an error if the error matches the argument provided
 /// </summary>
 public static Aff <RT, A> Catch <RT, A>(this Aff <RT, A> ma, Error error, Aff <A> Fail) where RT : struct, HasCancel <RT> =>
 ma | matchError(e => e == error, _ => Fail);
Exemple #18
0
 public static Aff <OuterRT, A> localAff <OuterRT, InnerRT, A>(Func <OuterRT, InnerRT> f, Aff <InnerRT, A> ma)
     where OuterRT : struct, HasCancel <OuterRT>
     where InnerRT : struct, HasCancel <InnerRT> =>
Exemple #19
0
 /// <summary>
 /// Catch an error if the error `Code` matches the `errorCode` argument provided
 /// </summary>
 public static Aff <RT, A> Catch <RT, A>(this Aff <RT, A> ma, int errorCode, Func <Error, Aff <A> > Fail) where RT : struct, HasCancel <RT> =>
 ma | matchError(e => e.Code == errorCode, Fail);
Exemple #20
0
 public static TryAsync <Aff <A> > Sequence <A>(this Aff <TryAsync <A> > ta) =>
 ta.Traverse(identity);
 /// <summary>
 /// Catch an error if it's of a specific exception type
 /// </summary>
 public static AffCatch <A> @catch <A>(Func <Exception, bool> predicate, Aff <A> Fail) =>
 matchError(e => e.Exception.Map(predicate).IfNone(false), e => Fail);
 public static EitherAsync <L, Aff <B> > Sequence <L, A, B>(this Aff <A> ta, Func <A, EitherAsync <L, B> > f) =>
 ta.Map(f).Traverse(Prelude.identity);
 /// <summary>
 /// Catch all errors
 /// </summary>
 public static AffCatch <RT, A> @catch <RT, A>(Aff <RT, A> Fail) where RT : struct, HasCancel <RT> =>
 matchError(static _ => true, e => Fail);
Exemple #24
0
 /// <summary>
 /// Catch an error if the error message matches the `errorText` argument provided
 /// </summary>
 public static Aff <RT, A> Catch <RT, A>(this Aff <RT, A> ma, string errorText, Func <Error, Aff <A> > Fail) where RT : struct, HasCancel <RT> =>
 ma | matchError(e => e.Message == errorText, Fail);
 /// <summary>
 /// Catch an error if the error matches the argument provided
 /// </summary>
 public static AffCatch <A> @catch <A>(Error error, Aff <A> Fail) =>
 matchError(e => e == error, _ => Fail);
Exemple #26
0
 /// <summary>
 /// Catch an error if the error message matches the `errorText` argument provided
 /// </summary>
 public static Aff <A> Catch <A>(this Aff <A> ma, string errorText, Func <Error, Aff <A> > Fail) =>
 ma | matchError(e => e.Message == errorText, Fail);
 /// <summary>
 /// Catch an error if the error message matches the `errorText` argument provided
 /// </summary>
 public static AffCatch <A> @catch <A>(string errorText, Aff <A> Fail) =>
 matchError(e => e.Message == errorText, _ => Fail);
Exemple #28
0
 /// <summary>
 /// Catch an error if it's of a specific exception type
 /// </summary>
 public static Aff <A> Catch <A>(this Aff <A> ma, Func <Exception, bool> predicate, Func <Exception, Aff <A> > Fail) =>
 ma | matchError(e => e.Exception.Map(predicate).IfNone(false), e => Fail(e));
 /// <summary>
 /// Catch an error if the error `Code` matches the `errorCode` argument provided
 /// </summary>
 public static AffCatch <A> @catch <A>(int errorCode, Aff <A> Fail) =>
 matchError(e => e.Code == errorCode, _ => Fail);
Exemple #30
0
 /// <summary>
 /// Catch an error if it's of a specific exception type
 /// </summary>
 public static Aff <RT, A> Catch <RT, A>(this Aff <A> ma, Func <Exception, bool> predicate, Func <Exception, Aff <RT, A> > Fail) where RT : struct, HasCancel <RT> =>
 ma | matchError(e => e.Exception.Map(predicate).IfNone(false), e => Fail(e));