Created
October 8, 2021 19:55
-
-
Save Restuta/b6913cdd6f8ce7649510195abbebd3e6 to your computer and use it in GitHub Desktop.
map/reduce/pipe in 3m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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