I have a pipeline
function that takes an arbitrary number of functions as arguments, it returns a single function helper
which contain one argument and this function in turn calls the pipeline
function with a single argument iteratively. Here is the code:
def pipeline(*funcs):
def helper(arg):
for func in funcs:
result = func(arg)
arg = result
return result
return helper
And here a test case:
fun = pipeline(lambda x: x * 3, lambda x: x + 1, lambda x: x / 2)
print(fun(3)) #should print 5.0
I was reading on a different question about generators and how they can be used to remember previous state here, and was wondering if I could reframe my current pipeline
function to use a generator instead. I need to remember the arg
for every func
call and currently I'm doing that by storing into a variable, however since generators can remember the last state I was wondering if I could use it instead.