Example #1
0
 def test_mutable_inits(self):
     """Tests that the same mutable init object isn't shared across invocations."""
     result = transduce(transducer=mapping(lambda x: x),
                        reducer=appending(),
                        iterable=range(3))
     self.assertListEqual(result, [0, 1, 2])
     result = transduce(transducer=mapping(lambda x: x),
                        reducer=appending(),
                        iterable=range(3))
     self.assertListEqual(result, [0, 1, 2])
Example #2
0
 def test_ordering_preserves_mutable_sequence_type(self):
     result = transduce(transducer=ordering(),
                        reducer=appending(),
                        iterable=[4, 2, 6, 10, 8],
                        init=deque())
     self.assertIsInstance(result, deque)
     self.assertSequenceEqual(result, deque([2, 4, 6, 8, 10]))
Example #3
0
 def test_ordering_reverse_with_key(self):
     result = transduce(transducer=ordering(key=lambda x: len(x),
                                            reverse=True),
                        reducer=appending(),
                        iterable="The quick brown fox jumped".split())
     self.assertSequenceEqual(result,
                              ['jumped', 'quick', 'brown', 'The', 'fox'])
Example #4
0
 def test_ordering_preserves_mutable_sequence_type(self):
     result = transduce(transducer=ordering(),
                        reducer=appending(),
                        iterable=[4, 2, 6, 10, 8],
                        init=deque())
     self.assertIsInstance(result, deque)
     self.assertSequenceEqual(result, deque([2, 4, 6, 8, 10]))
Example #5
0
 def test_windowing_no_padding(self):
     result = transduce(transducer=windowing(3, window_type=list),
                        reducer=appending(),
                        iterable=[42, 12, 45, 9, 18, 3, 34, 13])
     self.assertListEqual(
         result, [[42], [42, 12], [42, 12, 45], [12, 45, 9], [45, 9, 18],
                  [9, 18, 3], [18, 3, 34], [3, 34, 13], [34, 13], [13]])
Example #6
0
 def test_chained_transducers(self):
     result = transduce(transducer=compose(mapping(lambda x: x * x),
                                           filtering(lambda x: x % 5 != 0),
                                           taking(6),
                                           dropping_while(lambda x: x < 15),
                                           distinct()),
                        reducer=appending(),
                        iterable=range(20))
     self.assertSequenceEqual(result, [16, 36, 49])
Example #7
0
 def parse_group(lines):
     cmd, path_type, r, g, b = lines[0].split(';')
     color = float(r), float(g), float(b)
     strokes = transduce(
         compose(partitioning(lambda s: s.upper().startswith('MOVETO;')),
                 filtering(lambda lines: len(lines) > 1),
                 mapping(parse_path)), appending(), lines[1:])
     path = PltmePathGroup(strokes, path_type, color)
     return path
Example #8
0
 def test_chained_transducers(self):
     result = transduce(transducer=compose(
                       mapping(lambda x: x*x),
                       filtering(lambda x: x % 5 != 0),
                       taking(6),
                       dropping_while(lambda x: x < 15),
                       distinct()),
                   reducer=appending(),
                   iterable=range(20))
     self.assertSequenceEqual(result, [16, 36, 49])
Example #9
0
 def test_windowing_padding(self):
     result = transduce(transducer=windowing(3, padding=0, window_type=list),
                        reducer=appending(),
                        iterable=[42, 12, 45, 9, 18, 3, 34, 13])
     self.assertListEqual(result,
                          [[0, 0, 42],
                           [0, 42, 12],
                           [42, 12, 45],
                           [12, 45, 9],
                           [45, 9, 18],
                           [9, 18, 3],
                           [18, 3, 34],
                           [3, 34, 13],
                           [34, 13, 0],
                           [13, 0, 0]])
Example #10
0
def transduce(transducer, iterable):
    r = transducer(appending())
    accumulator = deque()
    reduced = False
    for item in iterable:
        accumulator = r.step(accumulator, item)
        if isinstance(accumulator, Reduced):
            accumulator = accumulator.value
            reduced = True

        yield from pending_in(accumulator)

        if reduced:
            break

    completed_result = r.complete(accumulator)
    assert completed_result is accumulator

    yield from pending_in(accumulator)
Example #11
0
def transduce(transducer, iterable):
    r = transducer(appending())
    accumulator = deque()
    reduced = False
    for item in iterable:
        accumulator = r.step(accumulator, item)
        if isinstance(accumulator, Reduced):
            accumulator = accumulator.value
            reduced = True

        yield from pending_in(accumulator)

        if reduced:
            break

    completed_result = r.complete(accumulator)
    assert completed_result is accumulator

    yield from pending_in(accumulator)
Example #12
0
async def transduce(transducer, aiterable):
    r = transducer(appending())
    accumulator = deque()
    reduced = False
    async for item in aiterable:
        accumulator = r.step(accumulator, item)
        if isinstance(accumulator, Reduced):
            accumulator = accumulator.value
            reduced = True

        while accumulator:
            yield accumulator.popleft()

        if reduced:
            break

    completed_result = r.complete(accumulator)
    assert completed_result is accumulator

    while accumulator:
        yield accumulator.popleft()
Example #13
0
def parse_pltme(source: str):
    def parse_path(lines):
        def coords(s):
            cmd, x, y = s.split(';')
            return float(x), float(y)

        coords = [coords(l) for l in lines]
        return PltmePath(coords)

    def parse_group(lines):
        cmd, path_type, r, g, b = lines[0].split(';')
        color = float(r), float(g), float(b)
        strokes = transduce(
            compose(partitioning(lambda s: s.upper().startswith('MOVETO;')),
                    filtering(lambda lines: len(lines) > 1),
                    mapping(parse_path)), appending(), lines[1:])
        path = PltmePathGroup(strokes, path_type, color)
        return path

    return transduce(
        compose(mapping(lambda s: s.strip()),
                partitioning(lambda s: s.upper().startswith('STARTPATH;')),
                mapping(parse_group)), appending(), source.splitlines())
Example #14
0
 def test_identity(self):
     result = transduce(transducer=Transducer,
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [0, 1, 2, 3, 4])
Example #15
0
 def test_filtering(self):
     result = transduce(transducer=filtering(lambda w: 'x' in w),
                        reducer=appending(),
                        iterable='socks in box fox on clocks'.split())
     self.assertListEqual(result, ['box', 'fox'])
Example #16
0
 def test_mapping(self):
     result = transduce(transducer=mapping(lambda x: x*x),
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [0, 1, 4, 9, 16])
Example #17
0
 def test_reversing_preserves_mutable_sequence_type(self):
     result = transduce(transducer=reversing(),
                        reducer=appending(),
                        iterable=[2, 4, 6, 8, 10])
     self.assertIsInstance(result, list)
     self.assertSequenceEqual(result, [10, 8, 6, 4, 2])
Example #18
0
 def test_two_items_returns_two_element_list(self):
     result = transduce(Transducer,
                        appending(),
                        (23, 78))
     self.assertEqual(result, [23, 78])
Example #19
0
 def test_repeating_validation(self):
     with self.assertRaises(ValueError):
         transduce(transducer=repeating(-1),
                   reducer=appending(),
                   iterable=[1, 3, 5])
Example #20
0
 def test_mapcatting(self):
     result = transduce(transducer=mapcatting(list),
                        reducer=appending(),
                        iterable=['new', 'found', 'land'])
     self.assertListEqual(result, list("newfoundland"))
Example #21
0
 def test_ordering_reverse_with_key(self):
     result = transduce(transducer=ordering(key=lambda x: len(x), reverse=True),
                        reducer=appending(),
                        iterable="The quick brown fox jumped".split())
     self.assertSequenceEqual(result, ['jumped', 'quick', 'brown', 'The', 'fox'])
Example #22
0
 def test_mutable_inits(self):
     """Tests that the same mutable init object isn't shared across invocations."""
     result = transduce(transducer=mapping(lambda x: x), reducer=appending(), iterable=range(3))
     self.assertListEqual(result, [0, 1, 2])
     result = transduce(transducer=mapping(lambda x: x), reducer=appending(), iterable=range(3))
     self.assertListEqual(result, [0, 1, 2])
Example #23
0
 def test_ordering_reverse(self):
     result = transduce(transducer=ordering(reverse=True),
                        reducer=appending(),
                        iterable=[4, 2, 6, 10, 8])
     self.assertSequenceEqual(result, [10, 8, 6, 4, 2])
Example #24
0
 def test_reversing(self):
     result = transduce(transducer=reversing(),
                        reducer=appending(),
                        iterable=[2, 4, 6, 8, 10])
     self.assertSequenceEqual(result, [10, 8, 6, 4, 2])
Example #25
0
 def test_filtering(self):
     result = transduce(transducer=filtering(lambda w: 'x' in w),
                        reducer=appending(),
                        iterable='socks in box fox on clocks'.split())
     self.assertListEqual(result, ['box', 'fox'])
Example #26
0
 def test_scanning(self):
     result = transduce(transducer=scanning(operator.add),
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [0, 1, 3, 6, 10])
Example #27
0
 def test_scanning(self):
     result = transduce(transducer=scanning(operator.add),
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [0, 1, 3, 6, 10])
Example #28
0
 def test_enumerating(self):
     result = transduce(transducer=enumerating(),
                        reducer=appending(),
                        iterable=[2, 4, 6, 8, 10])
     self.assertListEqual(result, [(0, 2), (1, 4), (2, 6), (3, 8), (4, 10)])
Example #29
0
 def test_scanning_with_init(self):
     result = transduce(transducer=scanning(operator.add, 3),
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [3, 4, 6, 9, 13])
Example #30
0
 def test_taking_while(self):
     result = transduce(transducer=taking_while(lambda x: x < 6),
                        reducer=appending(),
                        iterable=[2, 4, 5, 8, 10])
     self.assertListEqual(result, [2, 4, 5])
Example #31
0
 def test_enumerating(self):
     result = transduce(transducer=enumerating(),
                        reducer=appending(),
                        iterable=[2, 4, 6, 8, 10])
     self.assertListEqual(result, [(0, 2), (1, 4), (2, 6), (3, 8), (4, 10)])
Example #32
0
 def test_pairwise_at_least_two(self):
     result = transduce(transducer=pairwise(),
                        reducer=appending(),
                        iterable=[1, 3, 5, 7, 2, 1, 9])
     self.assertListEqual(result, [(1, 3), (3, 5), (5, 7), (7, 2), (2, 1), (1, 9)])
Example #33
0
 def test_enumerating_with_start(self):
     result = transduce(transducer=enumerating(start=3),
                        reducer=appending(),
                        iterable=[2, 4, 6, 8, 10])
     self.assertListEqual(result, [(3, 2), (4, 4), (5, 6), (6, 8), (7, 10)])
Example #34
0
 def test_ordering(self):
     result = transduce(transducer=ordering(),
                        reducer=appending(),
                        iterable=[4, 2, 6, 10, 8])
     self.assertSequenceEqual(result, [2, 4, 6, 8, 10])
Example #35
0
 def test_mapcatting(self):
     result = transduce(transducer=mapcatting(list),
                        reducer=appending(),
                        iterable=['new', 'found', 'land'])
     self.assertListEqual(result, list("newfoundland"))
Example #36
0
 def test_ordering(self):
     result = transduce(transducer=ordering(),
                        reducer=appending(),
                        iterable=[4, 2, 6, 10, 8])
     self.assertSequenceEqual(result, [2, 4, 6, 8, 10])
Example #37
0
 def test_taking_while(self):
     result = transduce(transducer=taking_while(lambda x: x < 6),
                        reducer=appending(),
                        iterable=[2, 4, 5, 8, 10])
     self.assertListEqual(result, [2, 4, 5])
Example #38
0
 def test_ordering_reverse(self):
     result = transduce(transducer=ordering(reverse=True),
                        reducer=appending(),
                        iterable=[4, 2, 6, 10, 8])
     self.assertSequenceEqual(result, [10, 8, 6, 4, 2])
Example #39
0
 def test_dropping(self):
     result = transduce(transducer=dropping(3),
                        reducer=appending(),
                        iterable=[2, 4, 5, 8, 10])
     self.assertListEqual(result, [8, 10])
Example #40
0
 def test_windowing_validation(self):
     with self.assertRaises(ValueError):
         transduce(transducer=windowing(0),
                   reducer=appending(),
                   iterable=[42, 12, 45, 9, 18, 3, 34, 13])
Example #41
0
 def test_dropping_validation(self):
     with self.assertRaises(ValueError):
         transduce(transducer=dropping(-3),
                   reducer=appending(),
                   iterable=[2, 4, 5, 8, 10])
Example #42
0
 def test_batching_inexact_2(self):
     result = transduce(transducer=batching(3),
                        reducer=appending(),
                        iterable=[42, 12, 45, 9, 18, 3, 34, 13])
     self.assertListEqual(result, [[42, 12, 45], [9, 18, 3], [34, 13]])
Example #43
0
 def test_repeating_zero(self):
     result = transduce(transducer=repeating(0),
                        reducer=appending(),
                        iterable=[1, 3, 5])
     self.assertListEqual(result, [])
Example #44
0
 def test_scanning_with_init(self):
     result = transduce(transducer=scanning(operator.add, 3),
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [3, 4, 6, 9, 13])
Example #45
0
 def test_identity(self):
     result = transduce(transducer=Transducer,
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [0, 1, 2, 3, 4])
Example #46
0
 def test_enumerating_with_start(self):
     result = transduce(transducer=enumerating(start=3),
                        reducer=appending(),
                        iterable=[2, 4, 6, 8, 10])
     self.assertListEqual(result, [(3, 2), (4, 4), (5, 6), (6, 8), (7, 10)])
Example #47
0
 def test_windowing_validation(self):
     with self.assertRaises(ValueError):
         transduce(transducer=windowing(0),
                   reducer=appending(),
                   iterable=[42, 12, 45, 9, 18, 3, 34, 13])
Example #48
0
 def test_taking_validation(self):
     with self.assertRaises(ValueError):
         transduce(transducer=taking(-3),
                   reducer=appending(),
                   iterable=[2, 4, 5, 8, 10])
Example #49
0
 def test_repeating_zero(self):
     result = transduce(transducer=repeating(0),
                        reducer=appending(),
                        iterable=[1, 3, 5])
     self.assertListEqual(result, [])
Example #50
0
 def test_dropping(self):
     result = transduce(transducer=dropping(3),
                        reducer=appending(),
                        iterable=[2, 4, 5, 8, 10])
     self.assertListEqual(result, [8, 10])
Example #51
0
 def test_repeating_validation(self):
     with self.assertRaises(ValueError):
         transduce(transducer=repeating(-1),
                   reducer=appending(),
                   iterable=[1, 3, 5])
Example #52
0
 def test_pairwise_single(self):
     """A single item fed into pairwise is discarded."""
     result = transduce(transducer=pairwise(),
                        reducer=appending(),
                        iterable=[42])
     self.assertListEqual(result, [])
Example #53
0
 def test_mapping(self):
     result = transduce(transducer=mapping(lambda x: x * x),
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [0, 1, 4, 9, 16])
Example #54
0
 def test_zero_items_returns_initial_empty_list(self):
     result = transduce(Transducer,
                        appending(),
                        empty_iter())
     self.assertEqual(result, [])
Example #55
0
 def test_reversing(self):
     result = transduce(transducer=reversing(),
                        reducer=appending(),
                        iterable=[2, 4, 6, 8, 10])
     self.assertSequenceEqual(result, [10, 8, 6, 4, 2])
Example #56
0
 def test_appending_to_immutable_sequence_raises_attribute_error(self):
     with self.assertRaises(AttributeError):
         transduce(Transducer,
                   appending(),
                   (23, 78),
                   init=tuple())
Example #57
0
 def test_reversing_preserves_mutable_sequence_type(self):
     result = transduce(transducer=reversing(),
                        reducer=appending(),
                        iterable=[2, 4, 6, 8, 10])
     self.assertIsInstance(result, list)
     self.assertSequenceEqual(result, [10, 8, 6, 4, 2])