Currying
Currying is the process of transforming a function that takes several arguments into a sequence of functions, each of which takes only one argument. A function f(a, b, c) becomes f(a)(b)(c), so it can be called in parts and partially applied calls can be reused.
Theory
TL;DR
- Currying splits a multi argument function into a chain of single argument functions.
sum(a, b)becomescurriedSum(a)(b).- It works thanks to closures: every returned function remembers the values passed so far.
- It produces partially applied functions that are convenient to reuse:
const add10 = curriedSum(10). - It simplifies function composition, which is why it is common in functional programming.
- It improves readability and flexibility, especially in libraries such as Lodash or Ramda.
Quick example
An ordinary function:
function sum(a, b) {
return a + b;
}
sum(2, 3); // 5After currying:
function curriedSum(a) {
return function (b) {
return a + b;
};
}
curriedSum(2)(3); // 5The same thing with arrow functions is shorter:
const curriedSum = (a) => (b) => a + b;
curriedSum(2)(3); // 5How it works
curriedSum(2)returns a new function that remembers the valuea = 2.- That function then waits for the second argument
b. - When we call it with
3, it returns the result2 + 3.
The memory of the first argument comes from the closure: the inner function keeps access to the outer function's scope even after the outer call has finished.
What currying is good for
-
Reusing partially applied functions
javascriptconst add10 = curriedSum(10); add10(5); // 15 add10(7); // 17 -
It simplifies function composition, which is handy in functional programming: a single argument function drops neatly into a chain of transformations.
-
It improves readability and flexibility, especially in libraries such as Lodash or Ramda, where curried variants of the methods come out of the box.
In one sentence: currying turns f(a, b, c) into f(a)(b)(c) so that it can be called piece by piece and partial calls can be reused.
Currying and partial application
The two are related but not the same. Currying always produces a chain of functions with exactly one argument per step. Partial application fixes any number of arguments at once and returns a function waiting for the rest:
function sum(a, b, c) {
return a + b + c;
}
// partial application: fix two arguments at once
const addTo3 = sum.bind(null, 1, 2);
addTo3(3); // 6The curried version, by contrast, is called one argument at a time: curried(1)(2)(3).
Common mistakes
- Confusing currying with partial application: currying is strictly one argument per step.
- Forgetting that each step returns a function rather than a value:
curriedSum(2)is a function, socurriedSum(2) + 3yields a meaningless string. - Losing
thiswhen currying an object method: arrow functions takethisfrom where they are declared, ordinary ones needbind. - Currying everything: for a normal call with all arguments present, a plain
sum(a, b)reads better. - Assuming currying makes code faster: it creates extra functions and closures, so it is about expressiveness, not speed.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.