Last active
June 15, 2018 07:13
-
-
Save L3viathan/71caebb8052760653ee1e10fccfd67e3 to your computer and use it in GitHub Desktop.
Currying decorator
This file contains hidden or 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
| 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