What is Currying?
Currying is the technique of converting a function that takes multiple arguments into a nested sequence of functions that each take a single argument.
Example of Currying
// Original Function
function multiply(a, b, c) {
return a * b * c;
}
multiply(1, 2, 3); // $: 6
// Curried Version
function multiply(a) {
return (b) => {
return (c) => {
return a * b * c;
};
};
}
multiply(1)(2)(3); // $: 6
// ES6 Curried Version
const multiply = (a) => (b) => (c) => a * b * c;
multiply(1)(2)(3); // $: 6