/apply_many.py Secret
Last active
November 9, 2022 06:19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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