Created
September 25, 2017 19:19
-
-
Save glifchits/3a4e498597b8fa4b81c3b4f3d50cfab5 to your computer and use it in GitHub Desktop.
map, filter from reduce
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
function mapper(fn, arr) { | |
return arr.reduce((acc, i) => [...acc, fn(i)], []) | |
} | |
function filterer(pred, arr) { | |
return arr.reduce((acc, i) => pred(i) ? [...acc, i] : acc, []) | |
} | |
console.log(mapper( x => x ** 2, [1, 2, 3] )) // => [1, 4, 9] | |
console.log(filterer( x => x >= 2, [1, 2, 3] )) // => [2, 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment