3 ms·
> A reducer might not add the current value to the memo (filter) but it always returns some memo for the next step. I think the idea is that your reducer func
by defen 12y ago
> A reducer might not add the current value to the memo (filter) but it always returns some memo for the next step.
I think the idea is that your reducer function shouldn't have to care about steps or memos. e.g. I should be able to do
filter(function(x) { return x < 10; });
and get back some thing that I can then pass in to map, or to reduce, or compose with other things.
- ww520 12y agoHmm, isn't that just the filter predicate function? Not the reducer itself? Looks like the filter() function constructs a reducer using the predicate. The new reducer adds the filtering functionality with the predicate. Looking at the code for filter (copied from above) filter: ([pred] (fn [f1] <-- builds the new reducer; f1 is the inner reducer (fn <-- this is the new outer reducer ([result input] <-- outer's params: result is memo; input is value (if (pred input) <-- pred is user provided, your function (f1 result input) <-- calls the inner reducer; returns its result. result))))) <-- call filtered, returns result unchanged. The reducer still behaves as reducer, returning one result. A Javascript version would be: function filterTransducer(innerReducer, pred) { return function(memo, value) { if (pred(value)) return innerReducer(memo, value) return memo; } }