Collect
In this blog post, you’ll learn -
- How to combine both map and filter into a single function.
Please read this post on Maps and Filters before continuing.
A common pattern in Scala is to filter items and then transforming (map) the filtered items. If you think about it, this is 2 iterations over the collection. Definitely, it can be optimized into a single iteration using the Collect method. Consider this example -
Kaushik Rangadurai
Code. Learn. Explore