Scala:迭代时提取元组值

Scala: Extracting tuple values when iterating

假设我有这张地图:Map[String, Int]

为了迭代它的值,我必须这样做:

myMap.foreach(t => {
    val word = t._1
    val number = t._2
    //do stuff with word and number here
})

有没有办法做这样的事情:

myMap.foreach( (word, number) => {
    //do stuff with word and number here
})

使用 scala 2.13.2 atm

您可以使用模式匹配:

myMap.foreach { case (word, number) => ... }

或为:

for((word, number) <- myMap) {
  ...
}