Exemplo n.º 1
0
    def test_list_functor_map(self):
        # fmap f (return v) = return (f v)
        x = unit(42)
        f = lambda x: x * 10

        self.assertEqual(x.map(f), unit(420))

        y = List([1, 2, 3, 4])
        g = lambda x: x * 10

        self.assertEqual(y.map(g), List([10, 20, 30, 40]))
Exemplo n.º 2
0
    def test_list_functor_law_1(self):
        # fmap id = id

        # Singleton list using return
        x = unit(42)
        self.assertEqual(x.map(identity), x)

        # Empty list
        y = List()
        self.assertEqual(y.map(identity), y)

        # Long list
        z = List(range(42))
        self.assertEqual(z.map(identity), z)
Exemplo n.º 3
0
    def test_list_applicative_law_identity(self):
        # pure id <*> v = v

        # Singleton list
        x = unit(42)
        self.assertEquals(pure(identity).apply(x), x)

        # Empty list
        y = List([])
        self.assertEquals(pure(identity).apply(y), y)

        # Log list
        y = List(range(42))
        self.assertEquals(pure(identity).apply(y), y)
Exemplo n.º 4
0
    def test_list_applicative_law_functor(self):
        # pure f <*> x = fmap f x
        f = lambda x: x * 42

        x = unit(42)
        self.assertEquals(pure(f).apply(x), x.map(f))

        # Empty list
        z = List()
        self.assertEquals(pure(f).apply(z), z.map(f))

        # Long list
        z = List(range(42))
        self.assertEquals(pure(f).apply(z), z.map(f))
Exemplo n.º 5
0
    def test_list_monad_law_associativity_empty(self):
        # (m >>= f) >>= g is just like doing m >>= (\x -> f x >>= g)
        f = lambda x: unit(x + 1000)
        g = lambda y: unit(y * 42)

        # Empty list
        m = List()
        self.assertEqual(m.bind(f).bind(g), m.bind(lambda x: f(x).bind(g)))
Exemplo n.º 6
0
    def test_identity_applicative_law_composition_empty(self):
        # pure (.) <*> u <*> v <*> w = u <*> (v <*> w)

        u = pure(lambda x: x * 42)
        v = pure(lambda x: x + 42)

        # Empty list
        w = List()
        self.assertEquals(
            pure(fmap).apply(u).apply(v).apply(w), u.apply(v.apply(w)))
Exemplo n.º 7
0
    def test_list_functor_law2(self):
        # fmap (f . g) x = fmap f (fmap g x)
        def f(x):
            return x + 10

        def g(x):
            return x * 10

        # Singleton list
        x = unit(42)
        self.assertEquals(x.map(compose(f, g)), x.map(g).map(f))

        # Empty list
        y = List([])
        self.assertEquals(y.map(compose(f, g)), y.map(g).map(f))

        # Long list
        z = List(range(42))
        self.assertEquals(z.map(compose(f, g)), z.map(g).map(f))
Exemplo n.º 8
0
 def test_list_length_empty(self):
     xs = List()
     self.assertEqual(0, len(xs))
Exemplo n.º 9
0
 def test_list_list(self):
     xs = List().cons(List().cons(42))
     self.assertEqual(42, xs.head().head())
Exemplo n.º 10
0
 def test_list_tail_tail_null(self):
     xs = List().cons("b").cons("a")
     assert (xs.tail().tail().null())
Exemplo n.º 11
0
 def test_list_tail_head(self):
     xs = List().cons("b").cons("a")
     self.assertEqual("a", xs.head())
Exemplo n.º 12
0
    def test_list_applicative_binary_func(self):
        f = lambda x, y: x + y
        v = List([1, 2])
        w = List([4, 8])

        self.assertEquals(pure(f).apply(v).apply(w), List([5, 9, 6, 10]))
Exemplo n.º 13
0
    def test_list_monad_empty_bind(self):
        m = List()
        f = lambda x: unit(x * 10)

        self.assertEqual(m.bind(f), List())
Exemplo n.º 14
0
 def test_list_head(self):
     x = List().cons(42).head()
     self.assertEqual(42, x)
Exemplo n.º 15
0
    def test_list_applicative_binary_func_empty_arg_2(self):
        f = lambda x, y: x + y
        v = unit(42)
        e = List()

        self.assertEquals(pure(f).apply(v).apply(e), List())
Exemplo n.º 16
0
    def test_list_applicative_empty_func(self):
        v = unit(42)
        w = List.from_iterable([1, 2, 3])

        self.assertEquals(List.empty().apply(v).apply(w), List(Unit))
Exemplo n.º 17
0
    def test_list_applicative_empty_func(self):
        f = []
        v = unit(42)
        w = List([1, 2, 3])

        self.assertEquals(List(f).apply(v).apply(w), List(Unit))
Exemplo n.º 18
0
 def test_list_append_empty(self):
     xs = List()
     ys = List.unit(42)
     zs = xs.append(ys)
     self.assertEqual(ys, zs)
Exemplo n.º 19
0
 def test_list_not_null_after_cons_and_tail(self):
     xs = List().cons(1).tail()
     assert (xs.null())
Exemplo n.º 20
0
 def test_list_null(self):
     xs = List()
     assert (xs.null())
Exemplo n.º 21
0
 def test_list_length_multiple(self):
     xs = List(range(42))
     self.assertEqual(42, len(xs))
Exemplo n.º 22
0
 def test_list_append_non_empty(self):
     xs = List(range(5))
     ys = List(range(5, 10))
     zs = xs.append(ys)
     self.assertEqual(List(range(10)), zs)
Exemplo n.º 23
0
 def test_list_append_empty_other(self):
     xs = List.unit(42)
     ys = List()
     zs = xs.append(ys)
     self.assertEqual(xs, zs)
Exemplo n.º 24
0
 def test_list_not_null_after_cons(self):
     xs = List().cons(1)
     assert (not xs.null())