Making Strict Collections Lazy

You learned how to make a single variable lazy. Why stop there? You can make an entire collection lazy.

All the collections you worked with in Chapter 8, Collections are called strict collections. Actions on them are strictly performed upon call. That’s good at times, but there’s a benefit to being lazy. Let’s explore this with some examples.

In order to see the eager vs. lazy evaluation, we’ll use print statements to show what calls are being made in the next example. We start with a list of tuples where each element contains a name and an age value. We’re interested in selecting the first person from the list whose name starts with J and who is older than 17. Let’s program that with a strict collection first. ...

Get Pragmatic Scala now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.