UI Development

## Currying Functions

Currying is a concept of functional programming in various programming languages. It was named after the logician ‘Haskell Brooks Curry’.

### Definition:

Currying is the process of chaining functions by returning a series of nested functions. Multiple arguments can be passed into a single function call by chaining the function calls one after another and passing those multiple arguments in different calls. The number of arguments passed into a function call is known as ‘arity’.

So,
(x, y) → x * y
can be curried into
x → (y → x * y)

In other words, currying is when a function called with one argument and returns another function which is called with the next argument and that function returns another function which is called with the next argument and so on instead of passing all the arguments in a single function call.

##### Examples:

The process of currying can easily be explained with a few examples. Let’s take a simple function and convert it into a curried function:

Here the function gets all three arguments at the same time and hence does not follow the concept of currying. Now let’s convert it into a curried function.

The above function takes one argument in every function call and hence is a perfect example of a curried function.

Let’s take another example of curried function:

The above example shows a curried function where we are calculating the average of any numbers and passing each number in separate function calls. The curriedAvg function takes list of arguments and saves the average of those numbers in property value. If more arguments are passed to the function, it will re-calculate the average every time and give the desired output.

Example of passing one function inside another function using currying:

The above ‘curryFn’ function is a reusable function in which we can pass any function with multiple arguments. Inside the curryFn, we have a _curriedFn which takes the depth of the function and values passed along for computation one after the other until all values are passed. In curriedMult we are passing mult function inside curryFn and then defining in a new function multTen which passes a single argument 10 to the curriedMult function and now when we pass the second argument to multTen function call it gives us the computed result. The same process is followed in curriedSI function.

Example of currying functions using bind():

In the above example, we can pass all three arguments at a single time as well as we can curry above function with one argument at a time. If we pass a single argument in the first function call, the bind will help in calling the other function later when the second and third arguments are passed.

Example of currying functions using apply():

In the above example, we are calculating the percentage of a student by passing marks for different subjects in the function call. We can pass marks for any number of subjects in one function call and repeat the calling process with the percentage of other subjects. Ideally, for currying we should pass the marks for one subject in one function call, but in the above example, we are passing an array of marks in one function call and then another array of marks in next function call. Then we are concatenating both the arrays and passing the concatenated array to the main function using the apply method.

## Conclusion:

Currying doesn’t call a function, it just transforms it by returning other functions. The concept of currying is a function that returns another function until it receives all the arguments it needs and every function takes a single argument. Currying helps in the reusability of functions and helps in better code readability. Currying also makes the functions more manageable.

## References:

https://blog.bitsrc.io/understanding-currying-in-javascript-ceb2188c339