Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array.prototype.foldLeft = function (sum, callback) {
- var head,
- list = Array.prototype.slice.call(this);
- if (list.length) {
- head = list.shift(1);
- return list.foldLeft(callback(sum, head), callback);
- }
- return sum;
- };
- var numbers = [1, 2, 3, 4];
- var sum = numbers.foldLeft(0, function (total, next) {
- return total + next;
- });
- var times = numbers.foldLeft(1, function (total, next) {
- return total * next;
- });
- console.log('sum ' + sum);
- console.log('times ' + times);
Add Comment
Please, Sign In to add comment