Skip to content

Instantly share code, notes, and snippets.

@littleq0903
Created April 10, 2014 16:55
Show Gist options
  • Save littleq0903/10401809 to your computer and use it in GitHub Desktop.
Save littleq0903/10401809 to your computer and use it in GitHub Desktop.
# 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
@timtan
Copy link

timtan commented Apr 11, 2014

Cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment