public void TestTraverseIo()
        {
            var xs     = Enumerable.Range(1, 5).ToArray();
            var result = xs.Traverse(x => from _1 in Io.Apply(() => Console.WriteLine(x)) select x).UnsafePerformIo();

            Assert.AreEqual(xs, result);
        }
 private static StateIo <int, Unit> Count(int n)
 {
     return
         (from _1 in State.Mod <int>(i => i + 1).ToStateIo()
          from _2 in Io.Apply(() => Console.WriteLine(n)).ToStateIo <int, Unit>()
          select Unit.Only);
 }
        public void TestTraverse()
        {
            var expected = new[] { 1, 2, 3 };
            var actual   = expected.Traverse(n => Io.Apply(() => n)).UnsafePerformIo();

            Assert.AreEqual(expected, actual);
        }
 public IoTryMaybe <TResult> Bind <TResult>(Func <T, IoTryMaybe <TResult> > f)
 {
     return(new IoTryMaybe <TResult>(Out.SelectMany(t => t.Match(
                                                        success: m => m.Match(
                                                            just: v => f(v).Out,
                                                            nothing: () => Io.Apply(() => Try.Attempt(() => Maybe.Nothing <TResult>()))),
                                                        failure: ex => Io.Apply(() => ex.Fail <IMaybe <TResult> >())))));
 }
示例#5
0
 public IoStateTry <TState, TResult> Bind <TResult>(Func <TValue, IoStateTry <TState, TResult> > f)
 {
     return(new IoStateTry <TState, TResult>(Out.SelectMany(state => Io.Apply(() => new State <TState, Try <TResult> >(s =>
     {
         var result = state.Run(s);
         return result.Item2.Match(
             success: val => f(val).Out.UnsafePerformIo().Run(result.Item1),
             failure: ex => Tuple.Create(result.Item1, ex.Fail <TResult>()));
     })))));
 }
示例#6
0
        public void TestEffectualLoop(IEnumerable <int> inputSequence, IEnumerable <int> expectedResult,
                                      int expectedEffectualResult)
        {
            var actualEffectualResult = 0;
            var actual =
                (from i in inputSequence.ToIoEnumerable()
                 from _ in Io.Apply(() => actualEffectualResult += 1).ToIoEnumerable()
                 select i + 1).Out.UnsafePerformIo();

            Assert.AreEqual(expectedEffectualResult, actualEffectualResult);
            Assert.AreEqual(expectedResult, actual);
        }
示例#7
0
        /// <summary>
        /// Functional implementation of the 'using' keyword from C#.
        /// </summary>
        /// <param name="source">An 'IDisposable' that will be disposed after body is invoked</param>
        /// <param name="body">Statements to execute before disposing of source</param>
        /// <returns>A value that represents an effectual computation that is only useful for its side-effects</returns>
        public static Io <Unit> Using <T>(Func <T> source, Action <T> body) where T : IDisposable
        {
            return(Io.Apply(() =>
            {
                using (var resource = source())
                {
                    body(resource);
                }

                return Unit.Only;
            }));
        }
示例#8
0
        public void TestIt()
        {
            var pgm =
                from n in State.Get <int, int>(BasicFunctions.Identity).ToStateIo()
                from _1 in Io.Apply(() => Console.WriteLine("Before modification {0}", n)).ToStateIo <int, Unit>()
                from _2 in (n + 1).Put().ToStateIo()
                from o in State.Get <int, int>(BasicFunctions.Identity).ToStateIo()
                from _3 in Io.Apply(() => Console.WriteLine("After modifications {0}", o)).ToStateIo <int, Unit>()
                select _3;

            pgm.Out.Eval(5).UnsafePerformIo();
        }
        public void TestTraverseMaybe(int[] testData, bool isSome, int[] expected)
        {
            Func <int, bool> greaterThanFour = i => i > 4;
            var keepGreaterThanFour          = FuncExtensions.Curry <Func <int, bool>, int, IMaybe <int> >(KeepIf)(greaterThanFour);
            var result = testData.Traverse(keepGreaterThanFour);

            Assert.AreEqual(isSome, !result.IsEmpty);
            if (isSome)
            {
                result.Match(
                    just: actual => Io.Apply(() => Assert.AreEqual(expected, actual)),
                    nothing: () => Io.Apply(() => Assert.Fail("Expected to get a result for input {0} but got nothing instead", testData))).UnsafePerformIo();
            }
        }
示例#10
0
        public void Test1()
        {
            var xs = Io.Apply(() => Enumerable.Range(0, 10000).Select(n => n % 8).Select(n => Try.Attempt(() =>
            {
                if (n == 0)
                {
                    throw new Exception("Ruh roh");
                }
                return(n);
            }))).ToIoEnumerableTry();

            var pgm = (from x in xs
                       from _ in PutStrLn(x).ToIoEnumerableTry()
                       select x + 3).Out;

            var res = pgm.UnsafePerformIo();
        }
        public void TestComposition()
        {
            var p1 = Io.Apply(() => Try.Pure(1)).ToIoTry();
            var p2 = Io.Apply(() => Try.Pure(2)).ToIoTry();
            var p3 = Io.Apply(() => Try.Pure(2)).ToIoTry();

            var program =
                from a in p1
                from b in p2
                from c in p3
                select a + b + c;

            var result = program.Out.UnsafePerformIo().Match(
                success: BasicFunctions.Identity,
                failure: ex => - 1);

            Assert.AreEqual(6, result);
        }
示例#12
0
        public void BasicSequenceTest()
        {
            var xs             = new[] { 1, 2, 3, 4, 5 };
            var expectedResult = new[] { 2, 3, 4, 5, 6 };
            var expectedState  = 5;

            var program =
                from x in xs.ToIoStateEnumerable <Tuple <int, int>, int>()
                from _1 in State.Mod <Tuple <int, int> >(pair => Tuple.Create(pair.Item1 + 1, pair.Item2)).ToIoStateEnumerable()
                where x > 3
                from _2 in State.Mod <Tuple <int, int> >(pair => Tuple.Create(pair.Item1, pair.Item2 + 1)).ToIoStateEnumerable()
                from _3 in Io.Apply(() => Console.WriteLine(x)).ToIoStateEnumerable <Tuple <int, int>, Unit>()
                select x + 1;

            var result = program.Out().UnsafePerformIo().Run(Tuple.Create(0, 0));

            Assert.AreEqual(expectedState, result.Item1);
            Assert.AreEqual(expectedResult, result.Item2);
        }
 public static Io <Unit> ErrorFormatIo(this ILog logger, string formatString, params object[] args)
 {
     return(Io.Apply(() => logger.ErrorFormat(formatString, args)));
 }
 public static Io <Unit> ErrorIo(this ILog logger, Exception ex)
 {
     return(Io.Apply(() => logger.Error(ex)));
 }
 public static Io <Unit> ErrorIo(this ILog logger, string msg)
 {
     return(Io.Apply(() => logger.Error(msg)));
 }
 public static Io <Unit> WarnIo(this ILog logger, Exception ex)
 {
     return(Io.Apply(() => logger.Warn(ex)));
 }
 public static Io <Unit> WarnIo(this ILog logger, string msg)
 {
     return(Io.Apply(() => logger.Warn(msg)));
 }
示例#18
0
 public static Io <Unit> PutStrLn(object s)
 {
     return(Io.Apply(() => Console.WriteLine(s)));
 }
 public IoTryMaybe(T t)
     : this(Io.Apply(() => Try.Attempt(() => t.ToMaybe())))
 {
 }
示例#20
0
 public IoEnumerableMaybe(IEnumerable <IMaybe <T> > maybes) : this(Io.Apply(() => maybes))
 {
 }
 public static Io <Unit> FatalIo(this ILog logger, Exception ex)
 {
     return(Io.Apply(() => logger.Fatal(ex)));
 }
 public static Io <Unit> FatalIo(this ILog logger, string msg)
 {
     return(Io.Apply(() => logger.Fatal(msg)));
 }
 public IoEnumerable(IEnumerable <T> ts) : this(Io.Apply(() => ts))
 {
 }
 public IoTryMaybe(Try <IMaybe <T> > aTry)
     : this(Io.Apply(() => aTry))
 {
 }
 public IoTryMaybe(IMaybe <T> m)
     : this(Io.Apply(() => Try.Attempt(() => m)))
 {
 }
 public static Io <Unit> InfoIo(this ILog logger, Exception ex)
 {
     return(Io.Apply(() => logger.Info(ex)));
 }
 public static Io <Unit> DebugIo(this ILog logger, string msg)
 {
     return(Io.Apply(() => logger.Debug(msg)));
 }
 public static Io <Unit> InfoIo(this ILog logger, string msg)
 {
     return(Io.Apply(() => logger.Info(msg)));
 }
示例#29
0
 public IoEnumerableMaybe <TResult> Bind <TResult>(Func <T, IoEnumerableMaybe <TResult> > f)
 {
     return(new IoEnumerableMaybe <TResult>(Out.SelectMany(maybes => maybes.Select(maybe => maybe.Match(
                                                                                       just: v => f(v).Out,
                                                                                       nothing: () => Io.Apply(() => Enumerable.Empty <IMaybe <TResult> >())))
                                                           .Sequence()
                                                           .Select(x => x.SelectMany(BasicFunctions.Identity)))));
 }
 public static Io <Unit> DebugIo(this ILog logger, Exception ex)
 {
     return(Io.Apply(() => logger.Debug(ex)));
 }