Last active
November 12, 2018 17:26
-
-
Save rebolyte/d14263f875a61ffe21004e8b590acd47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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:]) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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