Skip to content

Commit 25047c4

Browse files
committed
Add functional recursive reduce
1 parent f79280e commit 25047c4

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

JavaScript/e-reduce.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ const arr = [7, 10, 1, 5, 2];
55
const sum = (acc, val) => (count++, acc + val);
66
const res = arr.reduce(sum);
77
console.log({ res, count });
8+
9+
const reduce = (fn, acc, [cur, ...rest]) => cur === undefined ?
10+
acc : reduce(fn, fn(acc, cur), rest);
11+
12+
const res2 = reduce(sum, 0, arr);
13+
console.log({ res2 });

0 commit comments

Comments
 (0)