Example #1
0
    def test_list_applicative_binary_func(self):
        f = lambda x, y: x + y
        v = List.from_iterable([1, 2])
        w = List.from_iterable([4, 8])

        self.assertEquals(
            pure(f).apply(v).apply(w), List.from_iterable([5, 9, 6, 10]))
Example #2
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.from_iterable([1, 2, 3, 4])
        g = lambda x: x * 10

        self.assertEqual(y.map(g), List.from_iterable([10, 20, 30, 40]))
Example #3
0
 def test_do_list_basic(self):
     l = lambda *items: List.from_iterable(items)
     out1 = do(let(a=l(3, 10, 6)), let(b=l(100, 200)),
               lambda e: List.unit(e.a + e.b))
     out2 = l(3, 10, 6) | (lambda a: l(100, 200) |
                           (lambda b: List.unit(a + b)))
     self.assertEqual(out1, out2)
Example #4
0
 def test_do_scoping_correct(self):
     try:
         l = lambda *items: List.from_iterable(items)
         do(let(a=l(1, 2, 3)), let(b=lambda e: List.unit(e.a)),
            lambda e: List.unit(e.a * e.b))
     except AttributeError:
         assert False
Example #5
0
    def test_list_monad_law_associativity_range(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)

        # Long list
        m = List.from_iterable(range(42))
        self.assertEqual(m.bind(f).bind(g), m.bind(lambda x: f(x).bind(g)))
Example #6
0
    def test_identity_applicative_law_composition_range(self):
        # pure (.) <*> u <*> v <*> w = u <*> (v <*> w)

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

        # Long list
        w = List.from_iterable(range(42))
        self.assertEquals(
            pure(fmap).apply(u).apply(v).apply(w), u.apply(v.apply(w)))
Example #7
0
 def test_do_list_pythag(self):
     r = lambda low, high: List.from_iterable(range(low, high))
     out1 = do(let(z=r(1, 21)), let(x=lambda e: r(1, e.z + 1)),
               let(y=lambda e: r(e.x, e.z + 1)),
               lambda e: guard(List, e.x * e.x + e.y * e.y == e.z * e.z),
               lambda e: List.unit((e.x, e.y, e.z)))
     out2 = r(1, 21) | (
         lambda z: r(1, z + 1) |
         (lambda x: r(x, z + 1) |
          (lambda y: guard(List, x * x + y * y == z * z) >> List.unit(
              (x, y, z)))))
     self.assertEqual(out1, out2)
Example #8
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.from_iterable(range(42))
        self.assertEqual(z.map(identity), z)
Example #9
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.empty()
        self.assertEquals(pure(identity).apply(y), y)

        # Log list
        y = List.from_iterable(range(42))
        self.assertEquals(pure(identity).apply(y), y)
Example #10
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.from_iterable(range(42))
        self.assertEquals(pure(f).apply(z), z.map(f))
Example #11
0
def _get_setup_args(path):
    rtn = List.empty()
    with open(path, 'rU') as file:
        t = compile(file.read(), path, 'exec', ast.PyCF_ONLY_AST)
        for node in (n for n in t.body if isinstance(n, ast.Expr)):
            if isinstance(node.value, ast.Call):
                if hasattr(node.value.func,
                           'id') and node.value.func.id == 'setup':
                    rtn = List.from_iterable(node.value.keywords)
                    break
                else:
                    print(dir(node.value.func), node.value.func.value)

    return rtn
Example #12
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.empty()
        self.assertEquals(y.map(compose(f, g)), y.map(g).map(f))

        # Long list
        z = List.from_iterable(range(42))
        self.assertEquals(z.map(compose(f, g)), z.map(g).map(f))
Example #13
0
 def test_list_append_non_empty(self):
     xs = List.from_iterable(range(5))
     ys = List.from_iterable(range(5, 10))
     zs = xs.append(ys)
     self.assertEqual(List.from_iterable(range(10)), zs)
Example #14
0
 def test_list_length_multiple(self):
     xs = List.from_iterable(range(42))
     self.assertEqual(42, len(xs))
Example #15
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))