Ejemplo n.º 1
0
 def testing_reverse2():
     expect("18. Reversing an empty list_f is the empty list_f",
            reverse2(Nil), Nil)
     expect("18. Reversing a singleton list_f is the same list_f",
            reverse2(ListF(1)), ListF(1))
     expect("18. Reversing a list_f", reverse2(ListF(1, 2, 3)),
            ListF(3, 2, 1))
Ejemplo n.º 2
0
 def testing_init():
     expect("2. The first element in the init of a list_f",
            head(init(ListF(1, 2, 3))), 1)
     expect("2. The second element in the init of a list_f",
            head(tail(init(ListF(1, 2, 3)))), 2)
     expect("2. The init length should be 2",
            tail(tail(init(ListF(1, 2, 3)))), Nil)
Ejemplo n.º 3
0
 def testing_elem():
     expect(" 4. Element should not be found in an empty list_f",
            elem(1)(Nil), False)
     expect(" 4. Existing element is first", elem(1)(ListF(1, 2, 3)), True)
     expect(" 4. Existing element is last", elem(1)(ListF(3, 2, 1)), True)
     expect(" 4. Existing element in a singleton list_f",
            elem(1)(ListF(1)), True)
     expect(" 4. Non-existing element", elem(4)(ListF(1, 2, 3)), False)
Ejemplo n.º 4
0
 def testing_unzip():
     expect("20. unzipping empty lists", unzip_func(Nil), pair(Nil)(Nil))
     expect(
         "20. unzipping a list_f",
         unzip_func(ListF(pair(1)(10),
                          pair(2)(20),
                          pair(3)(30))),
         pair(ListF(1, 2, 3))(ListF(10, 20, 30)),
     )
Ejemplo n.º 5
0
    def testing_filter():
        expect("11. Filtering an empty list_f ",
               filter_f(constant(True))(Nil), Nil)
        expect("11. Filtering an empty list_f",
               filter_f(constant(False))(Nil), Nil)

        def odd(x):
            return x % 2 != 0

        expect("11. Filtering odd numbers",
               filter_f(odd)(ListF(1, 2, 3, 4)), ListF(1, 3))
Ejemplo n.º 6
0
    def testing_foldr():
        def plus(a):
            return lambda b: a + b

        def minus(a):
            return lambda b: a - b

        expect("14. Right folding an empty list_f to be the staring value",
               foldr(plus)(0)(Nil), 0)
        expect("14. Right folding a list_f with plus",
               foldr(plus)(0)(ListF(1, 2, 3)), 6)
        expect("14. Right folding a list_f with minus",
               foldr(minus)(0)(ListF(1, 2, 3)), 2)
Ejemplo n.º 7
0
 def testing_eq():
     expect(" 8. Empty list_f equality", eq(Nil)(Nil), True)
     expect(" 8. Non-empty list_f equality",
            eq(ListF(1, 2, 3))(ListF(1, 2, 3)), True)
     expect(" 8. Long and short lists",
            eq(ListF(1, 2, 3))(ListF(2, 3)), False)
     expect(" 8. Short and long lists",
            eq(ListF(1, 2))(ListF(1, 2, 3)), False)
     expect(
         " 8. Same length lists with non-equal element",
         eq(ListF(1, 2, 3))(ListF(1, 2, 4)),
         False,
     )
     expect(" 8. Empty and non-empty lists", eq(Nil)(ListF(1, 2, 4)), False)
     expect(" 8. Non-empty and empty lists", eq(ListF(1, 2, 3))(Nil), False)
Ejemplo n.º 8
0
 def testing_take():
     expect(" 6. Taking 0 from Nil", take(0)(Nil), Nil)
     expect(" 6. Taking 3 from Nil", take(3)(Nil), Nil)
     expect(" 6. Taking 0 from a list_f of 3", take(0)(ListF(1, 2, 3)), Nil)
     expect(" 6. Taking 1 from a list_f of 3",
            take(1)(ListF(1, 2, 3)), ListF(1))
     expect(" 6. Taking 2 from a list_f of 3",
            take(2)(ListF(1, 2, 3)), ListF(1, 2))
     expect(" 6. Taking 3 from a list_f of 3",
            take(3)(ListF(1, 2, 3)), ListF(1, 2, 3))
     expect(" 6. Taking 4 from a list_f of 3",
            take(4)(ListF(1, 2, 3)), ListF(1, 2, 3))
Ejemplo n.º 9
0
 def testing_drop():
     expect(" 7. Dropping 0 from Nil", drop(0)(Nil), Nil)
     expect(" 7. Dropping 3 from Nil", drop(3)(Nil), Nil)
     expect(" 7. Dropping 0 from a list_f of 3",
            drop(0)(ListF(1, 2, 3)), ListF(1, 2, 3))
     expect(" 7. Dropping 1 from a list_f of 3",
            drop(1)(ListF(1, 2, 3)), ListF(2, 3))
     expect(" 7. Dropping 2 from a list_f of 3",
            drop(2)(ListF(1, 2, 3)), ListF(3))
     expect(" 7. Dropping 3 from a list_f of 3",
            drop(3)(ListF(1, 2, 3)), Nil)
     expect(" 7. Dropping 4 from a list_f of 3",
            drop(4)(ListF(1, 2, 3)), Nil)
Ejemplo n.º 10
0
 def testing_map():
     expect("12. Mapping over an empty list_f", map_f(square)(Nil), Nil)
     expect("12. func_mapping with identity",
            map_f(identity)(ListF(1, 2, 3)), ListF(1, 2, 3))
     expect("12. Mapping over a singleton list_f",
            map_f(square)(ListF(2)), ListF(4))
     expect("12. Mapping over a list_f",
            map_f(square)(ListF(1, 2, 3)), ListF(1, 4, 9))
Ejemplo n.º 11
0
 def testing_concat():
     expect(" 9. Concatenating two empty lists should be an empty list_f",
            concat(Nil)(Nil), Nil)
     expect(
         " 9. Concatenating an empty list_f and a non-empty list_f should be the non-empty list_f",
         concat(Nil)(ListF(1, 2, 3)),
         ListF(1, 2, 3),
     )
     expect(
         " 9. Concatenating a non-empty list_f and an empty list_f should be the non-empty list_f",
         concat(ListF(1, 2, 3))(Nil),
         ListF(1, 2, 3),
     )
     expect(
         " 9. Concatenating two non-empty lists should be the second list_f appended to the first",
         concat(ListF(1, 2))(ListF(3, 4)),
         ListF(1, 2, 3, 4),
     )
Ejemplo n.º 12
0
 def testing_nil():
     expect("1. nil on empty list_f", nil(Nil), True)
     expect("1. nil on empty list_f", nil(ListF(1, 2, 3)), False)
Ejemplo n.º 13
0
 def testing_zip():
     expect("19. Zipping empty lists", zip_f(Nil)(Nil), Nil)
     expect("19. Zipping an empty list_f and a list_f",
            zip_f(Nil)(ListF(1, 2, 3)), Nil)
     expect("19. Zipping a list_f and an empty list_f",
            zip_f(ListF(1, 2, 3))(Nil), Nil)
     expect(
         "19. Zipping two same-length lists",
         zip_f(ListF(1, 2, 3))(ListF(10, 20, 30)),
         ListF(pair(1)(10),
               pair(2)(20),
               pair(3)(30)),
     )
     expect(
         "19. Zipping two lists, the first is shorter",
         zip_f(ListF(1, 2, 3))(ListF(10, 20, 30, 40)),
         ListF(pair(1)(10),
               pair(2)(20),
               pair(3)(30)),
     )
     expect(
         "19. Zipping two lists, the second is shorter",
         zip_f(ListF(1, 2, 3, 4))(ListF(10, 20, 30)),
         ListF(pair(1)(10),
               pair(2)(20),
               pair(3)(30)),
     )
Ejemplo n.º 14
0
 def testing_last():
     expect("3. tail of a list_f", last(ListF(1, 2, 3)), 3)
     expect("3. tail of a singleton list_f", last(ListF(1)), 1)
Ejemplo n.º 15
0
 def testing_map2():
     expect("17. Mapping over an empty list_f", func_map2(square)(Nil), Nil)
     expect("17. Mapping over a singleton list_f",
            func_map2(square)(ListF(2)), ListF(4))
     expect("17. Mapping over a list_f",
            func_map2(square)(ListF(1, 2, 3)), ListF(1, 4, 9))
Ejemplo n.º 16
0
 def testing_copy2():
     expect("16. Copy an empty list_f", copy2(Nil), Nil)
     expect("16. Copy a singleton list_f", copy2(ListF(1)), ListF(1))
     expect("16. Copy a list_f", copy2(ListF(1, 2, 3)), ListF(1, 2, 3))
Ejemplo n.º 17
0
 def testing_length():
     expect(" 5. The length of an empty list_f should be 0", length(Nil), 0)
     expect(" 5. The length of a singleton list_f should be 1",
            length(ListF(1)), 1)
     expect(" 5. The length of a list_f with 3 elements should be 3",
            length(ListF(1, 2, 3)), 3)