Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Latest commit

 

History

History
60 lines (44 loc) · 1.39 KB

06-parameter-untupling.md

File metadata and controls

60 lines (44 loc) · 1.39 KB

­

­

­

­

PARAMETER UNTUPLING


Parameter Untupling

­

  • The need to write a pattern matching decomposition when mapping over a sequence of tuples is inconvenient.
  • Consider you have a list of pairs:
 val pairs = List(1, 2, 3).zipWithIndex
 // pairs: List[(Int, Int)] = List((1,0), (2,1), (3,2))
  • Imagine you want to map pairs to a list of Ints so that each pair of numbers is mapped to their sum.
  • The best way to do this in Scala 2 is with a pattern matching anonymous function
 pairs map {
   case (x, y) => x + y
 }
 // res1: List[Int] = List(1, 3, 5)

Parameter Untupling

­

  • Scala 3 now allows us to write:
 pairs map ((x, y) => x + y)
 // val res0: List[Int] = List(1, 3, 5)
  • Or it can be written as:
pairs.map(_ + _)
 // val res1: List[Int] = List(1, 3, 5)

Parameter Untupling

­

  • In this exercise we will use parameter untupling
    • Make sure you're positioned at exercise "parameter untupling"
    • Follow the exercise instructions provided in the README.md file in the code folder