Ejemplo n.º 1
0
 def test_taking_while_doctest(self):
     result = []
     crt.consume(crt.taking_while(lambda x: x < 5)(
         crt.append(result)), range(1000))
     self.assertListEqual(
         result,
         list(range(5)))
Ejemplo n.º 2
0
 def test_filtering_doctest(self):
     result = []
     f = crt.filtering(lambda x: x % 2 == 0)
     crt.consume(f(crt.append(result)), range(10))
     self.assertListEqual(
         result,
         [x for x in range(10) if x % 2 == 0])
Ejemplo n.º 3
0
 def test_mapping_doctest(self):
     result = []
     m = crt.mapping(lambda x: x * 2)
     crt.consume(m(crt.append(result)), range(10))
     self.assertListEqual(
         result,
         [x * 2 for x in range(10)])
Ejemplo n.º 4
0
 def test_mapcatting_doctest(self):
     result = []
     m = crt.mapcatting(lambda x: reversed(x))
     crt.consume(m(crt.append(result)),
                 [(3, 2, 1, 0), (6, 5, 4), (9, 8, 7)])
     self.assertListEqual(
         result,
         list(range(10)))
Ejemplo n.º 5
0
    def test_compose_mapping_and_filter(self):
        composed = compose(crt.filtering(lambda x: x < 5),
                           crt.mapping(lambda x: x * 2))

        result = []
        f = composed(crt.append(result))
        for i in range(10):
            f.send(i)
        self.assertListEqual(result, [x * 2 for x in range(10) if x < 5])
Ejemplo n.º 6
0
    def test_compose_mapping_and_filter(self):
        composed = compose(
            crt.filtering(lambda x: x < 5),
            crt.mapping(lambda x: x * 2))

        result = []
        f = composed(crt.append(result))
        for i in range(10):
            f.send(i)
        self.assertListEqual(
            result,
            [x * 2 for x in range(10) if x < 5])
Ejemplo n.º 7
0
    def test_compose_three_coroutines(self):
        composed = compose(crt.mapping(lambda x: x * 2),
                           crt.mapping(lambda x: x * x),
                           crt.filtering(lambda x: x % 2 == 0))

        result = []
        f = composed(crt.append(result))

        for i in range(100):
            f.send(i)

        self.assertListEqual(
            result,
            list(
                filter(lambda z: z % 2 == 0,
                       (x * x for x in (y * 2 for y in range(100))))))
Ejemplo n.º 8
0
    def test_reuse_single_taking_instance(self):
        take_5 = crt.taking(5)
        composed = compose(take_5, crt.mapping(lambda x: x * 2), take_5)

        result = []
        f = composed(crt.append(result))
        try:
            for i in range(1000):
                f.send(i)
        except crt.StopConsumption:
            pass

        expected = list(
            itertools.islice((x * 2 for x in itertools.islice(range(1000), 5)),
                             5))

        self.assertListEqual(result, expected)
Ejemplo n.º 9
0
    def test_compose_three_coroutines(self):
        composed = compose(
            crt.mapping(lambda x: x * 2),
            crt.mapping(lambda x: x * x),
            crt.filtering(lambda x: x % 2 == 0))

        result = []
        f = composed(crt.append(result))

        for i in range(100):
            f.send(i)

        self.assertListEqual(
            result,
            list(filter(
                lambda z: z % 2 == 0,
                (x * x for x in
                 (y * 2 for y in
                  range(100))))))
Ejemplo n.º 10
0
    def test_reuse_single_taking_instance(self):
        take_5 = crt.taking(5)
        composed = compose(
            take_5,
            crt.mapping(lambda x: x * 2),
            take_5)

        result = []
        f = composed(crt.append(result))
        try:
            for i in range(1000):
                f.send(i)
        except crt.StopConsumption:
            pass

        expected = list(itertools.islice(
            (x * 2 for x in itertools.islice(range(1000), 5)), 5))

        self.assertListEqual(
            result,
            expected)
Ejemplo n.º 11
0
 def test_taking_while_doctest(self):
     result = []
     crt.consume(
         crt.taking_while(lambda x: x < 5)(crt.append(result)), range(1000))
     self.assertListEqual(result, list(range(5)))
Ejemplo n.º 12
0
 def test_mapcatting_doctest(self):
     result = []
     m = crt.mapcatting(lambda x: reversed(x))
     crt.consume(m(crt.append(result)), [(3, 2, 1, 0), (6, 5, 4),
                                         (9, 8, 7)])
     self.assertListEqual(result, list(range(10)))
Ejemplo n.º 13
0
 def test_filtering_doctest(self):
     result = []
     f = crt.filtering(lambda x: x % 2 == 0)
     crt.consume(f(crt.append(result)), range(10))
     self.assertListEqual(result, [x for x in range(10) if x % 2 == 0])
Ejemplo n.º 14
0
 def test_mapping_doctest(self):
     result = []
     m = crt.mapping(lambda x: x * 2)
     crt.consume(m(crt.append(result)), range(10))
     self.assertListEqual(result, [x * 2 for x in range(10)])