Example #1
0
 def test_bind(self):
     def f(x): return toLinkedList([x, x + 1])
     xs = toLinkedList([1, 2, 3])
     self.assertEqual(
         xs.bind(f),
         toLinkedList([1, 2, 2, 3, 3, 4])
     )
Example #2
0
 def test_map(self):
     xs = toLinkedList([1, 2, 3])
     def f(x): return x * 2
     self.assertEqual(
         xs.map(f),
         toLinkedList([2, 4, 6])
     )
Example #3
0
    def test_traverse_some(self):
        xs = toLinkedList([1, 2, 3])

        def add1A(x):
            return some(x + 1)

        self.assertEqual(Option.traverse(add1A, xs),
                         some(toLinkedList([2, 3, 4])))
Example #4
0
 def test_ap(self):
     def f(x): return x + 1
     def g(x): return x * 2
     fs = toLinkedList([f, g])
     xs = toLinkedList([1, 2, 3])
     self.assertEqual(
         fs.ap(xs),
         toLinkedList([2, 3, 4, 2, 4, 6])
     )
Example #5
0
 def test_functor_associativity_law(self):
     xs = toLinkedList([1, 2, 3])
     def f(x): return x + 1
     def g(x): return x * 2
     self.assertEqual(
         xs.map(f).map(g),
         xs.map(f |andThen| g)
     )
Example #6
0
    def test_traverse_nothing(self):
        xs = toLinkedList([1, 2, 3])

        def oddA(x):
            if x % 2 == 1:
                return some(x)
            else:
                return nothing

        self.assertEqual(Option.traverse(oddA, xs), nothing)
Example #7
0
 def test_functor_identity_law(self):
     xs = toLinkedList([1, 2, 3])
     self.assertEqual(
         identity(xs),
         xs.map(identity)
     )
Example #8
0
 def test_eq_cons(self):
     self.assertEqual(
         cons(1, cons(2, cons(3, nil))),
         toLinkedList([1, 2, 3])
     )
Example #9
0
 def test_fold(self):
     def add(x, y): return x + y
     self.assertEqual(
         toLinkedList([1, 2, 3]).fold(add, 0),
         6
     )
Example #10
0
 def f(x): return toLinkedList([x, x + 1])
 xs = toLinkedList([1, 2, 3])
Example #11
0
 def test_sequence_nothing(self):
     self.assertEqual(
         Option.sequence(toLinkedList([some(1), nothing,
                                       some(3)])), nothing)
Example #12
0
 def test_sequence_some(self):
     xs = toLinkedList([some(1), some(2), some(3)])
     self.assertEqual(Option.sequence(xs), some(toLinkedList([1, 2, 3])))