Skip to content

Instantly share code, notes, and snippets.

@L3viathan
Last active June 15, 2018 07:13
Show Gist options
  • Select an option

  • Save L3viathan/71caebb8052760653ee1e10fccfd67e3 to your computer and use it in GitHub Desktop.

Select an option

Save L3viathan/71caebb8052760653ee1e10fccfd67e3 to your computer and use it in GitHub Desktop.
Currying decorator
from functools import partial
def curry(fn, argcount=None):
if not callable(fn):
return partial(curry, argcount=fn)
argcount = argcount or fn.__code__.co_argcount
if argcount < 2:
return fn
def wrapper(first):
return curry(partial(fn, first), argcount-1)
return wrapper
@curry
def test1(a, b, c):
return a, b, c
@curry(4)
def test2(a, *rest):
return a, rest
if __name__ == '__main__':
print(test1(1)(2)(3))
print(test2(1)(2)(3)(4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment