Created
January 10, 2018 22:43
-
-
Save obouchari/bd93edade4ad9af787428859f20f906d to your computer and use it in GitHub Desktop.
FP pipe and compose util functions in ES2015
This file contains 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
// Pipe | |
const pipe = (...fns) => | |
(...input) => | |
fns.reduce(function(input, fn) { | |
if (typeof fn !== 'function') { | |
throw Error(`expect a "function" instead got "${typeof fn}".`); | |
} | |
return fn(...[].concat(input)); | |
}, input); | |
// Compose | |
const compose = (...fns) => pipe(...fns.reverse()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment