Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stefanfrede/3a85cebdb222b70542d9e9f4d853d06c to your computer and use it in GitHub Desktop.
Save stefanfrede/3a85cebdb222b70542d9e9f4d853d06c to your computer and use it in GitHub Desktop.
The B Combinator, or (variadic) compose.
const compose = (a, ...rest) =>
rest.length === 0
? a
: (c) => a(compose(...rest)(c))
// Given:
const addOne = (number) => number + 1;
const doubleOf = (number) => number * 2;
// Instead of:
const doubleOfAddOne = (number) => doubleOf(addOne(number));
// We could write:
const doubleOfAddOne = compose(doubleOf, addOne);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment