Created
June 27, 2016 05:28
-
-
Save stefanfrede/3a85cebdb222b70542d9e9f4d853d06c to your computer and use it in GitHub Desktop.
The B Combinator, or (variadic) compose.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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