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]))
 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])
 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]])
Beispiel #4
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)
 def test_reversing(self):
     result = transduce(transducer=reversing(),
                        reducer=appending(),
                        iterable=[2, 4, 6, 8, 10])
     self.assertSequenceEqual(result, [10, 8, 6, 4, 2])
 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])
 def test_pairwise_single(self):
     """A single item fed into pairwise is discarded."""
     result = transduce(transducer=pairwise(),
                        reducer=appending(),
                        iterable=[42])
     self.assertListEqual(result, [])
 def test_two_items_returns_two_element_list(self):
     result = transduce(Transducer,
                        appending(),
                        (23, 78))
     self.assertEqual(result, [23, 78])
 def test_dropping(self):
     result = transduce(transducer=dropping(3),
                        reducer=appending(),
                        iterable=[2, 4, 5, 8, 10])
     self.assertListEqual(result, [8, 10])
Beispiel #10
0
 def test_mapcatting(self):
     result = transduce(transducer=mapcatting(list),
                        reducer=appending(),
                        iterable=['new', 'found', 'land'])
     self.assertListEqual(result, list("newfoundland"))
Beispiel #11
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)])
Beispiel #12
0
 def test_scanning(self):
     result = transduce(transducer=scanning(operator.add),
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [0, 1, 3, 6, 10])
Beispiel #13
0
 def test_repeating_validation(self):
     with self.assertRaises(ValueError):
         transduce(transducer=repeating(-1),
                   reducer=appending(),
                   iterable=[1, 3, 5])
Beispiel #14
0
 def test_repeating_zero(self):
     result = transduce(transducer=repeating(0),
                        reducer=appending(),
                        iterable=[1, 3, 5])
     self.assertListEqual(result, [])
Beispiel #15
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])
Beispiel #16
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])
Beispiel #17
0
 def test_identity(self):
     result = transduce(transducer=Transducer,
                        reducer=appending(),
                        iterable=range(5))
     self.assertListEqual(result, [0, 1, 2, 3, 4])
Beispiel #18
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]])
Beispiel #19
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])
Beispiel #20
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])
Beispiel #21
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)])
Beispiel #22
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])
Beispiel #23
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])
Beispiel #24
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'])
Beispiel #25
0
 def test_dropping_validation(self):
     with self.assertRaises(ValueError):
         transduce(transducer=dropping(-3),
                   reducer=appending(),
                   iterable=[2, 4, 5, 8, 10])
Beispiel #26
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])
 def test_zero_items_returns_initial_empty_list(self):
     result = transduce(Transducer,
                        appending(),
                        empty_iter())
     self.assertEqual(result, [])
Beispiel #28
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'])
 def test_appending_to_immutable_sequence_raises_attribute_error(self):
     with self.assertRaises(AttributeError):
         transduce(Transducer,
                   appending(),
                   (23, 78),
                   init=tuple())
Beispiel #30
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)])