Skip to content

Instantly share code, notes, and snippets.

@jmaicaaan
Last active August 23, 2020 08:24
Show Gist options
  • Save jmaicaaan/bbe609e8250ad16a8f7de41c782344f7 to your computer and use it in GitHub Desktop.
Save jmaicaaan/bbe609e8250ad16a8f7de41c782344f7 to your computer and use it in GitHub Desktop.
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 combinerConcat = (accumulator, currentValue) => {
accumulator.push(currentValue);
return accumulator;
};
const transducer = pipe(
mapReduce(addBy1),
mapReduce(multiplyBy2),
filterReduce(getItemsBelow10)
);
const mapTransducer = mapReduce(addBy1)
// mapTransducer:: combinerFn -> (accumulator, currentValue) -> result
const filterTransducer = filterReduce(getItemsBelow10)
// filterTransducer:: combinerFn -> (accumulator, currentValue) -> result
const mapTransducerWithCombiner = mapReduce(addBy1)(combinerConcat)
// mapTransducerWithCombiner:: (accumulator, currentValue) -> result
const filterTransducerWithCombiner = filterReduce(getItemsBelow10)(combinerConcat)
// filterTransducerWithCombiner:: (accumulator, currentValue) -> result
// Execution of `mapTransducerWithCombiner` and `filterTransducerWithCombiner` in the `pipe`
const transducerWithCombiner = pipe(
mapReduce(addBy1)(combinerConcat), // returns a result
filterReduce(getItemsBelow10)(combinerConcat), // expects a (accumulator, currentValue) -> result
);
// BREAK! Incorrect function signature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment