Skip to content

Instantly share code, notes, and snippets.

@jmaicaaan
Last active August 23, 2020 08:32
Show Gist options
  • Save jmaicaaan/1bc63d6478c4c4e153d52ee5ca798da7 to your computer and use it in GitHub Desktop.
Save jmaicaaan/1bc63d6478c4c4e153d52ee5ca798da7 to your computer and use it in GitHub Desktop.
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