Reduce

by de Montalembert Jonathan

JavaScript

function reduce(reducer, init, arr) {
  function go(x, xs, acc) {
    if (typeof x !== 'undefined') {
      return go(xs[0], xs.slice(1), reducer(x, acc));
    } else {
      return acc;
    }
  }
  return go(arr[0], arr.slice(1), init);
}

console.log(reduce((a, b) => a + b, 0, [1, 2, 3, 4, 5, 6, 7, 8, 9]));
console.log(reduce((a, b) => a + b, 0, [1, 2, 3, 4, 5, 6, 7, 8, 9]));