Skip to content

Instantly share code, notes, and snippets.

View jmaicaaan's full-sized avatar

JM Santos jmaicaaan

  • Philippines
View GitHub Profile
const data = [
1,
2,
3,
4,
5,
];
/**
*
const data = [
1,
2,
3,
4,
5,
];
/**
*
const data = [
1,
2,
3,
4,
5,
];
/**
*
const data = [
1,
2,
3,
4,
5,
];
/**
*
// From here
const res = data
.map(
pipe(addBy1, multiplyBy2)
)
.filter(getItemsBelow10)
.reduce(sum, 0)
/**
*
* 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 = [
1,
2,
3,
4,
5,
];
/**
*
const transducer = pipe(
mapReduce(addBy1)(combinerConcat),
mapReduce(multiplyBy2)(combinerConcat),
filterReduce(getItemsBelow10)(combinerConcat)
);
const res = data
.reduce(transducer, [])
.reduce(sum, 0)
const mapReduce = mapperFn => combinerFn => (accumulator, currentValue) => {
return combinerFn(accumulator, mapperFn(currentValue));
};
const filterReduce = predicateFn => combinerFn => (accumulator, currentValue) => {
if (predicateFn(currentValue)) {
return combinerFn(accumulator, currentValue);
}
return accumulator;
};
const data = [
1,
2,
3,
4,
5,
];
/**
*