Last active
October 1, 2020 05:09
-
-
Save jeacom25b/300171c16a754b8b9ea91cb210c783c5 to your computer and use it in GitHub Desktop.
a simple curry 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 wraps | |
from inspect import signature | |
def curry(func): | |
if not callable(func): | |
raise TypeError('argumment must be callable') | |
sig = signature(func) | |
@wraps(func) | |
def wrapper(*args): | |
try: | |
sig.bind(*args) | |
except TypeError: | |
def keep_going(*args1): | |
return wrapper(*args, *args1) | |
return keep_going | |
return func(*args) | |
return wrapper | |
if __name__ == '__main__': | |
@curry | |
def test_fn(a, b, c): | |
print(a, b, c) | |
dogs_love = test_fn('dogs', 'love') | |
dogs_love('meat') | |
dogs_love('bones') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment