Skip to content

Instantly share code, notes, and snippets.

@rebolyte
Last active November 12, 2018 17:26
Show Gist options
  • Select an option

  • Save rebolyte/d14263f875a61ffe21004e8b590acd47 to your computer and use it in GitHub Desktop.

Select an option

Save rebolyte/d14263f875a61ffe21004e8b590acd47 to your computer and use it in GitHub Desktop.
def my_reduce(func, lst):
#base case
if (len(lst) == 1):
return lst[0]
#recurse
temp = func(lst[0], lst[1])
return my_reduce(func, [temp] + lst[2:])
function myReduce(cb, initial, arr) {
let acc = initial; // would need to clone ref vals
for (let i = 0, l = arr.length; i < l; i++) {
if (typeof acc === 'undefined') {
acc = arr[i];
continue;
}
acc = cb(acc, arr[i]);
}
return acc;
}
let a = [1, 2, 3];
let sum = myReduce.bind(null, (acc, cur) => acc + cur, undefined);
console.log(sum(a)); // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment