concept comprehension in category scala

This is an excerpt from Manning's book Get Programming with Scala.
Scala also offers a more powerful alternative to the classic for loop you have just seen, called for-comprehension: you are going to learn about it when discussing the class Option in unit 4.
In the previous lesson, you have learned how to use the functions map, flatten, and flatMap to manipulate optional values. In this lesson, you are also going to discover a more readable and elegant way of combining instances of Option thanks to a new type of statement, called “for-comprehension”. You are also going to see how you can integrate Boolean conditions in it to control further how the values are chained together. Finally, I’ll give you an overview of other useful operations implemented for Option, such as isDefined, getOrElse, find, and exists. In the capstone, you will use the function getOrElse on Option to provide an alternative message for your HTTP request when a draw of the game “Paper, Rock, Scissors, Lizard, Spock!” occurs.
Listing 24.1 shows you how you can re-implement your ownerDrivingLicense function using for-comprehension:
Listing 24.1: Example of for-comprehension
def ownerDrivingLicense(optCar: Option[Car]): Option[String] = for { car <- optCar person <- car.owner drivingLicense <- person.drivingLicense } yield drivingLicenseThe expressions optCar, car.owner, and person.drivingLicense are all producing an optional value: they have type Option[Car], Option[Person], Option[String] respectively. The values car, person, and drivingLicense are their corresponding extracted values: they have type Car, Person, and String. You have now have encountered a new keyword, called yield: it returns a value that you can compute using the extracted values wrapped into an Option. In this case, it returns the value of drivingLicense as optional value. As soon as the for-comprehension finds an absent optional value (e.g., car.owner is None), it evaluates the entire expression as None.
You can use a for-comprehension statement on every class that has a flatMap function: you’ll discover soon that this applies to other types other than Option. Thanks to it, your code can avoid an excessive nested structure, which makes it easier to read and understand. You can rewrite the earlier example of chained five or more operations as follows:
Listing 24.2 shows you how you can achieve the same using for-comprehension:
Listing 24.2: Example of for-comprehension with if condition
def ownerDrivingLicense(optCar: Option[Car], ownerName: String): Option[String] = for { car <- optCar person <- car.owner if person.name == ownerName //#A drivingLicense <- person.drivingLicense } yield drivingLicense.toUppercase //#BYou can modify the yield values in a for-comprehension statement (e.g., drivingLicense.toUppercase), as well as adding additional conditions to stop the combination of values by adding the keyword if followed by a Boolean expression (e.g., if person.name == ownerName). Have a look at figure 24.1 for a summary on for-comprehension statements in Scala.
Figure 24.1: Summary of for-comprehension on Option: they allow you chain optional operation together thanks to their syntactic sugar to rewrite nested map and flatMap function calls.
![]()