-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerics.py
63 lines (49 loc) · 1.72 KB
/
generics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from functools import reduce, singledispatch
from collections.abc import Hashable, Sequence, Generator
def coroutine(obj):
"""Ensure start of generator."""
# if isinstance(obj, Generator): # needs more general solution
# return obj
def _start(*args, **kwds):
cr = obj(*args, **kwds)
cr.send(None)
return cr
return _start
def send_to(target, break_on=None):
def source(src):
def captured(*args, **kwds):
for elt in iter(src(*args,**kwds), break_on):
target.send(elt)
return captured
return source
def start_gen(gen):
g = gen()
next(g)
return g
def pipethrough(elt, gens): # has to handle piplines also
"""Send 'elt' through given generators."""
# gens = list(map(start_gen, gens)) # have to encapsulate coroutine ensurement
return reduce(lambda elt, gen: gen.send(elt), gens, elt)
def consume(gen):
for _ in gen: pass
return None
# there is need to implement some 'compose' that could handle
# wrapped functions/gen expressions as partials and also
# coprocedures
@singledispatch # *gens or gens? how to handle both?
def compose(*gens):
"""Create pipeline from sequence of generators."""
# gens = list(map(coroutine, gens))
def _composition(src):
yield from map(lambda elt: pipethrough(elt, gens), src)
return _composition
# this functional implementation is a little bit slower than...
# for elt in src:
# for gen in gens:
# elt = gen().send(elt)
# yield elt
@compose.register
def starcompose(gens: Sequence):
yield from compose(*gens)
# some kind of branching/teeing
# yield from (gen().send(elt) for elt in src for gen in gens)