Sub topic 12
Point-Free Style
This chapter assumes you have read the earlier chapters on function composition (pipe and compose) and currying. You should be comfortable reading arrow functions and calling curried functions one argument at a time.
What "point-free" means
In this context, a "point" is a function's argument. Point-free style (sometimes called tacit programming) means defining a function without ever naming the data it works on. You build the function by combining other functions, and the data flows through them.
Here is the same idea written both ways:
const toUpper = (s) => s.toUpperCase();
// Pointed: names the argument only to pass it straight on
const shoutAll = (words) => words.map((w) => toUpper(w));
// Point-free: passes the function itself
const shoutAllPF = (words) => words.map(toUpper);
console.log(shoutAll(['a', 'b'])); // [ 'A', 'B' ]
console.log(shoutAllPF(['a', 'b'])); // [ 'A', 'B' ]The wrapper (w) => toUpper(w) adds nothing. It takes w and hands it straight to toUpper. Removing it is the smallest point-free refactor.
Building point-free pipelines
The style pays off when you combine curried helpers with pipe. Each helper takes its configuration first and its data last, so you can supply the configuration now and leave the data for later.
const pipe = (...fns) => (x) => fns.reduce((acc, fn) => fn(acc), x);
const curry = (fn) => {
const curried = (...args) =>
args.length >= fn.length ? fn(...args) : (...more) => curried(...args, ...more);
return curried;
};
const prop = curry((key, obj) => obj[key]);
const filter = curry((pred, xs) => xs.filter(pred));
const map = curry((fn, xs) => xs.map(fn));
const users = [
{ name: 'Ada', active: true },
{ name: 'Bob', active: false },
{ name: 'Cy', active: true }
];
// Pointed
const activeNames = (list) => list.filter((u) => u.active).map((u) => u.name);
// Point-free
const activeNamesPF = pipe(filter(prop('active')), map(prop('name')));
console.log(activeNames(users)); // [ 'Ada', 'Cy' ]
console.log(activeNamesPF(users)); // [ 'Ada', 'Cy' ]The point-free version reads as a list of steps: keep the active users, then take their names.
Common errors
Arity mismatches
Array methods pass extra arguments to their callback: the element, the index, and the array. If you pass a function directly, it receives all of them.
console.log(['1', '2', '3'].map(parseInt)); // [ 1, NaN, NaN ]
const unary = (fn) => (x) => fn(x);
console.log(['1', '2', '3'].map(unary(parseInt))); // [ 1, 2, 3 ]
console.log(['1', '2', '3'].map(Number)); // [ 1, 2, 3 ]parseInt treats the index as its radix argument, which is the base it parses in. A unary wrapper limits the function to one argument.
Losing `this`
Passing a method by reference detaches it from its object.
class Greeter {
constructor(greeting) { this.greeting = greeting; }
greet(name) { return `${this.greeting}, ${name}`; }
}
const g = new Greeter('Hi');
try {
['Ada'].map(g.greet);
} catch (e) {
console.log(e instanceof TypeError); // true
}
console.log(['Ada'].map(g.greet.bind(g))); // [ 'Hi, Ada' ]Over-applying the style
| Use point-free when | Keep the argument when |
|---|---|
| The wrapper only forwards its argument | The callback uses several arguments |
| Steps are small, well-named helpers | The function is a method that needs `this` |
| The pipeline reads like a sentence | Naming the data makes the intent clearer |
Exercise
Why does ['10', '10', '10'].map(parseInt) return [ 10, NaN, 2 ], and how would you fix it in point-free style?
Show answer
map calls parseInt(element, index), so the calls are parseInt('10', 0), parseInt('10', 1), and parseInt('10', 2). A radix of 0 falls back to base 10 and gives 10. Radix 1 is invalid and gives NaN. Radix 2 reads '10' as binary and gives 2. Fix it with map(unary(parseInt)), where unary = (fn) => (x) => fn(x), or use map(Number).