Function Currying
Currying turns a function that takes several arguments into a series of functions that each take one. Instead of f(a, b), a curried function is called as f(a)(b). Each call returns a new function that remembers the arguments given so far, so you can supply some arguments now and the rest later.
It works because functions in JavaScript are values: a function can return another function, and the returned function keeps access to the variables around it, which is called a closure.
A function that returns a function
Section titled “A function that returns a function”The two functions below calculate the same price. The first takes both arguments at once, and the second takes the rate and returns a function that waits for the price:
function addTax(rate, price) { return price * (1 + rate);}
function withTax(rate) { return function (price) { return price * (1 + rate); };}
console.log(addTax(0.2, 50));console.log(withTax(0.2)(50));This prints:
6060withTax(0.2) returns the inner function, and the second pair of parentheses calls it with 50. The inner function can still read rate after withTax has finished, because of the closure.
Making specialised functions
Section titled “Making specialised functions”The point of currying is to keep the function returned by the first call. Each one is a ready-made function with the first argument fixed. Arrow functions make the curried form short, since each => adds one more step:
const withTax = rate => price => price * (1 + rate);
const ukVat = withTax(0.2);const reducedRate = withTax(0.05);
console.log(ukVat(50));console.log(ukVat(12.5));console.log(reducedRate(80));This prints:
601584Curried functions as callbacks
Section titled “Curried functions as callbacks”Array methods pass one element at a time to their callback, so a function curried down to one remaining argument fits them directly:
const isWarmerThan = limit => temperature => temperature > limit;const temperatures = [12, 19, 23, 8, 26];
console.log(temperatures.filter(isWarmerThan(20)));console.log(temperatures.filter(isWarmerThan(10)));This prints:
[ 23, 26 ][ 12, 19, 23, 26 ]isWarmerThan(20) reads like a sentence at the call, and the same function serves any limit.
A general curry helper
Section titled “A general curry helper”A helper can curry any function with a fixed number of parameters. It collects arguments until it has as many as the original function declares, which it reads from the function’s length property, and then calls it:
function curry(fn) { return function curried(...args) { if (args.length >= fn.length) { return fn(...args); } return (...more) => curried(...args, ...more); };}
const formatLine = (currency, quantity, item) => `${quantity} x ${item} (${currency})`;const inPounds = curry(formatLine)('GBP');
console.log(inPounds(2, 'notebooks'));console.log(inPounds(1)('stapler'));This prints:
2 x notebooks (GBP)1 x stapler (GBP)This helper accepts several arguments in one call as well as one at a time. It does not work for functions with default values or rest parameters, because length counts only the parameters before the first of those.
Partial application with bind
Section titled “Partial application with bind”Fixing some arguments of an ordinary function, without currying it, is called partial application. The built-in bind method does it, and its first argument sets this, which a plain function does not need:
function logLine(level, message) { console.log(`[${level}] ${message}`);}
const warn = logLine.bind(null, 'WARN');warn('Disk nearly full');This prints:
[WARN] Disk nearly fullCall, Apply, and Bind Methods covers bind in full. Put the argument that changes least first, whichever technique you use, because that is the one that gets fixed.
Steve Griffith covers this topic in his video JavaScript Function Currying.