Could you please help me? I would like to write function which successively applies several functions to some iterable object and return list of results. I do not want to create temporary list object on each iteration step. Goal is to use lazy evaluation.
def applay(iterable, functions):
res = iterable
for f in functions:
res = (f(i) for i in res)
return list(res)
print applay([1,2,3], [lambda x: x+1, lambda y: y+2])
This does not work as f inside of each generator bind to the last function from function list. Can I avoid this python feature?
Look like I have found solution:
from itertools import repeat
from functools import partial
def applay(iterable, functions):
res = arg_to_iter(iterable)
for cur_f in functions:
res = (f(r) for r, f in zip(res, partial(repeat, cur_f)()))
return list(res)
print applay([1,2,3], [lambda x: x+1, lambda y: y+2])