[Python-ideas] (no subject)

Paul Moore p.f.moore at gmail.com
Wed May 6 20:44:25 CEST 2015


On 6 May 2015 at 17:23, Ivan Levkivskyi <levkivskyi at gmail.com> wrote:
> I should clarify why I would like to have the possibility to easily compose
> functions.
> I am a physicist (not a real programmer), and in my code I often compose
> functions.
>
> To do this I need to write something like
>
> def new_func(x):
>     return f(g(h(x)))
>
> This means I see f(g(h())) quite often and I would prefer to see f @ g @ h
> instead.

I appreciate that it's orthogonal to the proposal, but would a utility
function like this be useful?

def compose(*fns):
    def composed(x):
        for f in reversed(fns):
            x = f(x)
        return x
    return composed

comp = compose(f, g, h)
# comp(x) = f(g(h(x)))

Paul


More information about the Python-ideas mailing list