Currying is the technique of breaking down the evaluation of a function that takes multiple arguments into evaluating a sequence of single-argument functions.Currying is not only used in programming but in theoretical computer science as well. The reason is that it is often easier to transform multiple argument models into single argument models.Currying means rearranging a multiple-parameter function into a chain of functions applied to one argument. It is always possible to transform a function with multiple arguments into a chain of single-argument functions.Python is not equipped for this programming style. This means there are no special syntactical constructs available to support currying. On the other hand, Python is well suited to simulate this way of programming.
As we have already metnioned in the introduction, currying means transforming a function with multiple parameters into a chain of functions with one parameter.We will start with the simplest case, i.e. two parameters. Given is a function ff with two parameters xx and yy. We can curry the function in the following way:We have to find a function gg which returns a function hh when it is applied to the second parameter yy of ff. hh is a function which can be applied to the first parameter xx of ff, satisfying the condition
f(x,y)=g(y)(x)=h(x)f(x,y)=g(y)(x)=h(x)
Now we have a look at the general case of currying. Let us assume that we have a function ff with nn parameters:
f(x1,x2,…xn)f(x1,x2,…xn)
Currying leads to a cascade of functions:
fn−1=fn(xn)fn−1=fn(xn)
......
f1=f2(x2)f1=f2(x2)
f(x1,x2,…xn)=f1(x1)