Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created January 26, 2016 17:13
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save roman01la/2cdbf41d59939c54071e to your computer and use it in GitHub Desktop.
const mapping = (f) => (reducing) => (result, input) => reducing(result, f(input));
const filtering = (predicate ) => (reducing) => (result, input) => predicate(input) ? reducing(result, input) : result;
@dtipson
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