3 ms·
Hmm, isn't that just the filter predicate function? Not the reducer itself? Looks like the filter() function constructs a reducer using the predicate. The ne
by ww520 12y ago
Hmm, 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;
}
}