Example #1
0
 public static State <S, Reader <E, V> > SelectMany <S, E, T, U, V>(
     this State <S, T> self,
     Func <T, Reader <E, U> > bind,
     Func <T, U, V> project
     )
 {
     if (bind == null)
     {
         throw new ArgumentNullException("bind");
     }
     if (project == null)
     {
         throw new ArgumentNullException("project");
     }
     return((S s) =>
     {
         var resT = self(s);
         if (resT.IsBottom)
         {
             return StateResult.Bottom <S, Reader <E, V> >(s);
         }
         return StateResult.Return <S, Reader <E, V> >(resT.State, envInner =>
         {
             var resU = bind(resT.Value)(envInner);
             if (resU.IsBottom)
             {
                 return new ReaderResult <V>(default(V), true);
             }
             return ReaderResult.Return(project(resT.Value, resU.Value));
         });
     });
 }
Example #2
0
 public static Writer <Out, Reader <E, V> > SelectMany <Out, E, T, U, V>(
     this Writer <Out, T> self,
     Func <T, Reader <E, U> > bind,
     Func <T, U, V> project
     )
 {
     if (bind == null)
     {
         throw new ArgumentNullException("bind");
     }
     if (project == null)
     {
         throw new ArgumentNullException("project");
     }
     return(() =>
     {
         var resT = self();
         if (resT.IsBottom)
         {
             return WriterResult.Bottom <Out, Reader <E, V> >(resT.Output);
         }
         return WriterResult.Return <Out, Reader <E, V> >(env =>
         {
             var resU = bind(resT.Value)(env);
             if (resU.IsBottom)
             {
                 return ReaderResult.Bottom <V>();
             }
             return ReaderResult.Return(project(resT.Value, resU.Value));
         }, resT.Output);
     });
 }
Example #3
0
 public static Reader <Env, A> tryread <Env, A>(Reader <Env, A> m) =>
 state =>
 {
     try
     {
         return(m(state));
     }
     catch (Exception e)
     {
         return(ReaderResult <A> .New(Common.Error.New(e)));
     }
 };
Example #4
0
 private static ReaderResult <Unit> bmap <T>(ReaderResult <T> r, Action <T> f)
 {
     if (r.IsBottom)
     {
         return(Bottom <Unit>());
     }
     else
     {
         f(r.Value);
         return(Return(unit));
     }
 }
Example #5
0
 internal static Reader <Env, List <B> > TraverseFast <Env, A, B>(this IEnumerable <Reader <Env, A> > ma, Func <A, B> f) => env =>
 {
     var values = new List <B>();
     foreach (var item in ma)
     {
         var resA = item(env);
         if (resA.IsFaulted)
         {
             return(ReaderResult <List <B> > .New(resA.ErrorInt));
         }
         values.Add(f(resA.Value));
     }
     return(ReaderResult <List <B> > .New(values));
 };
Example #6
0
 internal static Reader <Env, List <A> > SequenceFast <Env, A>(this IEnumerable <Reader <Env, A> > ta) => env =>
 {
     var values = new List <A>();
     foreach (var item in ta)
     {
         var resA = item(env);
         if (resA.IsFaulted)
         {
             return(ReaderResult <List <A> > .New(resA.ErrorInt));
         }
         values.Add(resA.Value);
     }
     return(ReaderResult <List <A> > .New(values));
 };
Example #7
0
 public static Reader <Env, A> ReaderFail <Env, A>(Exception exception) => env =>
 ReaderResult <A> .New(Common.Error.New(exception));
Example #8
0
 public static Reader <Env, A> ReaderFail <Env, A>(int code, string error) => env =>
 ReaderResult <A> .New(Common.Error.New(code, error));
Example #9
0
 private static ReaderResult <R> bmap <T, R>(ReaderResult <T> r, Func <T, R> f) =>
 r.IsBottom
         ? Bottom <R>()
         : Return(f(r.Value));
Example #10
0
 private static ReaderResult <T> Bottom <T>() =>
 ReaderResult.Bottom <T>();
Example #11
0
 private static ReaderResult <T> Return <T>(T value) =>
 ReaderResult.Return(value);