Skip to content

Instantly share code, notes, and snippets.

@KC-Liu
Forked from littleq0903/curry_test.py
Created August 2, 2014 05:04
Show Gist options
  • Save KC-Liu/6a7988cf40ec8cc8fb9f to your computer and use it in GitHub Desktop.
Save KC-Liu/6a7988cf40ec8cc8fb9f 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment