problem with curring in python
Antoon Pardon
antoon.pardon at vub.be
Tue Oct 22 04:58:26 EDT 2019
Using python 3.5
I have been experimenting with curried functions. A bit like in Haskell.
So I can write the following function:
def sum4(a, b, c, d):
return a + b + c + d
summing = curry(sum4)
print summing(1)(2)(3)(4) # this prints 10.
The problem is I need the signature of the original function in order to
know when to finally call the function and return the actual result.
However buildin functions don't have a signature. Here below is my
current experimental implementation. Any ideas for an other approach?
def curry(func, *args):
arg_len = len(signature(func).parameters)
if arg_len <= len(args):
return func(*args)
else:
return CurryType(func, arg_len, args)
class CurryType:
def __init__(self, func, arg_len, args):
self.func = func
self.arg_len = arg_len
self.args = list(args)
def __call__(self, *args):
args = self.args + list(args)
if self.arg_len <= len(args):
return self.func(*args)
else:
return CurryType(self.func, self.arg_len, args)
More information about the Python-list
mailing list