I want a function named times(), in order to make:
times(func,2) equivalent to lambda x:func(func(x))
and times(func,5) equivalent to lambda x:func(func(func(func(func(x)))))
Is there such a tool in Python? What would the code looks like if I want to write it by myself?
Thanks!
I'd suggest to call this power(), since this is actually the nth power of a function.  There is no such thing in the standard library, but you can easily implement it yourself:
def power(f, n):
    def wrapped(x):
        for i in range(n):
            x = f(x)
        return x
    return wrapped
                        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