Created
August 23, 2020 05:56
-
-
Save jmaicaaan/4d6fb4ddeec3ae1b1a6ec1c812136a6b 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
/** | |
* | |
* These utility functions are often available on various | |
* functional libraries (i.e ramdajs) | |
* `compose` runs from right-to-left | |
* `pipe` runs from left-to-right | |
*/ | |
const compose = (...fns) => (initialValue) => fns.reduceRight((d, f) => f(d), initialValue); | |
const pipe = (...fns) => (initialValue) => fns.reduce((d, f) => f(d), initialValue); | |
const data = 5; | |
const addBy1 = (x) => x + 1; | |
const multiplyBy2 = (x) => x * 2; | |
const composeResult = compose(multiplyBy2, addBy1)(data); | |
// 12 | |
const pipeResult = pipe(addBy1, multiplyBy2)(data); | |
// 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment