コード例 #1
0
        public void MaybeMonadTest1()
        {
            //Arrange
            Person person   = new Person();
            string expected = "PostalCodeIsEmpty";

            //Act
            var result = person.ToMaybe()
                         .With(x => x.Adress)
                         .Return(x => x.PostalCode, "PostalCodeIsEmpty");

            InnerOutputWriter.WriteLine("result = {0}", result);

            //Assert
            result.Should().Be(expected, "For an empty postalcode we expect the result to be the default value");
            InnerOutputWriter.WriteLine("succes");
        }
コード例 #2
0
        public void SlightlyLessSimpleReduceFunctionTest()
        {
            //arrange
            var input = new List <int> {
                1, 2, 3, 4, 5
            };
            var seed     = 1;
            var expected = 120;

            //act
            var result = Impl.SlightlyLessSimpleReduceFunction(input, seed, (r, i) => r * i);

            InnerOutputWriter.WriteLine("Result = {0}", result);

            //Assert
            result.Should().Be(expected, "We expect the result to be the mutiplication result of all numbers");
            InnerOutputWriter.WriteLine("Succes !");
        }
コード例 #3
0
        public void SimpleFunctionalComposition()
        {
            //arrange
            int             expected = 2 * 5 + 2;
            Func <int, int> add2     = x => x + 2;
            Func <int, int> mult2    = x => x * 2;

            Func <int, int> add2Mult2 = x => add2(mult2(x));

            //act
            var result = add2Mult2(5);

            InnerOutputWriter.WriteLine("expected = {0} , result = {1}", expected, result);

            //assert
            result.Should().Be(expected);
            InnerOutputWriter.WriteLine("succes");
        }
コード例 #4
0
        public void MonadComposition2()
        {
            //arrange
            var expected = "Hello World!, 7, 11-01-2010";

            //act
            var result =
                from hello in "Hello World!".ToIdentity()
                from number in 7.ToIdentity()
                from date in (new DateTime(2010, 1, 11)).ToIdentity()
                select string.Format("{0}, {1}, {2:dd-MM-yyyy}", hello, number, date);

            InnerOutputWriter.WriteLine("result = {0}", result.Value);

            //assert
            result.Value.Should().Be(expected);
            InnerOutputWriter.WriteLine("succes");
        }
コード例 #5
0
        public void MonadComposition()
        {
            //arrange
            var expected = "Hello World!, 7, 11-01-2010";

            //act
            var result =
                "Hello World!".ToIdentity()
                .Bind(hello => 7.ToIdentity()
                      .Bind(number => (new DateTime(2010, 1, 11)).ToIdentity()
                            .Bind(date => string.Format("{0}, {1}, {2:dd-MM-yyyy}", hello, number, date).ToIdentity())));

            InnerOutputWriter.WriteLine("result = {0}", result.Value);

            //assert
            result.Value.Should().Be(expected);
            InnerOutputWriter.WriteLine("succes");
        }
コード例 #6
0
        public void DeclarativeVsImperativeShouldFilterOutOdds()
        {
            //arrange
            var input = new List <int> {
                1, 2, 3, 4, 5
            };
            var expected = new List <int> {
                1, 3, 5
            };
            //act

            var result = Impl.DeclarativeVsImperative(input);

            InnerOutputWriter.WriteLine("Result = {{{0}}}", string.Join(";", result));

            //assert
            result.Should().BeEquivalentTo(expected, "We expect the even numbers to be filtered out");
            InnerOutputWriter.WriteLine("Succes !");
        }
コード例 #7
0
        public void SlightlyLessSimpleMapFunctionTest()
        {
            //arrange
            var input = new List <int> {
                1, 2, 3, 4, 5
            };
            var expected = new List <int> {
                3, 5, 7, 9, 11
            };

            //act
            var result = Impl.SlightlyLessSimpleMapFunction(input, i => i * 2 + 1);

            InnerOutputWriter.WriteLine("Result = {{{0}}}", string.Join(";", result));

            //Assert
            result.Should().BeEquivalentTo(expected, "We expect all numbers to be multiplied by 2 plus one");
            InnerOutputWriter.WriteLine("Succes !");
        }
コード例 #8
0
        public void SimpleMapTest()
        {
            //arrange
            var input = new List <int> {
                1, 2, 3, 4, 5
            };
            var expected = new List <int> {
                2, 4, 6, 8, 10
            };

            //act
            var result = Impl.SimpleMapFunction(input);

            InnerOutputWriter.WriteLine("Result = {{{0}}}", string.Join(";", result));

            //Assert
            result.Should().BeEquivalentTo(expected, "We expect all numbers to be multiplied by 2");
            InnerOutputWriter.WriteLine("Succes !");
        }
コード例 #9
0
        public void SlightlyLessSimpleSelectFunctionTest()
        {
            //arrange
            var input = new List <int> {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
            };
            var expected = new List <int> {
                1, 2, 3, 4, 5, 6, 7, 8, 9
            };

            //act
            var result = Impl.SlightlyLessSimpleSelectFunction(input, i => i < 10);

            InnerOutputWriter.WriteLine("Result = {{{0}}}", string.Join(";", result));

            //Assert
            result.Should().BeEquivalentTo(expected, "We expect all numbers above 10 to be selected");
            InnerOutputWriter.WriteLine("Succes !");
        }
コード例 #10
0
        public void PartialApplicationComposition()
        {
            //arrange
            int expected              = 2 * 5 + 2;
            Func <int, int, int> add  = (x, y) => x + y;
            Func <int, int, int> mult = (x, y) => x * y;

            Func <int, int> add2  = x => add(x, 2);
            Func <int, int> mult2 = x => mult(x, 2);

            //act
            var result = add2(mult2(5));

            InnerOutputWriter.WriteLine("expected = {0} , result = {1}", expected, result);

            //assert
            result.Should().Be(expected);
            InnerOutputWriter.WriteLine("succes");
        }
コード例 #11
0
        public void MaybeMonadTest2()
        {
            //Arrange
            string expected = "Blah22";
            Person person   = new Person {
                Adress = new Address {
                    PostalCode = expected
                }
            };

            //Act
            var result = person.ToMaybe()
                         .With(x => x.Adress)
                         .Return(x => x.PostalCode, "PostalCodeIsEmpty");

            InnerOutputWriter.WriteLine("result = {0}", result);

            //Assert
            result.Should().Be(expected, "When the postalcode not is null we expect the result to be the postalcode");
            InnerOutputWriter.WriteLine("succes");
        }
コード例 #12
0
        public void CurryComposition()
        {
            //arrange
            int expected              = 2 * 5 + 2;
            Func <int, int, int> add  = (x, y) => x + y;
            Func <int, int, int> mult = (x, y) => x * y;

            Func <int, Func <int, int> > curriedAdd      = add.Curry();
            Func <int, Func <int, int> > curriedMultiply = mult.Curry();

            Func <int, int> add2  = curriedAdd(2);
            Func <int, int> mult2 = curriedMultiply(2);

            //act
            var result = add2(mult2(5));

            InnerOutputWriter.WriteLine("expected = {0} , result = {1}", expected, result);

            //assert
            result.Should().Be(expected);
            InnerOutputWriter.WriteLine("succes");
        }
コード例 #13
0
        public void MaybeMonadTest3()
        {
            //Arrange
            string expected = "PostalCodeIsEmpty";
            Person person   = new Person {
                Adress = new Address {
                    PostalCode = "Blah22"
                }, Name = "NameLongerThanFour"
            };

            //Act
            var result = person.ToMaybe()
                         .Where(x => x.Name.Length < 4)
                         .With(x => x.Adress)
                         .Return(x => x.PostalCode, "PostalCodeIsEmpty");

            InnerOutputWriter.WriteLine("result = {0}", result);

            //Assert
            result.Should().Be(expected, "For a person with a name longer than four characters we expect the postalcode to be the default value");
            InnerOutputWriter.WriteLine("succes");
        }
コード例 #14
0
        public void CurryComposition2()
        {
            //arrange
            int expected = 10;
            Func <int, int, int, int, int> addFourThings = (a, b, c, d) => a + b + c + d;

            var curriedAddFourThings = addFourThings.Curry();

            var addOne               = curriedAddFourThings(1);
            var addOneAndTwo         = addOne(2);
            var addOneAndTwoAndThree = addOneAndTwo(3);

            //act
            int result1 = curriedAddFourThings(1)(2)(3)(4);
            int result2 = addOneAndTwoAndThree(4);

            InnerOutputWriter.WriteLine("expected = {0} , result1 = {1}, result2 = {2}", expected, result1, result2);

            //assert
            result1.Should().Be(expected);
            result2.Should().Be(expected);
            InnerOutputWriter.WriteLine("succes");
        }