Name: Anonymous 2007-09-28 2:20 ID:9iEWgcnk
Hey /prog/
Check out my tail call decorator for Python.
Check out my tail call decorator for Python.
def tailcall(f):
'''Decorator that implements tail call optimization.
Supports cooperative tail call - even when only one of the functions
is decorated, and all exceptions pass through it. You can tell the
functions that have been tail optimized because they have "tco" before
them in the stack frame.
The only caveat is that if you attempt to decorate a function that
isn't called in a tail recursive fashion from another decorated
function, you'll get wrong results.'''
tdata = [False] #[Optimizing?]
DO_CALL = object() #Call marker
def tco(*a, **aa):
if tdata[0]:
return DO_CALL, a, aa
else:
tdata[0] = True
ret = f(*a, **aa)
while type(ret) is tuple and ret and ret[0] is DO_CALL:
ret = f(*ret[1], **ret[2])
tdata[0] = False
return ret
tco.__doc__ = f.__doc__
return tco