Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
map/reduce/pipe in 3m
// map, reduce, pipe in 3 minutes
const reduce = (reducer, defaultAcc, list) => {
let acc = defaultAcc
for(let item of list) {
acc = reducer(acc, item)
}
return acc
}
reduce((acc, x) => acc + x, 0, [1,2,8]) //?
const map = (func, list) => reduce(
(acc, item) => {
acc.push(func(item))
return acc
},
[],
list
)
map(x => x + 1, [2,2,2]) //?
const pipe = funcs => input => reduce(
(acc, func) => func(acc),
input,
funcs
)
pipe([
x => x + 'A',
x => x + 'B'
])('hello ') //?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment