Skip to content

Instantly share code, notes, and snippets.

@CYBAI CYBAI/step3.js
Last active Aug 5, 2016

Embed
What would you like to do?
Fix step3 in `transducing in javascript` from `getify`
function add1(v) { return v + 1; }
function isOdd(v) { return v % 2 == 1; }
function sum(total,v) { return total + v; }
function listReduction(list,v) {
list.push(v);
return list;
}
function mapReducer(fn) {
return function(reductionFn){
return function(list,v){
return reductionFn( list, fn(v) );
};
};
}
function filterReducer(fn) {
return function(reductionFn){
return function(list,v){
if (fn(v)) return reductionFn( list, v );
return list;
};
};
}
// No need to use this function
function listSumReduction(list,v) {
list[0] = sum(list[0] || 0,v);
return list;
}
function identity(v) { return v; }
function reduceReducer(reductionFn){
return mapReducer(identity)(reductionFn);
}
var list = [2,5,8,11,14,17,20];
list
.reduce( mapReducer(add1)(listReduction), [] )
.reduce( filterReducer(isOdd)(listReduction), [] )
.reduce( reduceReducer(sum));
@DrBoolean

This comment has been minimized.

Copy link

commented Aug 5, 2016

And since map(identity) == identity

reduceReducer(sum) ==
mapReducer(identity)(sum) ==
id(sum) ==
sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.