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)); }); }); }
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); }); }
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))); } };
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)); } }
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)); };
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)); };
public static Reader <Env, A> ReaderFail <Env, A>(Exception exception) => env => ReaderResult <A> .New(Common.Error.New(exception));
public static Reader <Env, A> ReaderFail <Env, A>(int code, string error) => env => ReaderResult <A> .New(Common.Error.New(code, error));
private static ReaderResult <R> bmap <T, R>(ReaderResult <T> r, Func <T, R> f) => r.IsBottom ? Bottom <R>() : Return(f(r.Value));
private static ReaderResult <T> Bottom <T>() => ReaderResult.Bottom <T>();
private static ReaderResult <T> Return <T>(T value) => ReaderResult.Return(value);