Skip to content

Instantly share code, notes, and snippets.

@agoose77
Created August 27, 2017 23:47
Show Gist options
  • Save agoose77/4a09f7c44e1872df08642421e8f72526 to your computer and use it in GitHub Desktop.
Save agoose77/4a09f7c44e1872df08642421e8f72526 to your computer and use it in GitHub Desktop.
class Deferred:
def __init__(self, call, *args, **kwargs):
self.call = getattr(call, 'func', call)
self.args = args
self.kwargs = kwargs
def result(self):
return self.call(*self.args, **self.kwargs)
class Context:
def __init__(self):
self.depth = 0
def __bool__(self):
return self.depth > 0
def __enter__(self):
depth = self.depth
self.depth += 1
return depth
def __exit__(self, exc_type, exc_val, exc_tb):
self.depth -= 1
class TailCallDecorator:
def __init__(self, func):
self.func = func
self.ctx = Context()
def __call__(self, *args, **kwargs):
if self.ctx:
return Deferred(self.func, *args, **kwargs)
with self.ctx:
result = self.func(*args, **kwargs)
while isinstance(result, Deferred):
result = result.result()
return result
def tco(f):
return TailCallDecorator(f)
@tco
def fact(n, r=1):
if n <= 1:
return r
else:
return fact(n - 1, n * r)
print(fact(1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment