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.
As input, you use the result of f2(...) . As input for this function, you use the result of f1() . This way, you can chain three or more functions by using the pattern f3(f2(f1())) .
The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function. When the instance is called as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.
Fold while calling.
destination = reduce((lambda x, y: y(x)), function_list, origin)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With