Created
September 17, 2014 06:18
-
-
Save rockneurotiko/34f9197cca00c218d701 to your computer and use it in GitHub Desktop.
tail_rec_print
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
import functools | |
def tail_call(func): | |
def _optimize_partial(*args, **kwargs): | |
old_reference = func.__globals__[func.__name__] | |
func.__globals__[func.__name__] = functools.partial(functools.partial, func) | |
to_execute = functools.partial(func, *args, **kwargs) | |
while isinstance(to_execute, functools.partial): | |
to_execute = to_execute() | |
func.__globals__[func.__name__] = old_reference | |
return to_execute | |
functools.update_wrapper(_optimize_partial, func) | |
return _optimize_partial | |
@tail_call | |
def t(n = 1): | |
print("%s - Hello" % n) | |
return t(n+1) if n < 1024 else 0 | |
t() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment