Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
const mapping = (f) => (reducing) => (result, input) => reducing(result, f(input));
const filtering = (predicate ) => (reducing) => (result, input) => predicate(input) ? reducing(result, input) : result;
Copy link

dtipson commented Feb 8, 2016

Not that adding extra, optional arguments is necessarily a wise thing, but I think adding the result to f(input, result) and predicate(input, result) can be interesting when using these methods. For instance, with that in place, in your filtering transducer (producer?) you can then do:

filtering((x, acc) => !~acc.indexOf(x))

And now you have a method that only allows values (or comparable objects) to be added to the result once:

reduce(filtering(dedupe)(concat), [], [1,1,1,2,6,6,7,8]);//-> [1, 2, 6, 7, 8]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment