Curry is one of the most asked interview question. Not just for interviews, we can use curry in real-world examples as well. So let's jump into the topic and explore it.
What is currying?
Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions.
So rather than having a function that looks like this-
someFunction('param1', 'param2', 'param3');
You end up with like this-
someFunction('param1')('param2')('param3');//currying
Let's understand what actually happens here-
What happens in the above code is that when you pass the parameter, this first function processes it and returns a function that processes the next parameter and so on.
When you have more than one argument, you are setting yourself for a potential side effect. When you curry a function, your first argument returns an expected outcome that proceeds to process your next argument and so on.
Curried function-
function someFunction(param1){
//do something
return (param2) => {
//do something
return (param3) => {
return finalResult;
}
}
}
The awesome part with curried functions is that you still have access to the functions inside the curried function.
Example of Currying
function multiply(x) {
return (y) => {
return (z) => {
return x * y * z
}
}
}
console.log(multiply(1)(2)(3)) // 6
Let’s understand how things work.
const multiply1 = multiply(1);
const multiply2 = multi1(2);
const result = multiply2(3);
console.log(result); // 6
Above code works same as multiply(1)(2)(3). I just divided it for better understanding.
First multiply1 will return -
return (y) => {
return (z) => {
return x * y * z
}
}
Next multiply2 will return-
return (z) => {
return x * y * z
}
And finally, result will do the final computation.
The last function only accepts ‘z’ but will perform operations with other variables whose enclosing scope has long since returned. This happens because of closures ( inner functions has access to its outer functions variables).
Why, how and where to use curried functions?
Let’s say we have a bunch of functions that has to be performed in a particular order. You can get all those functions inside a single function but that will make things hard to debug.
But currying lets you do this.
It lets you compose the sequence of your functions and ensures that the particular sequence is enforced and followed. The final output is only returned when all the dependencies have been passed through.
Let’s say we have a process to achieve-
check if a exists
---> do b
--->do c
So, your function will look something like this-
function curry(a) {
if (!a) throw error
return (b) => {
//do something
return (c) => {
//do something
}
}
}
curry(a)(b)(c)
You could merge all three functions into one, but that won’t check or detect issues at every step of the way. Currying does it in a elegant way.
Note- What if we have more arguments like 5 or 6 or even more? Then it would create more nesting. Hence we have to be selective while using currying.
Custom Curry Function-
function curry(f) {
return function (a) {
return function (b) {
return f(a, b);
};
};
}
const add = (a, b) => a + b;
const adder = curry(add)
That's currying for you. I know that there are lot more to the topic that can be explored. But I hope you all might have got a basic idea of what curry is.
Feel free to give your feedback. I will be more than happy to improve on it.
Thank you!!