Skip to content

Instantly share code, notes, and snippets.

@Kha
Created January 20, 2013 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kha/4576171 to your computer and use it in GitHub Desktop.
Save Kha/4576171 to your computer and use it in GitHub Desktop.
Minimally invasive function for heap recursion
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