If I have a very simple (although possibly very complex) function generator in Python 2.7, for example:
def accumulator(): x = yield 0 while True: x += yield x
What can be used, for example:
>>> a = accumulator() >>> a.send(None) 0 >>> a.send(1) 1 >>> a.send(2) 3 >>> a.send(3) 6
What will be a simple wrapper for another function generator that produces the same result except multiplying by 2? The above function generator is simple, but please assume that it is too complicated to copy-paste. I am trying something like:
def doubler(): a = accumulator() a.send(None) y = yield 0 while True: y = 2 * a.send(yield y)
Or, introducing something simpler:
def doubler(): a = accumulator() a.send = lambda v: 2 * super(self).send(v) return a
Both of them are terribly broken, so I will not pass syntax errors, but this can illustrate what I'm trying to do.
Ideally, I would like to get something like:
>>> d = doubler() >>> d.send(None) 0 >>> d.send(1) 2 >>> d.send(2) 6 >>> d.send(3) 12
The results are the same as the original, except for doubling.
I try to avoid duplication of a very complex function generator in order to create an identical result, except for scaling using a known factor.
The second generator will ultimately have a different input stream, so I cannot just use the result from the first generator and double it. I need a second independent generator to wrap the first.
The input stream is indefinite, so it is not possible to generate the entire sequence and then convert.
I seem to want to display or embed these function generators, but I'm not sure about the appropriate jargon, and so I havenβt been to Google anywhere.