用于在 map 函数中提取元组的用法

这三个地图功能是等效的,因此请使用你的团队最易读的变体。

val numberNames = Map(1 -> "One", 2 -> "Two", 3 -> "Three")

// 1. No extraction
numberNames.map(it => s"${it._1} is written ${it._2}" )

// 2. Extraction within a normal function
numberNames.map(it => {
    val (number, name) = it
    s"$number is written $name"
})

// 3. Extraction via a partial function (note the brackets in the parentheses)
numberNames.map({ case (number, name) => s"$number is written $name" })

partial 函数必须匹配所有输入 :任何不匹配的情况都会在运行时抛出异常。