コード例 #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)))
コード例 #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])
コード例 #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)])
コード例 #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)))
コード例 #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])
コード例 #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])
コード例 #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))))))
コード例 #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)
コード例 #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))))))
コード例 #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)
コード例 #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)))
コード例 #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)))
コード例 #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])
コード例 #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)])