Skip to content

Instantly share code, notes, and snippets.

@wshager
Last active March 20, 2018 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wshager/df8e0c67281068cd5d01f30452df7287 to your computer and use it in GitHub Desktop.
Save wshager/df8e0c67281068cd5d01f30452df7287 to your computer and use it in GitHub Desktop.
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