Skip to Content
Course content

49: Practice Exercise: Processing a Dataset with For-Comprehensions

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

Think of a for-comprehension like a professional quality-control line at a gourmet chocolate factory. You start with a big conveyor belt of raw truffles. First, a sensor kicks off any that are misshapen (that's your filtering). Then, a mechanical arm wraps the survivors in gold foil (that's your transformation). Finally, they are placed into a fancy gift box for the customer. You don't treat each truffle as a separate, disconnected project; you define a single, fluid pipeline that the chocolate moves through from start to finish.

In Scala, for-comprehensions do exactly this for your data. Instead of nesting three different .flatMap and .filter calls—which, let's be honest, quickly becomes a readability nightmare—you describe the sequence of events. I've always found that writing a for-comprehension feels more like writing a recipe than writing code. You're telling Scala: "Take this, make sure it meets this criteria, turn it into that, and give me the result."

Sifting Through the Noise

When you're dealing with real-world datasets, you rarely get "clean" data. You usually have a mix of Option, List, or Either types. The magic of the for-comprehension is that it flattens these wrappers. If you have a list of users, and each user has an Option[Address], a for-comprehension lets you iterate over the users and the addresses in one go. If an address is None, Scala simply skips that iteration. It's a silent, elegant way to handle missing data without writing if (address.isDefined) a dozen times.

case class User(id: Int, name: String, email: Option[String])
val users = List(
  User(1, "Alice", Some("alice@example.com")),
  User(2, "Bob", None),
  User(3, "Charlie", Some("charlie@example.com"))
)

// We only want a list of emails for people who actually have one
val emails = for {
  user  <- users            // Extract each user from the list
  email <- user.email        // Extract the email from the Option (skips None)
} yield email

// Result: List("alice@example.com", "charlie@example.com")

Adding Logic to the Pipeline

Now, sometimes just extracting data isn't enough. You need to apply business rules. In a for-comprehension, we do this with "guards"—essentially if statements placed right inside the block. I like to think of these as the "bouncers" of your data pipeline. If the data doesn't meet the requirement, it's not getting through to the yield statement.

Let's say we're processing a set of financial transactions. We only care about transactions that are flagged as "completed" and are over a certain dollar amount. Instead of chaining multiple .filter calls, we can bake that logic directly into the flow.

case class Transaction(id: String, amount: Double, status: String)
val txns = List(
  Transaction("T1", 150.0, "completed"),
  Transaction("T2", 20.0, "completed"),
  Transaction("T3", 300.0, "pending"),
  Transaction("T4", 500.0, "completed")
)

val highValueCompleted = for {
  t <- txns
  if t.status == "completed" && t.amount > 100.0
} yield s"Transaction ${t.id} is a big win!"

// Result: List("Transaction T1 is a big win!", "Transaction T4 is a big win!")

The Final Yield

The yield keyword is where the transformation actually happens. Everything before yield is about gathering and filtering; the yield is about shaping. This is where you turn your raw objects into the final format your API or UI expects. You can return a new object, a formatted string, or even a tuple. Just remember: whatever the "outermost" wrapper was (in these cases, the List), the result of the for-comprehension will be wrapped in that same type.




📋 Practical Task

Exercise: Filtering and Formatting High-Value Customer Transactions

You are building a reporting tool for an e-commerce platform. You have a list of Order objects and a Map that associates customer IDs with their names. Some orders are missing customer IDs (represented as Option[Int]), and some customer IDs in the orders might not exist in your customer database.

Your goal: Use a for-comprehension to create a list of strings for all orders that are over $100.00 and have a valid customer name. The resulting strings should follow the format: "Customer [Name] spent $[Amount]".

case class Order(id: Int, customerId: Option[Int], amount: Double)
case class Customer(id: Int, name: String)

val orders = List(
  Order(101, Some(1), 150.0), // Valid, High value
  Order(102, Some(2), 50.0),  // Too low
  Order(103, None, 200.0),    // Missing customer ID
  Order(104, Some(3), 300.0), // Customer ID 3 doesn't exist in map
  Order(105, Some(1), 120.0)  // Valid, High value
)

val customers = Map(
  1 -> Customer(1, "Alice"),
  2 -> Customer(2, "Bob")
)

// TODO: Implement the for-comprehension here
val report = for {
  // Your code here
} yield {
  // Your code here
}

println(report) 
// Expected Output: List("Customer Alice spent $150.0", "Customer Alice spent $120.0")
Rating
0 0

There are no comments for now.

to be the first to leave a comment.