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]) )
def test_map(self): xs = toLinkedList([1, 2, 3]) def f(x): return x * 2 self.assertEqual( xs.map(f), toLinkedList([2, 4, 6]) )
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])))
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]) )
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) )
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)
def test_functor_identity_law(self): xs = toLinkedList([1, 2, 3]) self.assertEqual( identity(xs), xs.map(identity) )
def test_eq_cons(self): self.assertEqual( cons(1, cons(2, cons(3, nil))), toLinkedList([1, 2, 3]) )
def test_fold(self): def add(x, y): return x + y self.assertEqual( toLinkedList([1, 2, 3]).fold(add, 0), 6 )
def f(x): return toLinkedList([x, x + 1]) xs = toLinkedList([1, 2, 3])
def test_sequence_nothing(self): self.assertEqual( Option.sequence(toLinkedList([some(1), nothing, some(3)])), nothing)
def test_sequence_some(self): xs = toLinkedList([some(1), some(2), some(3)]) self.assertEqual(Option.sequence(xs), some(toLinkedList([1, 2, 3])))