Last active
September 7, 2023 20:24
-
-
Save xhluca/230b5a21cf7f6942f92cb0d284b3a07e to your computer and use it in GitHub Desktop.
Pythonn currying with functools partial
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(func): | |
def curried(*args, **kwargs): | |
if args or kwargs: | |
return curry(partial(func, *args, **kwargs)) | |
else: | |
return func() | |
return curried | |
if __name__ == '__main__': | |
# Let's tets a function | |
def add(a, b, c): | |
"""Add three numbers""" | |
return a + b + c | |
cur_add = curry(add) | |
add_a1 = cur_add(a=1) | |
add_a2 = cur_add(a=2) | |
add_a1_b2 = add_a1(b=2) | |
add_a1_b3 = add_a1(b=3) | |
add_a2_b2 = add_a2(b=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Couldn't get the str and repr to be assigned correctly. Wish
functools.wraps
but couldn't get it to