O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required