Is there a shortcut in python for applying the same function multiple times to a variable (and its output)?
Something like:
# n times foo(x)
instead of
foo(foo(foo...(foo(x))))...)
for i in range(n):
x = foo(x)
If you need to preserve x, use a different name
baz = x
for i in range(n):
baz = foo(baz)
If you are desperate for a shortcut (eg. for codegolf)
reduce(lambda x,y:foo(x),[x]*n)
One of the ways i can think of is creating a generic recursive function to do this
def repeatX(foo, output, count):
if not count:
return output
else:
return repeatX(foo, foo(output), count -1)
forloop is best for this.