public void ReaderWriterStateTest1()
        {
            var world = RWS.Return <Env, string, App, int>(0);

            var rws = (from _ in world
                       from app in RWS.Get <Env, string, App>()
                       from env in RWS.Ask <Env, string, App>()
                       from x in Value(app.UsersLoggedIn, "Users logged in: " + app.UsersLoggedIn)
                       from y in Value(100, "System folder: " + env.SystemFolder)
                       from s in RWS.Put <Env, string, App>(new App {
                UsersLoggedIn = 35
            })
                       from t in RWS.Tell <Env, string, App>("Process complete")
                       select x *y)
                      .Memo(new Env(), new App());

            var res = rws();

            Assert.True(res.Value == 3400);
            Assert.True(res.State.UsersLoggedIn == 35);
            Assert.True(res.Output.Count() == 3);
            Assert.True(res.Output.First() == "Users logged in: 34");
            Assert.True(res.Output.Skip(1).First() == "System folder: C:/Temp");
            Assert.True(res.Output.Skip(2).First() == "Process complete");
        }
Exemple #2
0
    // RWSMonad usage example 1 : Realize the contents performed in each Example 1 of WriterMonad, ReaderMonad, and StateMonad with one RWSMonad
    public void Example1()
    {
        var checkCount = from currentState in RWS.Get <DateTime, string, SampleState>()
                         from date in RWS.Ask <DateTime, string, SampleState>()
                         from _ in RWS.Tell <DateTime, string, SampleState>(date.ToString("yyyy/MM/dd HH:mm:ss"))
                         from greeting in RWS.Tell <DateTime, string, SampleState, string>(currentState.Count == 0 ? "Nice to meet you" : "Hello", string.Format("currentState.Count = {0}", currentState.Count))
                         from comment in RWS.Return <DateTime, string, SampleState, string>(greeting + ", Monad world!")
                         from __ in RWS.Tell <DateTime, string, SampleState>("Add count")
                         from putAddCount in RWS.Put <DateTime, string, SampleState>(new SampleState(currentState.Count + 1))
                         select comment;

        var result1State = checkCount.Execute(
            new DateTime(2000, 1, 1, 10, 20, 30),
            defaultState,
            value => Debug.Log(value),
            outPut => {
            foreach (var log in outPut)
            {
                Debug.Log(string.Format("Log[{0}]", log));
            }
        });

        var result2State = checkCount.Execute(
            DateTime.UtcNow,
            result1State,
            value => Debug.Log(value),
            outPut => {
            foreach (var log in outPut)
            {
                Debug.Log(string.Format("Log[{0}]", log));
            }
        });

        Debug.Log(string.Format("It has been called {0} times", result2State.Count));
    }