Last active
August 23, 2020 08:32
-
-
Save jmaicaaan/1bc63d6478c4c4e153d52ee5ca798da7 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 data = [ | |
1, | |
2, | |
3, | |
4, | |
5, | |
]; | |
/** | |
* | |
* This utility function is often available on various | |
* functional libraries (i.e ramdajs) | |
* `pipe` is the opposite of `compose` | |
*/ | |
const pipe = (...fns) => initialValue => fns.reduce((result, fn) => fn(result), initialValue); | |
const addBy1 = (x) => x + 1; | |
const multiplyBy2 = (x) => x * 2; | |
const getItemsBelow10 = (x) => x < 10; | |
const sum = (accumulator, currentValue) => accumulator += currentValue; | |
const res = data | |
.map( | |
pipe(addBy1, multiplyBy2) | |
) | |
.filter(getItemsBelow10) | |
.reduce(sum, 0) | |
// 18 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment