I am trying to use currying to make a simple functional add in Python. I found this curry decorator here .
def curry(func):
def curried(*args, **kwargs):
if len(args) + len(kwargs) >= func.__code__.co_argcount:
return func(*args, **kwargs)
return (lambda *args2, **kwargs2:
curried(*(args + args2), **dict(kwargs, **kwargs2)))
return curried
@curry
def foo(a, b, c):
return a + b + c
Now this is great because I can make some simple curries:
>>> foo(1)(2, 3)
6
>>> foo(1)(2)(3)
6
But this only works for exactly three variables. How to write the function foo so that it can accept any number of variables and still be able to curry the result? I tried a simple solution to use * args, but that did not work.
Edit: I looked at the answers, but still cannot figure out how to write a function that can work, as shown below:
>>> foo(1)(2, 3)
6
>>> foo(1)(2)(3)
6
>>> foo(1)(2)
3
>>> foo(1)(2)(3)(4)
10
source
share