Skip to content

Instantly share code, notes, and snippets.

@jude90
Created April 16, 2014 13:30
Show Gist options
  • Save jude90/10875401 to your computer and use it in GitHub Desktop.
Save jude90/10875401 to your computer and use it in GitHub Desktop.
Trampoline -- python 尾递归优化方案 (并不理想)
from types import GeneratorType
def trampo(func):
def _(*args, **kwargs):
g = func(*args, **kwargs)
while isinstance(g, GeneratorType):
g = g.next()
return g
return _
@trampo
def fibo(count, cur=0, next_=1):
if count <= 1:
yield cur
else:
yield fibo(count-1, next_, cur+ next_)
@jude90
Copy link
Author

jude90 commented Apr 16, 2014

参考了Bouncing Python's Generators With a Trampoline , 但实际上并不成功。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment