Python 有没有确切的功能性咖喱替代品?

Python 有没有确切的功能性咖喱替代品?,python,Python,我试图在Python2.6运行时上运行此代码段 from functional import * taxcalc = lambda income,rate,deduct: (income-(deduct))*rate taxCurry = curry(taxcalc) taxCurry = taxCurry(50000) taxCurry = taxCurry(0.30) taxCurry = taxCurry(10000) print "Curried taxes due =",taxCu

我试图在Python2.6运行时上运行此代码段

from functional import *

taxcalc = lambda income,rate,deduct: (income-(deduct))*rate

taxCurry = curry(taxcalc)
taxCurry = taxCurry(50000)
taxCurry = taxCurry(0.30)
taxCurry = taxCurry(10000)
print "Curried taxes due =",taxCurry

print "Curried expression taxes due =", \
      curry(taxcalc)(50000)(0.30)(10000)
好的,我从中了解到,functional重命名为functools,curry重命名为partial,但仅仅进行重命名并没有帮助。我得到一个错误:

taxCurry = taxCurry(50000)
TypeError: <lambda>() takes exactly 3 arguments (1 given)
有没有更好的方法来保存原始示例的机制?最后,最初的示例是真正的curry,还是只是部分应用?(根据)


感谢您的时间

我想他们之所以更改它,是因为Python是动态类型的。这意味着,如果出现任何错误,调试原始的
curry
代码将非常困难-比在Haskell这样的语言中直接得到一个不错的类型错误要困难得多。因此,我认为用更明确的
partial
版本取代它是一个合理的决定(在我看来更像python)


您的示例也有点奇怪,因为您只是将部分应用的函数重新指定给相同的名称。通常,部分应用的函数将被赋予另一个函数。至少这是我能想到的Python中唯一合理的用例。

curry
的实现应该是一个替代品

$ pip install toolz
>>> from toolz import curry
我写了一篇很好的文章:

def curry(func):
    """
    Decorator to curry a function, typical usage:

    >>> @curry
    ... def foo(a, b, c):
    ...    return a + b + c

    The function still work normally:
    >>> foo(1, 2, 3)
    6

    And in various curried forms:
    >>> foo(1)(2, 3)
    6
    >>> foo(1)(2)(3)
    6

    This also work with named arguments:
    >>> foo(a=1)(b=2)(c=3)
    6
    >>> foo(b=1)(c=2)(a=3)
    6
    >>> foo(a=1, b=2)(c=3)
    6
    >>> foo(a=1)(b=2, c=3)
    6

    And you may also change your mind on named arguments,
    But I don't know why you may want to do that:
    >>> foo(a=1, b=0)(b=2, c=3)
    6

    Finally, if you give more parameters than expected, the exception
    is the expected one, not some garbage produced by the currying
    mechanism:

    >>> foo(1, 2)(3, 4)
    Traceback (most recent call last):
       ...
    TypeError: foo() takes exactly 3 arguments (4 given)
    """
    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


if __name__ == "__main__":
    import doctest
    doctest.testmod()

这个例子重要吗?或者你有一些真实的例子,可以揭示一些有用的咖喱?谢谢
def curry(func):
    """
    Decorator to curry a function, typical usage:

    >>> @curry
    ... def foo(a, b, c):
    ...    return a + b + c

    The function still work normally:
    >>> foo(1, 2, 3)
    6

    And in various curried forms:
    >>> foo(1)(2, 3)
    6
    >>> foo(1)(2)(3)
    6

    This also work with named arguments:
    >>> foo(a=1)(b=2)(c=3)
    6
    >>> foo(b=1)(c=2)(a=3)
    6
    >>> foo(a=1, b=2)(c=3)
    6
    >>> foo(a=1)(b=2, c=3)
    6

    And you may also change your mind on named arguments,
    But I don't know why you may want to do that:
    >>> foo(a=1, b=0)(b=2, c=3)
    6

    Finally, if you give more parameters than expected, the exception
    is the expected one, not some garbage produced by the currying
    mechanism:

    >>> foo(1, 2)(3, 4)
    Traceback (most recent call last):
       ...
    TypeError: foo() takes exactly 3 arguments (4 given)
    """
    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


if __name__ == "__main__":
    import doctest
    doctest.testmod()