Пример #1
0
    def test_functor_1st_low(self):
        seq = Seq(1, 2, 3, 4, 5)

        def identity(x):
            return x

        self.assertEqual(seq.map(identity), identity(seq))
Пример #2
0
def test_monad_1st_low(self):
    seq = Seq(1)

    def f(x):
        return Seq(x + 1)

    self.assertEqual(seq.flat_map(f), f(1))
Пример #3
0
    def test_applicative_2nd_low(self):
        seq = Seq(1, 2, 3, 4, 5)

        def identity(x):
            return x

        self.assertEqual(seq.ap(Seq(identity)), seq)
Пример #4
0
    def test_applicative_1st_low(self):
        seq = Seq(1, 2, 3, 4, 5)

        def f(x):
            return x + 1

        self.assertEqual(seq.ap(Seq(f)), seq.map(f))
Пример #5
0
    def test_functor_2nd_low(self):
        seq = Seq(1, 2, 3, 4, 5)

        def f(x):
            return x + 1

        def g(x):
            return x * 2

        self.assertEqual(seq.map(lambda x: f(g(x))), seq.map(g).map(f))
Пример #6
0
def test_monad_3rd_low(self):
    seq = Seq(1, 2, 3, 4, 5)

    def f(x):
        return Seq(x + 1)

    def g(x):
        return Seq(x * 2)

    self.assertEqual(
        seq.flat_map(f).flat_map(g), seq.flat_map(lambda x: f(x).flat_map(g)))
Пример #7
0
 def f(x):
     return Seq(x + 1)
Пример #8
0
 def g(x):
     return Seq(x * 2)
Пример #9
0
def test_monad_2nd_low(self):
    seq = Seq(1, 2, 3, 4, 5)

    self.assertEqual(seq.flat_map(Seq), seq)
Пример #10
0
    def test_fold(self):
        seq = Seq(1, 2, 3, 4, 5)
        expect = 15
        actual = seq.fold(lambda x, y: x + y, 0)

        self.assertEqual(expect, actual)