def create3(): Observable.create(throw_error).transduce( compose( filtering(even), mapping(mul10)) ).subscribe() self.assertRaises(RxException, create3)
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
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_double(self): """ compose(f, g)(x) -> f(g(x)) """ f = lambda x: x * 2 g = lambda x: x + 1 c = compose(f, g) self.assertSequenceEqual([f(g(x)) for x in range(100)], [c(x) for x in range(100)])
def test_single(self): """ compose(f)(x) -> f(x) """ f = lambda x: x * 2 c = compose(f) # We can't test the equivalence of functions completely, so... self.assertSequenceEqual([f(x) for x in range(1000)], [c(x) for x in range(1000)])
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_triple(self): """ compose(f, g, h)(x) -> f(g(h(x))) """ f = lambda x: x * 2 g = lambda x: x + 1 h = lambda x: x - 7 c = compose(f, g, h) self.assertSequenceEqual([f(g(h(x))) for x in range(100)], [c(x) for x in range(100)])
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()), iterable=range(20)) expected = [16, 36, 49] for r, e in zip(result, expected): self.assertEqual(r, e)
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): result = transduce( transducer=compose( mapping(lambda x: x * x), filtering(lambda x: x % 5 != 0), taking(6), dropping_while(lambda x: x < 15), distinct(), ), iterable=range(20), ) expected = [16, 36, 49] for r, e in zip(result, expected): self.assertEqual(r, e)
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' ])
async def counter(delay, to): # from transducer.coop import transduce # from transducer.reducers import effecting # await transduce( # transducer=compose( # enumerating(), # windowing(3) # ), # reducer=effecting(print), # aiterable=ticker(delay, to) # ) from transducer.lazy_coop import transduce async for item in transduce(transducer=compose(enumerating(), windowing(3)), aiterable=ticker(delay, to)): print(item)
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())
def create(): Observable.throw_exception(RxException()).transduce( compose( filtering(even), mapping(mul10)) ).subscribe(noop, throw_error)
def create2(): Observable.empty().transduce( compose( filtering(even), mapping(mul10)) ).subscribe(noop, noop, throw_error)
def create(): return xs.transduce(compose(filtering(even), mapping(mul10)))
def create3(): Observable.create(throw_error).transduce( compose(filtering(even), mapping(mul10))).subscribe() self.assertRaises(RxException, create3)
def create2(): Observable.empty().transduce( compose(filtering(even), mapping(mul10))).subscribe_(noop, noop, throw_error)
def create(): Observable.throw(RxException()).transduce( compose(filtering(even), mapping(mul10))).subscribe_(noop, throw_error)
import cosmic_ray.commands import cosmic_ray.counting import cosmic_ray.modules import cosmic_ray.worker from cosmic_ray.testing.test_runner import TestOutcome from cosmic_ray.timing import Timer from cosmic_ray.util import redirect_stdout from cosmic_ray.work_db import use_db, WorkDB LOG = logging.getLogger() REMOVE_COMMENTS = mapping(lambda x: x.split('#')[0]) REMOVE_WHITESPACE = mapping(str.strip) NON_EMPTY = filtering(bool) CONFIG_FILE_PARSER = compose(REMOVE_COMMENTS, REMOVE_WHITESPACE, NON_EMPTY) def _load_file(config_file): """Read configuration from a file. This reads `config_file`, yielding each non-empty line with whitespace and comments stripped off. """ with open(config_file, 'rt', encoding='utf-8') as f: yield from transducer.lazy.transduce(CONFIG_FILE_PARSER, f) @dsc.command('load') def handle_load(config): """usage: cosmic-ray load <config-file>
import cosmic_ray.counting import cosmic_ray.modules import cosmic_ray.json_util import cosmic_ray.worker import cosmic_ray.testing import cosmic_ray.timing from cosmic_ray.work_db import use_db, WorkDB LOG = logging.getLogger() REMOVE_COMMENTS = mapping(lambda x: x.split('#')[0]) REMOVE_WHITESPACE = mapping(str.strip) NON_EMPTY = filtering(bool) CONFIG_FILE_PARSER = compose(REMOVE_COMMENTS, REMOVE_WHITESPACE, NON_EMPTY) def _load_file(config_file): """Read configuration from a file. This reads `config_file`, yielding each non-empty line with whitespace and comments stripped off. """ with open(config_file, 'rt', encoding='utf-8') as f: yield from transducer.lazy.transduce(CONFIG_FILE_PARSER, f) def handle_help(config): """usage: cosmic-ray help [<command>]