Querying a Collection Using For-Comprehensions in Scala

Extracting elements from a collection of objects using Scala's for-comprehensions.
// Defining a case class
case class Book(title: String, author: List[String])

// Creating Book instances
val book1: Book = Book("War & Peace", List("Leo Tolstoy"))
val book2: Book = Book("Crime & Punishment", List("Fyodor Dostoievski"))
val book3: Book = Book("Rebecca", List("Daphne du Maurier"))
val book4: Book = Book("Les Miserables", List("Victor Hugo"))
val book5: Book = Book("The Count of Monte Cristo", List("Charles Dumas"))
val book6: Book = Book("The Passion According to G.H.", List("Clarice Lispector"))
val book7: Book = Book("Ham on Rye", List("Charles Bukowski"))
val book8: Book = Book("The Brothers Karamazov", List("Fyodor Dostoievski"))

// Defining a list of books
val myBooks: List[Book] = List(book1, book2, book3, book4, book5, book6, book7, book8)

// Getting all books starting with "The"
for
    b <- myBooks
    if b.title.startsWith("The")
yield b.title

// res0: List[String] = List(The Count of Monte Cristo, The Passion According to G.H., The Brothers Karamazov)
Scala
Scala is a strong, statically typed, high-level, general-purpose programming language that supports both object-oriented programming and functional programming. It…
Programming is an essential tool for innovation in the digital age, with businesses and organizations across all industries relying…
We could spend our entire developer’s life using just the basic concepts of any programming language we choose to…
Formally, a type class is a type-system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables…

Request Full Resume