Guest User

Untitled

a guest
Jan 22nd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. Array.prototype.foldLeft = function (sum, callback) {
  2. var head,
  3. list = Array.prototype.slice.call(this);
  4.  
  5. if (list.length) {
  6. head = list.shift(1);
  7. return list.foldLeft(callback(sum, head), callback);
  8. }
  9. return sum;
  10. };
  11.  
  12. var numbers = [1, 2, 3, 4];
  13. var sum = numbers.foldLeft(0, function (total, next) {
  14. return total + next;
  15. });
  16. var times = numbers.foldLeft(1, function (total, next) {
  17. return total * next;
  18. });
  19. console.log('sum ' + sum);
  20. console.log('times ' + times);
Add Comment
Please, Sign In to add comment