Skip to content

Instantly share code, notes, and snippets.

@showthesunli
Last active November 9, 2022 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save showthesunli/f20a028226af4dd084014f94d12bade0 to your computer and use it in GitHub Desktop.
Save showthesunli/f20a028226af4dd084014f94d12bade0 to your computer and use it in GitHub Desktop.
def apply_many(f):
"""Write a generator function apply_many that takes in a one-argument function f and at the nth yield,
it yields a fcuntion that applies f n times. For example, it first yield f(), f(f()), f(f(f())), etc ...
>>> f = lambda x: x*2
>>> gen = apply_many(f)
>>> [next(gen)(1) for _ in range(3)]
[2, 4, 8]
"""
yield f
for func in apply_many(f):
yield lambda arg: f(func(arg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment