Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exercises Bookstore zip

Use Case 1: Creating Pairs of Book Titles and Prices with zip

val titles = library.map(_.title)
val prices = library.map(_.price)
titles.zip(prices)

Use Case 2: Creating Pairs of Book Titles and Their Indexes with zipWithIndex

  library.map(_.title).zipWithIndex

Use Case 3: Matching Authors to Their Books with zip

  val authors = library.map(_.author.name).distinct
  val booksByAuthor = authors.map { author =>
    val books = library.filter(_.author.name == author).map(_.title)
    (author, books)
  }
  booksByAuthor

Use Case 4: Pairing Book Titles with Publication Years with zip

  val titles = library.map(_.title)
  val years = library.map(_.year)
  titles.zip(years)

Use Case 5: Creating Pairs of Original and Discounted Prices with zip

  val originalPrices = library.map(_.price)
  val discountedPrices = originalPrices.map(price => price * (1 - discountRate))
  originalPrices.zip(discountedPrices)

Use Case 6: Creating a List of Book Titles with Their Indexes for Display Purposes with zipWithIndex

  library.map(_.title).zipWithIndex.map { case (title, index) => s"$index: $title" }