Skip to content

Instantly share code, notes, and snippets.

@xhluca
Last active September 7, 2023 20:24
Show Gist options
  • Save xhluca/230b5a21cf7f6942f92cb0d284b3a07e to your computer and use it in GitHub Desktop.
Save xhluca/230b5a21cf7f6942f92cb0d284b3a07e to your computer and use it in GitHub Desktop.
Pythonn currying with functools partial
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)
@xhluca
Copy link
Author

xhluca commented Sep 6, 2023

Couldn't get the str and repr to be assigned correctly. Wish functools.wraps but couldn't get it to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment