Created
December 19, 2015 17:44
-
-
Save hsavit1/82b11b031df2dd087003 to your computer and use it in GitHub Desktop.
Tail Call Optimized Python Decorator
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
class TailPromise(object): | |
__metaclass__ = PromiseMetaClass | |
def __init__(self,func,args,kw): | |
self.__func = func | |
self.__args = args | |
self.__kw = kw | |
def __arginfo__(self): | |
return self.__args, self.__kw | |
def __func__(self): | |
return self.__func | |
def __force__(self): | |
return self.__func(*self.__args,**self.__kw) | |
def trampolined(g): | |
def func(*args,**kwargs): | |
old_trampolining = func.currently_trampolining | |
# if this is not the first call, and it is a tail call: | |
if (func.currently_trampolining != func): | |
# Set up the trampoline! | |
func.currently_trampolining = func | |
while 1: | |
res = g(*args,**kwargs) | |
if res.__class__ is TailPromise and res.__func__() is g: | |
# A tail recursion! | |
args,kwargs = res.__arginfo__() | |
else: | |
func.currently_trampolining = old_trampolining | |
return res | |
else: | |
return TailPromise(g,args,kwargs) | |
func.currently_trampolining = None | |
return func |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment