示例#1
0
 public static RWS <MonoidW, R, W, S, bool> Exists <MonoidW, R, W, S, A>(this RWS <MonoidW, R, W, S, A> self, Func <A, bool> pred)
     where MonoidW : struct, Monoid <W> => (env, state) =>
 {
     var res = self(env, state);
     return(res.IsFaulted
             ? RWSResult <MonoidW, R, W, S, bool> .New(res.Output, state, false)
             : RWSResult <MonoidW, R, W, S, bool> .New(res.Output, res.State, pred(res.Value)));
 };
示例#2
0
 public static RWS <MonoidW, R, W, S, int> Count <MonoidW, R, W, S, A>(this RWS <MonoidW, R, W, S, A> self)
     where MonoidW : struct, Monoid <W> => (env, state) =>
 {
     var res = self(env, state);
     return(res.IsFaulted
             ? RWSResult <MonoidW, R, W, S, int> .New(res.Output, state, res.Error)
             : RWSResult <MonoidW, R, W, S, int> .New(res.Output, res.State, 1));
 };
示例#3
0
 public static RWS <MonoidW, R, W, S, R> Fold <MonoidW, R, W, S, A>(this RWS <MonoidW, R, W, S, A> self, Func <R, A, R> f)
     where MonoidW : struct, Monoid <W> => (env, state) =>
 {
     var res = self(env, state);
     return(res.IsFaulted
             ? RWSResult <MonoidW, R, W, S, R> .New(res.Output, state, res.Error)
             : RWSResult <MonoidW, R, W, S, R> .New(res.Output, res.State, f(env, res.Value)));
 };
示例#4
0
 /// <summary>
 /// Runs the RWS monad and memoizes the result in a TryOption monad.  Use
 /// Match, IfSucc, IfNone, etc to extract.
 /// </summary>
 public static RWSResult <MonoidW, R, W, S, A> Run <MonoidW, R, W, S, A>(this RWS <MonoidW, R, W, S, A> self, R env, S state)
     where MonoidW : struct, Monoid <W>
 {
     if (self == null)
     {
         throw new ArgumentNullException(nameof(self));
     }
     if (state == null)
     {
         throw new ArgumentNullException(nameof(state));
     }
     try
     {
         return(self(env, state));
     }
     catch (Exception e)
     {
         return(RWSResult <MonoidW, R, W, S, A> .New(state, Error.New(e)));
     }
 }
示例#5
0
 public RWS <MonoidW, R, W, S, R> Ask() => (env, state) =>
 RWSResult <MonoidW, R, W, S, R> .New(default(MonoidW).Empty(), state, env);
示例#6
0
 public static RWS <MonoidW, R, W, S, Unit> Modify <MonoidW, R, W, S, A>(RWS <MonoidW, R, W, S, A> self, Func <S, S> f)
     where MonoidW : struct, Monoid <W> => (env, state) =>
 RWSResult <MonoidW, R, W, S, Unit> .New(default(MonoidW).Empty(), f(state), unit);