Files
30-seconds-of-code/snippets/composeRight.md
30secondsofcode c4a78512b2 Travis build: 1360
2018-01-23 20:14:21 +00:00

469 B

composeRight

Performs left-to-right function composition.

Use Array.reduce() to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.

const composeRight = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
const add = (x, y) => x + y;
const square = x => x * x;
const addAndSquare = composeRight(add, square);
addAndSquare(1, 2); // 9