Last active
December 15, 2015 16:59
-
-
Save Lucretiel/5293606 to your computer and use it in GitHub Desktop.
Decorator to enable tail recursion in python.
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
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