Skip to content

Instantly share code, notes, and snippets.

View hch12907's full-sized avatar

Hoe Hao Cheng hch12907

  • Malaysia
View GitHub Profile
@hch12907
hch12907 / map_reduce.js
Created June 11, 2017 08:10
recursive implementation of map and reduce using only lambdas and Array.prototype.pop()
let map = (func, arr) =>
arr.length == 0 ?
[] : ((x, y) => [...y, x])(func(arr.pop()), map(func, arr))
// if you want to pull an one-liner:
// let map = (func, arr) => arr.length == 0 ? [] : ((x, y) => [...y, x])(func(arr.pop()), map(func, arr))
let reduce = (func, initial_value, arr) =>
arr.length == 0 ?
initial_value : ((x, y) => func(y, x))(arr.pop(), reduce(func, initial_value, arr))