Created
April 16, 2014 13:30
-
-
Save jude90/10875401 to your computer and use it in GitHub Desktop.
Trampoline -- python 尾递归优化方案 (并不理想)
This file contains hidden or 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
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_) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考了Bouncing Python's Generators With a Trampoline , 但实际上并不成功。