[New-bugs-announce] [issue13430] Add a curry function to the functools module

Marko Nervo report at bugs.python.org
Fri Nov 18 17:01:34 CET 2011


New submission from Marko Nervo <marko at python.it>:

I think it would be very usefull to add a curry function to the functools module. It should be like this.


def curry(func, *args, **kwargs):

    if (len(args) + len(kwargs)) >= func.__code__.co_argcount:

        return func(*args, **kwargs)

    return (lambda *x, **y: curry(func, *(args + x), **dict(kwargs, **y)))


This function allows you to create curried functions or methods in two main ways:


(1)


>>> def adder(x, y, z):
...     return (x + y + z)
>>> adder = curry(adder)


(2)


>>> @curry
... def adder(x, y, z):
...     return (x + y + z)


Curried functions could be used as follow.


>>> adder(2, 3, 4)
>>> adder(2, 3)(4)
>>> adder(2)(3)(4)
>>> adder(z = 4)(2, 3)  # etc, etc, etc...


Best regards,
Marco.

----------
components: Library (Lib)
messages: 147882
nosy: collinwinter, eric.araujo, gregory_p, markonervo, rhettinger, serprex
priority: normal
severity: normal
status: open
title: Add a curry function to the functools module
type: feature request
versions: Python 2.7, Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13430>
_______________________________________


More information about the New-bugs-announce mailing list