Skip to content

Instantly share code, notes, and snippets.

@Restuta
Created October 8, 2021 19:55
Show Gist options
  • Save Restuta/b6913cdd6f8ce7649510195abbebd3e6 to your computer and use it in GitHub Desktop.
Save Restuta/b6913cdd6f8ce7649510195abbebd3e6 to your computer and use it in GitHub Desktop.
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