[SOLVED] How to consume/chain functions "Pythonically"?

Issue

I have a set of functions f(x), g(x), s(x), t(x).

They are designed to be chained together like this: f(g(s(t(x))))

The functions are designed so they can be chained in any sequence. Another part of my code builds the sequence in a list: [f, g, s, t]

Is there an elegant way to take the list and chain the function calls, getting the output of the last result?

Now obviously I can do it using an outside counter-like variable which tracks the output and a simple for loop increment index, but I was wondering if there is an more Pythonic way to do this?

Solution

Reduce is the perfect answer (though we need to start with the innermost function, thus reversed):

import functools

functions = [f, g, s, t]
y = functools.reduce(lambda a, f: f(a), reversed(functions), x)

EDIT: With constant parameters, as easy as this:

p1, p2 = 5, 42
y = functools.reduce(lambda a, f: f(a, p1, p2), reversed(functions), x)

Answered By – Amadan

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published.