Skip to content

Instantly share code, notes, and snippets.

@Lucretiel
Last active December 15, 2015 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lucretiel/5293606 to your computer and use it in GitHub Desktop.
Save Lucretiel/5293606 to your computer and use it in GitHub Desktop.
Decorator to enable tail recursion in python.
from functools import wraps
class tail(object):
def __init__(self, *args, **kwargs):
try:
self.func = args[0]._tail_function
except AttributeError:
self.func = args[0]
self.args = args[1:]
self.kwargs = kwargs
def __call__(self):
return self.func(*self.args, **self.kwargs)
def tail_recursive(func):
@wraps(func)
def wrapper(*args, **kwargs):
f = func(*args, **kwargs)
while isinstance(f, tail):
f = f()
return f
wrapper._tail_function = func
return wrapper
#Example
@tail_recursive
def rec(x):
if x <= 0:
return 1
else:
return tail(rec, x-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment