我什么时候应该在Python中使用函数currying?

时间:2022-07-27 22:02:46

When should I write my functions in curried form? does not match my thought, need to correct myself.

什么时候应该以咖喱形式写我的功能?不符合我的想法,需要纠正自己。

As part of my learning link, this is what I understand from function currying. Below is one example:

作为我学习链接的一部分,这是我从函数currying中理解的。以下是一个例子:

def curry2(f):
    """Returns a function g such that g(x)(y) == f(x, y)

    >>> from operator import add
    >>> add_three = curry2(add)(3)
    >>> add_three(4)
    """
    def g(x):
        def h(y):
            return f(x, y)
        return h
    return g

In any application, if I know that the number of arguments are fixed (say 2 arguments) and function name is normalise_range(say), then I will define def normalise_range(x, y): function and use it in my application directly by calling normalise_range(x, y).

在任何应用程序中,如果我知道参数的数量是固定的(比如2个参数)而函数名是normalise_range(比如说),那么我将定义def normalise_range(x,y):function并直接通过调用在我的应用程序中使用它normalise_range(x,y)。

In any application, if I know that, the number of arguments are fixed (say 2 arguments), but the function name is varying (can be normalise_range/average/I don't know..), then I will use def curry2(f): as shown above, which will accept all functions that take two arguments (fixed).

在任何应用程序中,如果我知道,参数的数量是固定的(比如2个参数),但函数名称是变化的(可以是normalise_range / average /我不知道..),那么我将使用def curry2( f):如上所示,它将接受带有两个参数(固定)的所有函数。

My question:

我的问题:

  1. Is my understanding correct?
  2. 我的理解是否正确?
  3. If yes, can we think of currying for functions of variable number of arguments?
  4. 如果是,我们可以考虑为可变数量的参数的函数进行求解吗?

1 个解决方案

#1


8  

The purpose of function currying is to easily get specialized functions from more general functions. You achieve this by pre-setting some some parameters at a different time and keeping them fixed afterwards.

函数currying的目的是从更一般的函数中轻松获得专门的函数。您可以通过在不同时间预先设置一些参数并在之后保持固定来实现此目的。

It has nothing to do with the naming. In Python you can rename a variable/function easily at all times.

它与命名无关。在Python中,您可以随时轻松地重命名变量/函数。

Example:

例:

def simple_function(a):
    def line(b=0):
        def compute(x):
            return [a + b * xi for xi in x]
        return compute
    return line

x = range(-4, 4, 1)
print('x {}'.format(list(x)))
print('constant {}'.format(simple_function(3)()(x)))
print('line {}'.format(simple_function(3)(-2)(x)))

gives

x [-4, -3, -2, -1, 0, 1, 2, 3]
constant [3, 3, 3, 3, 3, 3, 3, 3]
line [11, 9, 7, 5, 3, 1, -1, -3]

Now this was not yet that exciting. It only replaced functions calls of type f(a,b,c) with calls of type f(a)(b)(c) which might even be seen as the less elegant style in Python.

现在这还不是那么令人兴奋。它只用类型为f(a)(b)(c)的调用替换了f(a,b,c)类型的函数调用,甚至可能被认为是Python中不太优雅的样式。

But it allows you to do:

但它允许你这样做:

line_through_zero = simple_function(0)
print('line through zero {}'.format(line_through_zero(1)(x))) # only slope and x

which gives

这使

line through zero [-4, -3, -2, -1, 0, 1, 2, 3]

So the advantage of currying is that you get specialized functions that have fixed parameters and can be used instead of writing the more general form and setting the parameters fixed at each single call.

因此,currying的优势在于您可以获得具有固定参数的专用函数,而不是编写更通用的表单并设置每次调用时固定的参数。

Alternatives to currying are: partial, lambda and default parameters. So in practice currying might be useful but you can also get around it if you want.

currying的替代方法是:partial,lambda和default参数。所以在实践中,currying可能很有用,但如果你愿意,你也可以绕过它。

See also Currying in Python

另请参阅Python中的Currying

#1


8  

The purpose of function currying is to easily get specialized functions from more general functions. You achieve this by pre-setting some some parameters at a different time and keeping them fixed afterwards.

函数currying的目的是从更一般的函数中轻松获得专门的函数。您可以通过在不同时间预先设置一些参数并在之后保持固定来实现此目的。

It has nothing to do with the naming. In Python you can rename a variable/function easily at all times.

它与命名无关。在Python中,您可以随时轻松地重命名变量/函数。

Example:

例:

def simple_function(a):
    def line(b=0):
        def compute(x):
            return [a + b * xi for xi in x]
        return compute
    return line

x = range(-4, 4, 1)
print('x {}'.format(list(x)))
print('constant {}'.format(simple_function(3)()(x)))
print('line {}'.format(simple_function(3)(-2)(x)))

gives

x [-4, -3, -2, -1, 0, 1, 2, 3]
constant [3, 3, 3, 3, 3, 3, 3, 3]
line [11, 9, 7, 5, 3, 1, -1, -3]

Now this was not yet that exciting. It only replaced functions calls of type f(a,b,c) with calls of type f(a)(b)(c) which might even be seen as the less elegant style in Python.

现在这还不是那么令人兴奋。它只用类型为f(a)(b)(c)的调用替换了f(a,b,c)类型的函数调用,甚至可能被认为是Python中不太优雅的样式。

But it allows you to do:

但它允许你这样做:

line_through_zero = simple_function(0)
print('line through zero {}'.format(line_through_zero(1)(x))) # only slope and x

which gives

这使

line through zero [-4, -3, -2, -1, 0, 1, 2, 3]

So the advantage of currying is that you get specialized functions that have fixed parameters and can be used instead of writing the more general form and setting the parameters fixed at each single call.

因此,currying的优势在于您可以获得具有固定参数的专用函数,而不是编写更通用的表单并设置每次调用时固定的参数。

Alternatives to currying are: partial, lambda and default parameters. So in practice currying might be useful but you can also get around it if you want.

currying的替代方法是:partial,lambda和default参数。所以在实践中,currying可能很有用,但如果你愿意,你也可以绕过它。

See also Currying in Python

另请参阅Python中的Currying