Skip to Content
Course content

60: View and Lazy Collections

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I was looking through some legacy code yesterday and found a pattern that drove me crazy: a massive list of data being filtered and mapped three or four times in a row, only for the developer to call .take(5) at the very end. It seemed like such a waste of CPU cycles. I decided to spin up a REPL to see exactly how much waste we're talking about, and I want to walk you through it.

The Cost of Being Eager

Let's pretend we're dealing with a stream of sensor readings. Each reading is just an integer, but in a real app, these might be heavy objects. I'll create a list of 1,000 readings and try to find the first 3 readings that are even and then multiply them by 10.

val readings = (1 to 1000).toList

val result = readings
  .filter { x => 
    println(s"Filtering $x")
    x % 2 == 0 
  }
  .map { x => 
    println(s"Mapping $x")
    x * 10 
  }
  .take(3)

println(s"Final result: $result")

When I ran this, my console exploded. I saw "Filtering 1", "Filtering 2", "Mapping 2", all the way up to 1,000. Even though I only wanted the first 3 results, Scala's default collections are eager. The filter method finished its work on the entire 1,000-element list and created a brand new intermediate list. Then map did the same thing, creating yet another list. Only after all that heavy lifting did take(3) swoop in and throw away 497 elements.

That's a lot of wasted effort. If that map function were calling an API or hitting a database, we'd be in serious trouble.

Flipping the Switch to Lazy

Now, I wondered if there was a way to tell Scala, "Don't actually do this until I absolutely need the value." This is where .view comes in. Let's try the exact same logic, but I'll slide .view in right at the start.

val readings = (1 to 1000).toList

val result = readings.view
  .filter { x => 
    println(s"Filtering $x")
    x % 2 == 0 
  }
  .map { x => 
    println(s"Mapping $x")
    x * 10 
  }
  .take(3)

println("The view is defined, but has anything happened yet?")
println(s"Final result: ${result.toList}")

The difference here is staggering. When I ran this, the first thing printed was: "The view is defined, but has anything happened yet?"

Wait, what? The filter and map didn't run at all. By calling .view, I didn't actually perform any transformations; I created a "recipe" for transformations. The computations only started when I called .toList at the very end. And even then, the console only showed:

Filtering 1
Filtering 2
Mapping 2
Filtering 3
Filtering 4
Mapping 4
Filtering 5
Filtering 6
Mapping 6
Final result: List(20, 40, 60)

It stopped the moment it found three elements that satisfied the criteria. It didn't even look at element 7. This is the essence of lazy collections: they fuse the operations together and only execute them on-demand.

The Trade-off and the Concrete Return

You might be wondering why we don't just make everything a view. Well, views are essentially wrappers. If you iterate over a view multiple times, it might re-calculate the transformations every single time unless you "materialize" it back into a concrete collection.

If I had tried to print result twice without calling .toList, I would have seen those "Filtering" and "Mapping" logs repeat. It's a classic space-vs-time trade-off. We save memory and time on the initial pass by not creating intermediate lists, but we pay for it if we need to access that data repeatedly.

So, the rule of thumb I use is: use .view when you have a chain of transformations followed by a limiting operation (like take, find, or exists), or when the source collection is so massive that creating intermediate copies would trigger an OutOfMemoryError. Just remember to call .toList, .toSeq, or .toSet when you're done being lazy and actually need the data in hand.




📋 Practical Task

Efficiency Audit: Large Log Processor

You are given a list of 10,000 simulated log entries. Each entry is a string. Your goal is to find the first 5 "ERROR" logs that contain the word "Database", and transform them to uppercase for a report.

Implement the processLogs function using a View to ensure that the program does not process the entire list of 10,000 entries if the 5 matches are found early in the list. Prove the efficiency by adding a println inside the filter block to track how many elements are actually inspected.

val logs = List.fill(10000)("INFO: System heartbeat") ++ 
             List("ERROR: Database connection failed", 
                  "INFO: User logged in", 
                  "ERROR: Database timeout", 
                  "DEBUG: Cache hit", 
                  "ERROR: Database disk full", 
                  "ERROR: Database read error", 
                  "INFO: Update complete", 
                  "ERROR: Database write failed") 
             // Note: The errors are scattered; you'll need to shuffle or 
             // strategically place them to test your view.

def processLogs(logs: List[String]): List[String] = {
  // Your implementation here
}

// Expected output: A list of 5 uppercase error strings.
// Observation: The filter println should NOT run 10,000 times.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.