Пример #1
0
        public void FuncTest()
        {
            bool            isExecuted1 = false;
            Func <int, int> addOne      = x => { isExecuted1 = true; return(x + 1); };
            Func <int>      query1      = FuncExtensions.Func(addOne).Apply(2.Func());

            Assert.IsFalse(isExecuted1);          // Deferred and lazy.
            Assert.AreEqual(addOne(2), query1()); // Execution.
            Assert.IsTrue(isExecuted1);

            // f.Functor().Apply(F) == F.Select(f)
            Assert.AreEqual(FuncExtensions.Func(addOne).Apply(1.Func())(), 1.Func().Select(addOne)());
            // id.Functor().Apply(F) == F
            Func <int, int> id = Functions.Id;

            Assert.AreEqual(FuncExtensions.Func(id).Apply(1.Func())(), 1.Func()());
            // o.Functor().Apply(F1).Apply(F2).Apply(F3) == F1.Apply(F2.Apply(F3))
            Func <int, int> addTwo = x => x + 2;
            Func <int>      left1  = FuncExtensions.Func(Functions <int, int, int> .o).Apply(FuncExtensions.Func(addOne)).Apply(FuncExtensions.Func(addTwo)).Apply(1.Func());
            Func <int>      right1 = FuncExtensions.Func(addOne).Apply(FuncExtensions.Func(addTwo).Apply(1.Func()));

            Assert.AreEqual(left1(), right1());
            // f.Functor().Apply(a.Functor()) == f(a).Functor()
            Assert.AreEqual(FuncExtensions.Func(addOne).Apply(1.Func())(), addOne(1).Func()());
            // F.Apply(a.Functor()) == (f => f(a)).Functor().Apply(F)
            Func <int> left2  = FuncExtensions.Func(addOne).Apply(1.Func());
            Func <int> right2 = FuncExtensions.Func(new Func <Func <int, int>, int>(f => f(1))).Apply(FuncExtensions.Func(addOne));

            Assert.AreEqual(left2(), right2());
        }
Пример #2
0
        public void FuncTest2()
        {
            bool isExecuted1 = false;
            Func <int, Func <int, string> > add = x => y =>
            { isExecuted1 = true; return((x + y).ToString(CultureInfo.InvariantCulture)); };
            Func <string> query2 = FuncExtensions.Func(add).Apply(1.Func()).Apply(2.Func());

            Assert.IsFalse(isExecuted1);          // Deferred and lazy.
            Assert.AreEqual(add(1)(2), query2()); // Execution.
            Assert.IsTrue(isExecuted1);

            // f.Functor().Apply(F) == F.Select(f)
            Assert.AreEqual(FuncExtensions.Func(add).Apply(1.Func())()(2), 1.Func().Select(add)()(2));
            // id.Functor().Apply(F) == F
            Func <int, int> id = Functions.Id;

            Assert.AreEqual(FuncExtensions.Func(id).Apply(1.Func())(), 1.Func()());
            // o.Functor().Apply(F1).Apply(F2).Apply(F3) == F1.Apply(F2.Apply(F3))
            Func <Func <int, string>, Func <int, int> > length = f => x => f(x).Length;
            Func <Func <int, int> > left1  = FuncExtensions.Func(Functions <int, Func <int, string>, Func <int, int> > .o).Apply(FuncExtensions.Func(length)).Apply(FuncExtensions.Func(add)).Apply(1.Func());
            Func <Func <int, int> > right1 = FuncExtensions.Func(length).Apply(FuncExtensions.Func(add).Apply(1.Func()));

            Assert.AreEqual(left1()(2), right1()(2));
            // f.Functor().Apply(a.Functor()) == f(a).Functor()
            Assert.AreEqual(FuncExtensions.Func(add).Apply(1.Func())()(2), FuncExtensions.Func(add(1))()(2));
            // F.Apply(a.Functor()) == (f => f(a)).Functor().Apply(F)
            Func <Func <int, string> > left2  = FuncExtensions.Func(add).Apply(1.Func());
            Func <Func <int, string> > right2 = FuncExtensions.Func(new Func <Func <int, Func <int, string> >, Func <int, string> >(
                                                                        f => f(1))).Apply(FuncExtensions.Func(add));

            Assert.AreEqual(left2()(2), right2()(2));

            bool          isExecuted3      = false;
            Func <string> consoleReadLine1 = () => "a";
            Func <string> consoleReadLine2 = () => "b";
            Func <string, Func <string, string> > concat = x => y =>
            { isExecuted3 = true; return(string.Concat(x, y)); };
            Func <string> concatLines = FuncExtensions.Func(concat).Apply(consoleReadLine1).Apply(consoleReadLine2);

            Assert.IsFalse(isExecuted3); // Deferred and lazy.
            Assert.AreEqual(string.Concat(consoleReadLine1(), consoleReadLine2()), concatLines());
            Assert.IsTrue(isExecuted3);
        }