Beispiel #1
0
        public static void MaybePlayaround()
        {
            Console.Out.WriteLine("\n-------------------------------------------------------------");
            Console.Out.WriteLine("------------------------Maybe playground-----------------------");
            Console.Out.WriteLine("-------------------------------------------------------------\n");

            // Just 5, use implicit operator for *Maybe* to make a Just directly.
            Maybe <int> justInt = 5;

            Console.WriteLine("A new Just<double>: " + justInt.ToString());
            Maybe <int> nothingInt = 0;      // same as nothingInt = new Nothing<int>();

            Console.WriteLine("A new Nothing<double>: " + nothingInt.ToString());

            // justInt = 0; or justInt = new Nothing<int>() would make a Nothing out of the justInt

            Console.WriteLine("A new ListMonad<char>: ");
            var listMonadChar = new ListMonad <char>()
            {
                'a', 'b', 'c', 'd', 'e', 'f', 'g'
            }
            .Visit((x) => { Console.Out.Write(x + ", "); });

            Console.WriteLine("\n___________________________________________________________");

            Console.WriteLine("A new ListMonad<int>: ");
            var listMonadInt = new ListMonad <int>()
            {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            }
            .Visit((x) => { Console.Out.Write(x + ", "); });

            Console.WriteLine("\n___________________________________________________________");

            Console.WriteLine("A new ListMonad<double>: ");
            var listMonadDouble = new ListMonad <double>()
            {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            }
            .Visit((x) => { Console.Out.Write(x + ", "); });

            Console.WriteLine("\n___________________________________________________________");

            var intToDoubleFunction = new Func <int, double>(x => { return(x * 0.5); });

            Console.WriteLine("A new Just with a function inside: f(x) = x * 0.5 ");
            var justFunction = new Just <Func <int, double> >(intToDoubleFunction);

            Console.WriteLine(justFunction.ToString());
            Console.WriteLine("___________________________________________________________");
            Console.ReadLine();

            Console.WriteLine("Visits each combination of the Just 5 and the ListMonad<char>" +
                              "using a lambda and Console.Write inside: ");
            justInt.Visit((i, c) => { Console.Out.Write(i + "" + c + ", "); }, listMonadChar);
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();
            // Outputs: 1a, 1b, 1c, 1d, 1e,

            Console.WriteLine("Same with Nothing<int> will output nothing: ");
            nothingInt.Visit((i, c) => { Console.Out.Write(i + "" + c + ", "); }, listMonadChar);
            Console.WriteLine("___________________________________________________________");
            Console.ReadLine();

            Console.WriteLine("Visits each combination of the Just 5 and the ListMonad<int> \n" +
                              "using a lambda and Console.Write inside. Add both values in print out: ");
            justInt.Visit((x, y) => { Console.Out.Write(x + y + ", "); }, listMonadInt);

            Console.WriteLine("\nSame with Nothing<int>:");
            nothingInt = (Maybe <int>)nothingInt
                         .Visit((x, y) => { Console.Out.Write(x + y + ", "); }, listMonadInt);
            Console.WriteLine(nothingInt.ToString());
            Console.WriteLine("___________________________________________________________");
            Console.ReadLine();


            Console.Write("Fmap f(x) = x * 0.5 over the Just<int>(5): ");
            var justDouble = justInt.Fmap(intToDoubleFunction).Visit((x) => { Console.Out.Write(x + "\n"); });

            Console.WriteLine("___________________________________________________________");
            Console.ReadLine();

            Console.Write("App Just<Func>(f(x) = x * 0.5) over the Just<int>(5): ");
            justDouble = justInt.App(justFunction).Visit((x) => { Console.Out.Write(x + "\n"); });
            Console.WriteLine("___________________________________________________________");
            Console.ReadLine();

            Console.Write("App Just<Func> over the Just<int>(5), \n where the functions returns a new " +
                          "ListMonad<int>() \n with two times the value inside the Just 5. Output: ");
            var function     = new Just <Func <int, Monad <int> > >((x) => { return(new ListMonad <int>()
                {
                    x, x
                }); });
            var intListMonad = justInt.App(function).Visit((x) => { Console.Out.Write(x + ", "); });

            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();
            // The result is a ListMonad<int>
            // Output: 5, 5,

            Console.WriteLine("Create a new ListMonad with Func<int, int, double> (x*y, x/y, x%y) inside.");
            Console.WriteLine("Combinate Just 5 and that functions. Result is Just<int>.");
            Console.WriteLine("Only last value is returned because this Com function cannot break out of the Just.");
            Console.WriteLine();
            var functionListMonad = new ListMonad <Func <int, int, double> >();

            functionListMonad.Append((x, y) => { return(x * y); });
            functionListMonad.Append((x, y) => { return(x / (y == 0 ? 1 : y)); });
            functionListMonad.Append((x, y) => { return(x % (y == 0 ? 1 : y)); });
            functionListMonad.Visit((x) => { Console.Out.WriteLine("Func: " + x); });
            var result = justInt.Com(functionListMonad, listMonadInt).Visit((x) => { Console.Out.Write(x + ", "); });

            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();
            // Output: 5

            Console.WriteLine("Create a new ListMonad with \n" +
                              "Func<int, int, IMonad<double>> (x+y, x-y, x*y, x/y, x%y) inside.\n" +
                              "Where every function packs the result in a new ListMonad<double>.\n" +
                              "Combine the Just 5 and the ListMonad<double> with all the functions.\n" +
                              "The result ListMonad´s are flattned out, and only one result ListMonad<double> \n" +
                              " with all result values is returned: ");
            Console.WriteLine();
            var functionListMonadTwo = new ListMonad <Func <int, double, Monad <double> > >();

            functionListMonadTwo.Append((x, y) => { return(new ListMonad <double>()
                {
                    x + y
                }); });
            functionListMonadTwo.Append((x, y) => { return(new ListMonad <double>()
                {
                    x - y
                }); });
            functionListMonadTwo.Append((x, y) => { return(new ListMonad <double>()
                {
                    x *y
                }); });
            functionListMonadTwo.Append((x, y) => { return(new ListMonad <double>()
                {
                    x / (y == 0 ? 1 : y)
                }); });
            functionListMonadTwo.Append((x, y) => { return(new ListMonad <double>()
                {
                    x % (y == 0 ? 1 : y)
                }); });
            functionListMonadTwo.Append((x, y) => { return(new Nothing <double>()); });
            int counter   = 0;
            var resultTwo = justInt.Com(functionListMonadTwo, listMonadDouble)
                            .Visit((x) => {
                Console.Out.Write(x + ", ");
                counter++;
                if (counter % 10 == 0)
                {
                    Console.WriteLine("");
                }
            });

            // Output: 5*0, 5*1, 5*2,... 5*1, 5/1, 5/2, 5/3, ... 5%1, 5%1, 5%2, 5%3,....
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();

            Console.WriteLine("Do the same with the Nothing<int>: ");
            resultTwo = nothingInt.Com(functionListMonadTwo, listMonadDouble)
                        .Visit((x) => { Console.Out.Write(x + ", "); });
            // Output: 5*0, 5*1, 5*2,... 5*1, 5/1, 5/2, 5/3, ... 5%1, 5%1, 5%2, 5%3,....
            Console.WriteLine("___________________________________________________________");
            Console.ReadLine();

            Console.WriteLine("Combinate Just 5 and the ListMonad<int> with only one function ( f(x,y) = x+y ): ");
            var resultThree = justInt.Com((x, y) => { return(x + y); }, intListMonad)
                              .Visit((x) => { Console.Out.WriteLine(x); });

            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();

            Console.WriteLine("Maping a f(x, y) = x*y over the Just 5 and a new Just<int>(10) using LINQ: ");
            var query = from f in new Just <Func <int, int, int> >((x, y) => { return(x * y); })
                        from x in justInt
                        from y in new Just <int>(10)
                        select f(x, y);

            query.Visit((x) => { Console.Out.WriteLine(x + ", "); });
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();
        }
Beispiel #2
0
        public static void ListMonadPlayground()
        {
            Console.Out.WriteLine("\n-------------------------------------------------------------");
            Console.Out.WriteLine("------------------------ListMonad playground-------------------");
            Console.Out.WriteLine("-------------------------------------------------------------\n");

            Console.Out.WriteLine("Create two lists [1..5] and [J(1)..(J5)]: ");
            ListMonad <int> listMonadInt = new ListMonad <int>()
            {
                1, 2, 3, 4, 5
            };

            ListMonad <double> listMonadDouble = new ListMonad <double>()
            {
                1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0
            };

            // Because Maybe class has an implicit operator it can be written very cool and easy like a normal list.
            ListMonad <Maybe <int> > listMonadMaybeInt = new ListMonad <Maybe <int> >()
            {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            };

            // Functions for Fmap and first App function.
            Func <int, double> intDoubleFunc1 = (x) => { return(0.5 * x); };
            Func <int, double> intDoubleFunc2 = (x) => { return(0.7 * x); };

            Console.WriteLine("Fmap f(x) = 0.5 * x over [1,..5,]");
            int counter = 0;

            listMonadInt.Fmap(intDoubleFunc1).Visit((x) =>
            {
                Console.Out.Write(x + ", ");
                counter++;
                if (counter % 5 == 0)
                {
                    Console.WriteLine("");
                }
            });
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();

            Console.WriteLine("App [f(x)=0.5*x, f(x)=0.7*x] over [1,..,5]");
            var listMonadintDoubleFunc = new ListMonad <Func <int, double> >()
            {
                intDoubleFunc1, intDoubleFunc2
            };

            counter = 0;
            listMonadInt.App(listMonadintDoubleFunc).Visit((x) =>
            {
                Console.Out.Write(x + ", ");
                counter++;
                if (counter % 5 == 0)
                {
                    Console.WriteLine("");
                }
                if (counter % (5 * 5) == 0)
                {
                    Console.WriteLine("-----------------------------------------");
                }
            });
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();

            // Functions for second App function.
            Func <int, Monad <double> > intIMonadIntDoubleFunc1 =
                (x) => { return(new Just <double>(x * x)); };
            Func <int, Monad <double> > intIMonadIntDoubleFunc2 =
                (x) => { return(new Just <double>(x * x * x)); };
            Func <int, Monad <double> > intIMonadIntDoubleFunc3 =
                (x) => { return(new Just <double>(x * x * x * x)); };
            Func <int, Monad <double> > intIMonadIntDoubleFunc4 =
                (x) => { return(new Just <double>(x * x * x * x * x)); };
            Func <int, Monad <double> > intIMonadIntDoubleFunc5 =
                (x) => { return(new ListMonad <double>()
                {
                    x + 1, x - 1
                }); };

            var listMonadIMonadIntDoubleFunc = new ListMonad <Func <int, Monad <double> > >();

            listMonadIMonadIntDoubleFunc.Append(intIMonadIntDoubleFunc1);
            listMonadIMonadIntDoubleFunc.Append(intIMonadIntDoubleFunc2);
            listMonadIMonadIntDoubleFunc.Append(intIMonadIntDoubleFunc3);
            listMonadIMonadIntDoubleFunc.Append(intIMonadIntDoubleFunc4);
            listMonadIMonadIntDoubleFunc.Append(intIMonadIntDoubleFunc5);

            Console.WriteLine("App [Just(x^2), Just(x^3), Just(x^4), Just(x^5) ListMonad{x+1, x-1} over [1,..,5]");
            listMonadInt.App(listMonadIMonadIntDoubleFunc).Visit((x) =>
            {
                Console.Out.Write(x + ", ");
                counter++;
                if (counter % 5 == 0)
                {
                    Console.WriteLine("");
                }
            });
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();

            // Functions for combination
            Func <int, double, double> intDoubleDoubleFunc1 =
                (x, y) => { return((double)x + y); };
            Func <int, double, double> intDoubleDoubleFunc2 =
                (x, y) => { return((double)x - y); };
            Func <int, double, double> intDoubleDoubleFunc3 =
                (x, y) => { return((double)x * y); };
            Func <int, double, double> intDoubleDoubleFunc4 =
                (x, y) => { return((double)x / y); };
            Func <int, double, double> intDoubleDoubleFunc5 =
                (x, y) => { return((double)x % y); };

            var listMonadIntDoubleDoubleFunc = new ListMonad <Func <int, double, double> >()
            {
                intDoubleDoubleFunc1,
                intDoubleDoubleFunc2,
                intDoubleDoubleFunc3,
                intDoubleDoubleFunc4,
                intDoubleDoubleFunc5
            };

            Console.WriteLine("Combinate [1..5] and [1.0,..,9.0] with 'normal' result value and function +:");
            counter = 0;
            listMonadInt.Com(intDoubleDoubleFunc1, listMonadDouble)
            .Visit((x) =>
            {
                Console.Out.Write(x + ", ");
                counter++;
                if (counter % 9 == 0)
                {
                    Console.WriteLine("");
                }
            });
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();

            Console.WriteLine("Combinate [1..5] and [1.0,..,9.0] 'normal' result value and functions [+, -, *, /, %]: ");
            counter = 0;
            listMonadInt.Com(listMonadIntDoubleDoubleFunc, listMonadDouble)
            .Visit((x) =>
            {
                Console.Out.Write(x + ", ");
                counter++;
                if (counter % 9 == 0)
                {
                    Console.WriteLine("");
                }
                if (counter % (5 * 9) == 0)
                {
                    Console.WriteLine("------------------------------------------------");
                }
            });
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();

            // Functions for combination with IMonad as result.
            Func <int, double, Monad <double> > intDoubleIMonadDoubleFunc1 =
                (x, y) => { return(new Just <double>((double)x + y)); };

            Func <int, double, Monad <double> > intDoubleIMonadDoubleFunc2 =
                (x, y) => { return(new Just <double>((double)x - y)); };

            Func <int, double, Monad <double> > intDoubleIMonadDoubleFunc3 =
                (x, y) => { return(new Just <double>((double)x * y)); };

            Func <int, double, Monad <double> > intDoubleIMonadDoubleFunc4 =
                (x, y) => { return(new Just <double>((double)x / y)); };

            Func <int, double, Monad <double> > intDoubleIMonadDoubleFunc5 =
                (x, y) => { return(new ListMonad <double>()
                {
                    (double)x % y
                }); };

            Func <int, double, Monad <double> > intDoubleIMonadDoubleFunc6 =
                (x, y) => { return(new ListMonad <double>()
                {
                    (double)x * y * y, (double)x * y * y * y
                }); };

            Func <int, double, Monad <double> > intDoubleIMonadDoubleFunc7 =
                (x, y) => { return(new Nothing <double>()); };

            var listMonadIntDoubleIMonadDoubleFunc = new ListMonad <Func <int, double, Monad <double> > >()
            {
                intDoubleIMonadDoubleFunc1,
                intDoubleIMonadDoubleFunc2,
                intDoubleIMonadDoubleFunc3,
                intDoubleIMonadDoubleFunc4,
                intDoubleIMonadDoubleFunc5,
                intDoubleIMonadDoubleFunc6,
                intDoubleIMonadDoubleFunc7
            };

            Console.WriteLine("Combination with IMonad function results.");
            Console.WriteLine("List1[1,..,5], List2[1.0,..,9.0] and function +");
            counter = 0;
            listMonadInt.Com(intDoubleIMonadDoubleFunc1, listMonadDouble)
            .Visit((x) =>
            {
                Console.Out.Write(x + ", ");
                counter++;
                if (counter % 9 == 0)
                {
                    Console.WriteLine("");
                }
                if (counter % (5 * 9) == 0)
                {
                    Console.WriteLine("------------------------------------------------");
                }
            });
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();

            Console.WriteLine("Combination with IMonad function results.");
            Console.WriteLine("List1[1,..,5], List2[1.0,..,9.0] and " +
                              "functions [+, -, *, /, %, [x*y*y, x*y*y*y], Nothing]");
            counter = 0;
            listMonadInt.Com(listMonadIntDoubleIMonadDoubleFunc, listMonadDouble)
            .Visit((x) =>
            {
                Console.Out.Write(x + ", ");
                counter++;
                if (counter % 9 == 0)
                {
                    Console.WriteLine("");
                }
                if (counter % (5 * 9) == 0)
                {
                    Console.WriteLine("------------------------------------------------");
                }
            });
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();

            // Visit with other IMonad
            Console.WriteLine("Visit with other IMonad and add (+) values in output.");
            counter = 0;
            listMonadInt.Visit <double>((x, y) =>
            {
                Console.Write(x * y + ", ");
                counter++;
                if (counter % 9 == 0)
                {
                    Console.WriteLine("");
                }
            }, listMonadDouble);
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();

            Console.WriteLine("Function applying with Linq: \n" +
                              " from f in [+, -, *, %]\n" +
                              " from x in [1,..,5]\n" +
                              " from y in [1.0,..,9.0] \n" +
                              " select f(x,y) \n");
            var query = from f in listMonadIntDoubleDoubleFunc
                        from x in listMonadInt
                        from y in listMonadDouble
                        select f(x, y);

            counter = 0;
            query.Visit((x) =>
            {
                Console.Out.Write(x + ", ");
                counter++;
                if (counter % 9 == 0)
                {
                    Console.WriteLine("");
                }
                if (counter % (5 * 9) == 0)
                {
                    Console.WriteLine("------------------------------------------------");
                }
            });
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();

            Console.WriteLine("Function applying with Linq: ");
            var query2 = from f in listMonadIntDoubleIMonadDoubleFunc
                         from x in listMonadInt
                         from y in listMonadDouble
                         select f(x, y);

            counter = 0;
            query2.Visit((x) =>
            {
                Console.Out.Write(x + ", ");
                counter++;
                if (counter % 9 == 0)
                {
                    Console.WriteLine("");
                }
                if (counter % (5 * 9) == 0)
                {
                    Console.WriteLine("------------------------------------------------");
                }
            });
            Console.WriteLine("\n___________________________________________________________");
            Console.ReadLine();
        }