Currying and partial application
Currying turns a function of several arguments into a chain of functions of one argument, so add(a, b) becomes add(a)(b). Partial application is the practical half of the idea: fix some arguments now, keep the rest for later, and get a new function back. Both are built from closures, which is why they belong right after functions as values.
The two techniques earn their place in code that builds other code: a logging function with the prefix already set, a validator with the field already chosen, a step of a pipeline that needs one setting. This lesson shows the manual form, the two ways to write it, and the trap that fn.length sets for a default or rest parameter.
One argument at a time
The simplest curried function returns a function from each call until the last argument arrives. The returned function closes over the values collected so far, so the same partial function can be called many times with different second arguments.
function add(a) {
return (b) => a + b;
}
const add5 = add(5);
console.log(add5(3), add5(10));
console.log('one at a time:', add(1)(2));
console.log('the same function still works:', add5(0));
node one.js
8 15
one at a time: 3
the same function still works: 5
add5 is a function that adds five to whatever it is given, and it cost one line to build. Nothing is stored in a variable of the module: the value five lives in the closure of the arrow that add returned, and a second call of add(5) would build a second closure with the same content. The drawback is visible in the call: add(1)(2) is harder to read than add(1, 2), which is why currying in JavaScript is a tool for building functions rather than a style for writing every call.
Fixing the first arguments
Partial application keeps the original function and fills in the first arguments. Two ways are common: bind, whose first argument is the this value and the rest become fixed arguments, and a small wrapper that collects the fixed values in a closure. The wrapper is usually clearer when this plays no part.
function multiply(a, b) {
return a * b;
}
const double = multiply.bind(null, 2);
console.log('bind:', double(21));
function partial(fn, ...fixed) {
return (...rest) => fn(...fixed, ...rest);
}
const triple = partial(multiply, 3);
console.log('partial:', triple(14));
function greet(greeting, name, punctuation) {
return `${greeting}, ${name}${punctuation}`;
}
const helloAda = partial(greet, 'Hello', 'Ada');
console.log(helloAda('!'));
console.log(helloAda('?'));
node partial.js
bind: 42
partial: 42
Hello, Ada!
Hello, Ada?
Both lines print 42 from different arguments: bind fixed the two and the wrapper fixed the three. The null in multiply.bind(null, 2) is the this of the call, which a plain function does not use, and it is the reason the wrapper reads better where this is not wanted. The last two lines are the useful shape: one function with the greeting and the name already decided, called twice with a punctuation.
Currying a function of any arity
A general curry collects arguments until it has enough and then calls the original. The number it needs is fn.length, the count of declared parameters. Until that count is reached, each call returns another function that remembers what it has; once it is reached, the real function runs.
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn(...args);
}
return (...rest) => curried(...args, ...rest);
};
}
function volume(length, width, height) {
return length * width * height;
}
const curriedVolume = curry(volume);
console.log(curriedVolume(2, 3, 4));
console.log(curriedVolume(2)(3)(4));
console.log(curriedVolume(2, 3)(4));
console.log(curriedVolume(2)(3, 4));
const addThree = curry((a, b, c) => a + b + c);
console.log('reused:', addThree(1)(2)(3), addThree(1, 2)(3));
node curry.js
24
24
24
24
reused: 6 6
Four ways of passing three arguments to one function, and the same product comes out. The recursion is in curried, which either has enough arguments or returns a function that calls itself with the new ones appended. The function is called once, at the moment the last argument arrives, so a partially applied function is inert until then. That is also why curry takes a function whose parameters it can count: the next section is where the counting breaks.
The arity trap
fn.length stops at the first parameter with a default and counts a rest parameter as nothing, so a function of two parameters where the second has a default reports one. A curry built on that number calls the function too early. The fix is to give the arity explicitly, either as an argument of the curry or by removing the default.
function pad(text, width = 5) {
return text.padStart(width, '.');
}
console.log('pad.length:', pad.length);
const result = curry(pad)('ab');
console.log('one argument is enough:', JSON.stringify(result), 'of type', typeof result);
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn(...args);
}
return (...rest) => curried(...args, ...rest);
};
}
function sum(...values) {
return values.reduce((total, value) => total + value, 0);
}
console.log('sum.length:', sum.length);
console.log('curried sum:', curry(sum)(1, 2, 3));
function curryArity(arity, fn) {
return function curried(...args) {
if (args.length >= arity) {
return fn(...args);
}
return (...rest) => curried(...args, ...rest);
};
}
const padded = curryArity(2, pad);
console.log('with an explicit arity:', padded('ab')(8));
console.log('and at once:', padded('ab', 3));
node arity.js
pad.length: 1
one argument is enough: "...ab" of type string
sum.length: 0
curried sum: 6
with an explicit arity: ......ab
and at once: .ab
The fifth line is the trap in one sentence: curry(pad)("ab") returned a string, not a function, so the width can never be passed and the default of 5 is used for ever. The rest parameter of sum has the same problem from the other side: its length is 0, so the curried form expects no arguments and calls it at once, which happens to work here and would not for a function that needed two of its values. An explicit arity is the honest fix, and it is what a curry helper in a library offers.
Exercises
- Build a function of three nested arrows that subtracts the three arguments from left to right and call it one argument at a time. Which value does it return?
- Call partial(greet, 'Hi') in partial.js and then with two more arguments. What does it print?
- Use bind on greet to fix the greeting and the name, then call the result with the punctuation. Which line of partial.js does it replace?
- Remove the default from pad in arity.js and run the file again. What is pad.length now, and what does curry(pad)('ab') return?
- Add a fourth argument to the volume call in curry.js without changing the function. Which value does the callback receive, and what does the product become?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does currying do to a function of three arguments?
Answer
It turns it into three functions of one argument
Each call takes one argument and returns a function waiting for the next, until the last one runs the body.
-
What is partial application?
Answer
Fixing some arguments now and getting a function for the rest
A partial function keeps the values already given and waits for the remaining parameters.
-
What does this program print?
const add = (a) => (b) => a + b; console.log(add(2)(3));Answer
5
The first call fixes a as two and returns a function that adds its argument to it.
-
What is the first argument of
multiply.bind(null, 2)?Answer
The this value of the call, which a plain function ignores
bind takes this first and then the fixed arguments, which is why a wrapper reads better when this is unused.
-
What does this program print?
function mul(a, b) { return a * b; } const double = mul.bind(null, 2); console.log(double(4));Answer
8
The bound value becomes the first argument, so the call supplies the second and the product is eight.
-
What does this program print?
function partial(fn, x) { return (y) => fn(x, y); } const inc = partial((a, b) => a + b, 1); console.log(inc(4));Answer
5
The wrapper calls the original with the fixed one and the new four.
-
What does a general
curryfunction use to know when it has enough arguments?Answer
The length property of the function
fn.length is the declared parameter count, and the curried function runs the original once it reaches it.
-
What does this program print?
function volume(a, b, c) { return a * b * c; } console.log(volume.length);Answer
3
The property counts the declared parameters when none of them has a default or takes the rest.
-
Why does a default parameter break a curry built on
fn.length?Answer
The length stops at the default, so the function is called before the later argument arrives
A function with a default reports the parameters before it, so the curried call runs the body too early.
-
What does an explicit arity in a curry helper fix?
Answer
It says how many arguments to wait for, whatever fn.length reports
The number is a parameter of the helper, so defaults and rest parameters no longer decide when the call happens.
The code on this page was run with Node.js 24.22.1 and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev