Created
October 23, 2019 06:30
-
-
Save smolijar/e1097322e190132dcbeea359f72fcd39 to your computer and use it in GitHub Desktop.
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
const numbers = [ 1, 2, 3 ] | |
const isOdd = num => { | |
console.log(['isOdd', num]) | |
return num % 2 === 0 | |
} | |
const double = num => { | |
console.log(['double', num]) | |
return num * 2 | |
} | |
console.log('.filter.map') | |
numbers | |
.filter(isOdd) | |
.map(double) //=> [ 2, 6 ] | |
console.log('filterMap') | |
const filterMap = (filter, map) => (acc, x) => { | |
if (filter(x)) { | |
const value = map(x) | |
acc.push(value) | |
} | |
return acc | |
} | |
numbers.reduce(filterMap(isOdd, double), []) //=> [ 2, 6 ] | |
// --> output | |
// .filter.map | |
// ["isOdd", 1] | |
// ["isOdd", 2] | |
// ["isOdd", 3] | |
// ["double", 2] | |
// filterMap | |
// ["isOdd", 1] | |
// ["isOdd", 2] | |
// ["double", 2] | |
// ["isOdd", 3] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment