Reputation: 173
I want to get the result of a chain of computations from an initial value. I'm actually using the following code:
def function_composition(function_list, origin):
destination = origin
for func in function_list:
destination = func(destination)
return destination
With each function in function_list
having a single argument.
I'd like to know if there is a similar function in python standard library or a better way (example: using lambdas) to do this.
Upvotes: 17
Views: 6594
Reputation: 798606
Fold while calling.
destination = reduce((lambda x, y: y(x)), function_list, origin)
Upvotes: 20