Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
function reduceAhead(arr,fn,seed,nextSeed) {
const l = arguments.length;
seed = l < 3 ? arr.shift() : seed;
let tmp = {
out:seed,
entry:arr.shift(),
at: l < 3 ? 1 : 0
};
tmp = arr.reduce(function(tmp,next){
tmp.out = fn.call(arr,tmp.out,tmp.entry,next,tmp.at,arr);
tmp.entry = next;
tmp.at = tmp.at + 1;
return tmp;
},tmp);
return fn.call(arr,tmp.out,tmp.entry,nextSeed,tmp.at);
}
function reduceAround(arr,fn,seed,lastSeed,nextSeed) {
const l = arguments.length;
seed = l < 3 ? arr.shift() : seed;
let tmp = {
out:seed,
last:lastSeed,
entry:arr.shift(),
at: l < 3 ? 1 : 0
};
tmp = arr.reduce(function(tmp,next){
tmp.out = fn.call(arr,tmp.out,tmp.entry,tmp.last,next,tmp.at,arr);
tmp.last = tmp.entry;
tmp.entry = next;
tmp.at = tmp.at + 1;
return tmp;
},tmp);
return fn.call(arr,tmp.out,tmp.entry,tmp.last,nextSeed,tmp.at);
}
function reduceBehind(arr,fn,seed,lastSeed) {
const l = arguments.length;
seed = l < 3 ? arr.shift() : seed;
let tmp = {
out:seed,
last:lastSeed,
at: l < 3 ? 1 : 0
};
tmp = arr.reduce(function(tmp,entry){
tmp.out = fn.call(arr,tmp.out,entry,tmp.last,tmp.at,arr);
tmp.last = entry;
tmp.at = tmp.at + 1;
return tmp;
},tmp);
return tmp.out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment