Created
January 20, 2013 01:26
-
-
Save Kha/4576171 to your computer and use it in GitHub Desktop.
Minimally invasive function for heap recursion
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 heap_rec(f): | |
stack = [f] | |
result = None | |
while stack: | |
try: | |
stack.append(stack[-1].send(result)) | |
result = None | |
except StopIteration as ex: | |
result = ex.value | |
stack.pop() | |
return result | |
def fib(n): | |
if n < 2: return n | |
return (yield fib(n-1)) + (yield fib(n-2)) | |
print(heap_rec(fib(20))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment