A more functional way to implement map, reduce, and filter:
Recursive Map
let map = (fn, list) =>
!list.length ? [] : [fn(list[0])].concat(map(fn, list.slice(1)));
map(x => x + 1, [1, 2, 3, 4]); // [2, 3, 4, 5]
## Recursive Reduce
javascript
let reduce = (fn, value, list) =>
!list.length ? value : reduce(fn, fn(value, list[0]), list.slice(1));
reduce((x, y) => x + y, 0, [1, 2, 3, 4]); // 10
## Recursive Filter
javascript
let filter = (predicate, list) =>
!list.length ? [] :
(predicate(list[0]) ? [list[0]] : [])
.concat(filter(predicate, list.slice(1)));
filter(x => x > 2, [1, 2, 3, 4, 5]); // [3, 4, 5]
#javascript#functional
About Hemanth HM
Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.