Files
30-seconds-of-code/snippets/composeRight.md
Angelos Chalaris e1f71f492e Add composeRight
2018-01-23 22:12:56 +02:00

468 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