Last active
August 28, 2016 23:56
-
-
Save jdmorlan/de8284c53d4a90189d16d1f27ccb5a11 to your computer and use it in GitHub Desktop.
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 = (...funcs) => { | |
//... if statements removed for clarity | |
const last = funcs[funcs.length - 1] | |
const rest = funcs.slice(0, -1) | |
return (...args) => rest.reduceRight((composed, f) => f(composed), last(...args)) | |
} | |
// Examples | |
const addTwo = (x) => x + 2 | |
const addThree = (x) => x + 3 | |
const composedFunction = compose(addTwo, addThree) | |
composedFunction(2) //=> 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment