Skip to content

Instantly share code, notes, and snippets.

@jmaicaaan
Last active August 23, 2020 08:31
Show Gist options
  • Save jmaicaaan/5f07a386592e2068b712c4d5cefd367e to your computer and use it in GitHub Desktop.
Save jmaicaaan/5f07a386592e2068b712c4d5cefd367e 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 mapReduce = mapperFn => (accumulator, currentValue) => {
accumulator.push(mapperFn(currentValue));
return accumulator;
};
const filterReduce = predicateFn => (accumulator, currentValue) => {
if (predicateFn(currentValue)) {
accumulator.push(currentValue);
}
return accumulator;
};
const res = data
.reduce((accumulator, currentValue) => {
accumulator.push(addBy1(currentValue));
return accumulator;
}, [])
.reduce((accumulator, currentValue) => {
accumulator.push(multiplyBy2(currentValue));
return accumulator;
}, [])
.reduce((accumulator, currentValue) => {
if (getItemsBelow10(currentValue)) {
accumulator.push(currentValue);
}
return accumulator;
}, [])
.reduce(sum, 0)
// 18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment