JavaScript Currying
By: Chris Hosler
Tags: Software Engineering, Javascript Tips, JavaScript, Software Development

Curry is delicious, however, that is not the kind of curry that we are talking about when I say Currying in JavaScript. In JavaScript currying, refers to taking a function with multiple arguments and transforming it into a series of nested functions. Basically, each nested function is going to take care of the next argument from our original function, so we can keep adding as many arguments as we have. It’s like starting a dish with only one or two spices and then adding more and more spice to the dish, to make it as spicy as you like. Let’s start with our multi-argument function first…

function addNumbers(a, b, c) {

return a + b + c;

} console.log(addNumbers(1, 2, 3)) // Outputs 6

And now let’s convert it into a simple curried function…

function addNumbers(a) {

return (b) => {

return (c) => {

return a + b + c

}

}

} console.log(addNumbers(1)(2)(3)) // Outputs 6

Here what we have done is converted our single function call, addNumbers(1, 2, 3) , into multiple function calls using addNumbers(1)(2)(3) , if we break this down step by step, we are essentially doing…

const add1 = addNumbers(1);

// Pass 1 into our addNumbers function

// add1 contains return (b) => { return (c) => { return a + b + c }} const add2 = add1(2);

// Pass 2 into our add1 function

// add2 contains return (c) => { return a + b + c } const answer = add2(3);

// Pass 3 into our add2 function

// answer contains return a + b + c

// Our calculation is then performed and stored as answer console.log(answer) // Outputs 6

One useful thing about currying, is that it can help us to avoid passing a single variable multiple times, let’s say that we want a function, addTen() , that we always want to add 10 to a number. Instead of having to call something like addTen(10)(1) , addTen(10)(2) , and addTen(10)(3) , we could rework our addNumbers() function to something like…

function addNumbers(base) {

return (number) => {

return base + number;

}

} const addTen = addNumbers(10);

// Pass 10 as our base to addNumbers

// addTen contains return (number) => { return 10 + number } addTen(1) // Outputs 11

addTen(2) // Outputs 12

addTen(3) // Outputs 13

Once we have 10 stored as our base in addNumbers() and created our addTen , we can simply call addTen() with whatever we would like our argument to be.

Another thing that we could do is to create a curry function that will accept another function as it’s argument, check whether there are any additional arguments that the original function did not expect, and if there are no additional arguments run the original function but if there are then it will curry the original function. Here’s how that would look, fair warning it’s considered to be “advanced”…

function curry(func) {

return function curried(...args) {

if (args.length >= func.length) {

return func.apply(this, args);

} else {

return function(...args2) {

return curried.apply(this, args.concat(args2));

}

}

};

}

Using curry() we could now do this…

function addNumbers(a, b, c) {

return a + b + c;

} let curriedAdd = curry(addNumbers); console.log(curriedAdd(1)(2)(3)) // Outputs 6

Well there you have it, I hope that this gives you another interesting topic from JavaScript, and thanks for reading!