21

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?
4
  • 2
    Why would your decision whether or not to curry a function have anything to do with whether you know the name ahead of time? Whenever you could call curry2(f), you could call f(x, y) just fine without currying it. Jul 22, 2014 at 8:11
  • @user2357112 I see your point. Aug 3, 2014 at 8:32
  • @Trilarion let me first understand, when to use function currying, then i would think of comparing Function Currying and partial application Aug 3, 2014 at 8:41

3 Answers 3

30

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

3
  • 1
    What is the advantage of having specialised functions(for example, line_through_zero in your answer)? Now, i think, currying could be useful, in a scenario where we have 3 parameters(say) and first parameter is ready(say) so we create specialised function after appying one parameter and wait for other two parameters from third party modules(say). Do you think we could have such scenarios? Jul 23, 2014 at 15:19
  • i mean, If there are two arguments for a function, then first argument is available at place1, then partially-applied function is created and passed on multiple times to place2 where second argument is available.am i correct? Jul 23, 2014 at 15:36
  • 1
    "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." -- This way of stating it, while true, buries the lede imo. The main advantage that it allows the creation of new functions through composition.
    – Jonah
    Oct 18, 2017 at 16:46
5

Currying has at-least two advantages I can think of:

1) It keeps your code (and in turn your thinking) DRY.

Say you are have a function like:

def call_me(context, args):
    ...

by currying you can get a specialized function for that context which can be tossed around etc. You don't have to repeat the context again.

2) Thinking in terms of a single input function is much easier than n arguments; this can be debatable at times though.

See also: What is the difference between currying and partial application?

0

In many ways, this combination of currying and the partial application of arguments to functions emulates what in Object Oriented Programming we would call the "Factory Pattern". Currying lets the programmer to create specialized functions, as others have pointed out in this forum. For instance, check this (not efficient by elegant) implementation of quicksort below:

leq = lambda x: lambda y: x <= y
gth = lambda x: lambda y: x > y

def qsort(L):
    if L:
        smalls = filter(gth(L[0]), L[1:])
        bigs = filter(leq(L[0]), L[1:])
        return qsort(smalls) + [L[0]] + qsort(bigs)
    else:
        return L

print(qsort([2, 4, 1, 4, 2]))

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.