JavaScriptFundamental (ES6), part 1
Curry, compose and pipe in JavaScript
65 Partial application, curry, compose and pipe
hardFix the leading arguments of a three-argument fare function two ways, with your own partial and with bind. Write curry that keeps collecting until it has enough arguments, then compose and pipe, and show that the two run the same steps in opposite directions.
Stuck? Three hints, smallest first
- 1.
partialis one line: capture the fixed arguments with rest, spread them in front of the rest at call time. - 2.
curryneeds to know when to stop collecting, and the only thing it has to go on isfn.length. - 3.
pipeisreduceover the functions;composeisreduceRight.
const fare = (base, perKm, km) => base + perKm * km;
const partial = (fn, ...fixed) => (...rest) => fn(...fixed, ...rest);
const lagosFare = partial(fare, 25000, 1500);
const viaBind = fare.bind(null, 25000, 1500);
const curry = (fn) => {
const collect = (got) => (...args) => {
const all = [...got, ...args];
return all.length >= fn.length ? fn(...all) : collect(all);
};
return collect([]);
};
const curried = curry(fare);
const pipe = (...fns) => (value) => fns.reduce((acc, fn) => fn(acc), value);
const compose = (...fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value);
const toKobo = (naira) => naira * 100;
const addVat = (kobo) => Math.round(kobo * 1.075);
const label = (kobo) => `₦${kobo / 100}`;
const priced = pipe(toKobo, addVat, label);
const sameThing = compose(label, addVat, toKobo);
const directions = [pipe((n) => n + 1, (n) => n * 2)(5), compose((n) => n + 1, (n) => n * 2)(5)];
const variadic = (...args) => args.length;
const brokenCurry = curry(variadic);
const collectedNothing = brokenCurry();
const arities = [fare.length, viaBind.length, lagosFare.length];
console.log('partial :', lagosFare(10), '| bind:', viaBind(10));
console.log('curried :', curried(25000)(1500)(10), curried(25000, 1500)(10));
console.log('pipe :', priced(400), '| compose:', sameThing(400));
console.log('directions:', directions);
console.log('arities :', arities, '| curry of a variadic function:', collectedNothing);
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('partial fixes the leading arguments', lagosFare(10), 40000);
test('bind does the same, with null standing in for this', viaBind(10), 40000);
test('a curried function takes them one at a time', curried(25000)(1500)(10), 40000);
test('or in any grouping, until it has enough', [curried(25000, 1500)(10), curried(25000)(1500, 10)], [40000, 40000]);
test('pipe reads left to right', priced(400), '₦430');
test('compose is the same three steps written right to left', sameThing(400), '₦430');
test('and the direction genuinely changes the answer', directions, [12, 11]);
test('bind keeps the remaining arity; a rest-parameter wrapper reports zero', arities, [3, 1, 0]);
test('so currying a variadic function calls it at once, having collected nothing', [brokenCurry.length, collectedNothing], [0, 0]);
Partial application and currying are not the same thing. partial fixes some arguments and returns something you call once with the rest. curry returns a chain: one argument at a time, or several, until the count is reached. Both exist to turn a general function into a specific one — lagosFare is a fare function with the tariff already in it — which is exactly what you want when passing a function to map.
The arity assertion is where hand-rolled currying breaks. fn.length counts parameters before the first default or rest, so it is 0 for (...args) => and for anything already wrapped in a rest-parameter helper. Curry a variadic function and it calls immediately with nothing; curry the result of your own partial and the same thing happens. Note the contrast in that assertion: bind reports the arity that is genuinely left, 3 - 2 = 1, because the engine tracks it; your wrapper cannot.
pipe and compose differ only in reading order, and pipe is usually the kinder one because it matches the order things happen. Both are single-value chains: to thread more than one value, make the first function take the several arguments, or pass an object through.