Skip to content

Instantly share code, notes, and snippets.

@smolijar
Created October 23, 2019 06:30
Show Gist options
  • Save smolijar/e1097322e190132dcbeea359f72fcd39 to your computer and use it in GitHub Desktop.
Save smolijar/e1097322e190132dcbeea359f72fcd39 to your computer and use it in GitHub Desktop.
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