Filtering before mapping

Let's modify our example slightly, building on this lazy mapping pattern. It's common to filter collections before mapping them so that they only include relevant values:

myList
.toSeq()
.filter(v => v.get('age') > 35)
.map(v => v.update('first', capitalize))
.map(v => v.update('last', capitalize))
.map(v => [v.get('first'), v.get('last')].join(' '))
.forEach(v => console.log('name', v));
// -> name Joe Brown
// -> name Mary Wise

In this case, we only want capitalized names where age is over 35. Once a filter match is found, it flows through each of the map() calls.

When in doubt about how your sequence is lazily evaluating values, add logging to your iteratee and predicate functions so that you can see the order in which things are called. Stepping through the sequence using a debugger works too, if that's what you're more accustomed to.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.181.52