Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

functional programming - When should I use function currying in Python?

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:

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).

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).

My question:

  1. Is my understanding correct?
  2. If yes, can we think of currying for functions of variable number of arguments?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.

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.

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.

See also Currying in Python


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...