-
-
Save KC-Liu/6a7988cf40ec8cc8fb9f to your computer and use it in GitHub Desktop.
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
# generic currying decorator | |
# right currying behavior | |
from functools import partial | |
""" | |
Curry decorator | |
""" | |
def curry(func, args_num=1, args_total=None): | |
if not args_total: args_total = func.func_code.co_argcount | |
def curried(arg1): | |
next_func = partial(func, arg1) | |
if args_num == args_total: | |
return next_func() | |
return curry(next_func, args_num+1, args_total) | |
return curried | |
if __name__ == '__main__': | |
@curry | |
def add(a, b, c, d, e, f, g): | |
return a + b + c + d + e + f + g | |
add12 = add(1)(2) | |
add1234 = add12(3)(4) | |
add1234567 = add1234(5)(6)(7) | |
print add1234567 # 28 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment