Skip to content

Instantly share code, notes, and snippets.

@divanvisagie
Created April 20, 2016 16:42
Show Gist options
  • Save divanvisagie/2b9093bb67dd12d6b9c3131d40180be3 to your computer and use it in GitHub Desktop.
Save divanvisagie/2b9093bb67dd12d6b9c3131d40180be3 to your computer and use it in GitHub Desktop.
Array.prototype.foldLeft = function(init) {
function f(sum, list, callback) {
if (list.length > 0) {
var head = list.shift();
return f(callback(sum, head), list, callback);
}
return sum;
}
var list = this;
return function(cb) {
return f(init, list, cb);
};
};
console.dir(
[1,1,1].foldLeft(3)((r, x) => r + x)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment