Jan-27-2026, 09:32 PM
(This post was last modified: Jan-27-2026, 10:31 PM by Gribouillis.)
Currying is the process of transforming a function with signature e.g.
The code below automates the process of currying the functions. It also provides a decorator to curryfy the functions as soon as they are defined.
(X, Y, Z) -> T into a function having signature X -> (Y -> (Z -> T)). For example the following spam() functiondef spam(x, y, z):
...is normally called with the syntax spam(7, 4, 3). A curried version would be called with the syntax spam(7)(4)(3).The code below automates the process of currying the functions. It also provides a decorator to curryfy the functions as soon as they are defined.
__version__ = '2026.01.27'
class Curry:
def __init__(self, arity, func, args: tuple=()):
self.arity = arity
self.func = func
self.args = args
def __repr__(self):
return f'Curry({self.arity!r}, {self.func!r}, {self.args!r})'
def __call__(self, arg):
if self.arity == 1:
return self.func(*self.args, arg)
return Curry(self.arity - 1, self.func, self.args + (arg,))
curry = Curry(2, Curry)
@curry(3)
def spam(a, b, c):
return ('result', a, b, c)
print(spam(7)(4)(3)) # prints ('result', 7, 4, 3)Of course there are many modules devoted to this in the Cheese Shop.
« We can solve any problem by introducing an extra level of indirection »
