Пример #1
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("Task #4: implement monad in a nonfunctional language");

            // perform 1 * 2 + 30
            var monadId = IdentityMonad <int> .Return(1).BindM(x =>
                                                               IdentityMonad <int> .Return(x * 2).BindM(y =>
                                                                                                        IdentityMonad <int> .Return(y + 30))
                                                               );

            System.Console.WriteLine("IdentityMonad test result: {0}", monadId.Value);

            System.Console.WriteLine("MaybeMonad test:");
            // the container will be broken because: 10 - 8 - 3 < 0
            //     despite 10 - 8 - 3 + 3 > 0
            var container = new Container(10);

            new int [] { 10, -8, -3, 3 }.Aggregate(container,
                                                   (c, x) => {
                c.change(x);
                System.Console.WriteLine(c);
                return(c);
            });

            // nothing wrong in the following example
            container = new Container(10);
            new int [] { 10, -8, 3, -3 }.Aggregate(container,
                                                   (c, x) => {
                c.change(x);
                System.Console.WriteLine(c);
                return(c);
            });
        }
Пример #2
0
        public IdentityMonad <TTo> BindM <TTo>(Func <T, IdentityMonad <TTo> > f)
        {
            var mNew = f(Value) as IdentityMonad <TTo>;

            return(IdentityMonad <TTo> .Return(mNew.Value));
        }