def test_chained_transducers(self): input = [0.0, 0.2, 0.8, 0.9, 1.1, 2.3, 2.6, 3.0, 4.1] output = CollectingSink() iterable_source(iterable=input, target=transduce( compose(pairwise(), mapping(lambda p: p[1] - p[0]), filtering(lambda d: d < 0.5), mapping(lambda _: "double-click")), target=output())) result = list(output) self.assertListEqual(result, ['double-click', 'double-click', 'double-click', 'double-click', 'double-click'])
def test_chained_transducers(self): input = [0.0, 0.2, 0.8, 0.9, 1.1, 2.3, 2.6, 3.0, 4.1] output = CollectingSink() iterable_source(iterable=input, target=transduce(compose( pairwise(), mapping(lambda p: p[1] - p[0]), filtering(lambda d: d < 0.5), mapping(lambda _: "double-click")), target=output())) result = list(output) self.assertListEqual(result, [ 'double-click', 'double-click', 'double-click', 'double-click', 'double-click' ])
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_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)])