用於在 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 函式必須匹配所有輸入 :任何不匹配的情況都會在執行時丟擲異常。