Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to call a chain of functions in python?

Tags:

python

I have a chain of operations which needs to occur one after the other and each depends on the previous function's output.

Like this:

out1 = function1(initial_input)
out2 = function2(out1)
out3 = function3(out2)
out4 = function4(out3)

and so on about 10 times. It looks a little ugly in the code.

What is the best way to write it? Is there someway to handle it using some functional programming magic? Is there a better way to call and execute this function chain?

like image 859
ComputerFellow Avatar asked Dec 07 '22 04:12

ComputerFellow


2 Answers

You can use functools.reduce:

out = functools.reduce(lambda x, y : y(x), [f1, f2, f3, f4], initial_value)

Quoting functools.reduce documentation:

Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.

Here, we use the fact that functions can be treated as any variable in Python, and an anonymous functions which simply does "apply x to function y".

This "reduce" operation is part of a very general pattern which have been applied successfully to parallelize tasks (see http://en.wikipedia.org/wiki/MapReduce).

like image 147
Nicolas Barbey Avatar answered Dec 26 '22 16:12

Nicolas Barbey


Use a loop:

out = initial_input
for func in [function1, function2, function3, function4]:
    out = func(out)
like image 39
falsetru Avatar answered Dec 26 '22 17:12

falsetru