【发布时间】:2012-10-07 03:51:48
【问题描述】:
假设我有以下 Map[Int,Double]:
scala> map
res19: scala.collection.immutable.Map[Int,Double] = Map(1 -> 1.1, 2 -> 2.2)
我可以在上面运行以下 foldLeft:
scala> map.foldLeft("A")((initVal,x:(Int,Double)) => initVal + x._1)
res20: java.lang.String = A12
但我找不到将元组的值分配给命名变量的方法:
scala> map.foldLeft("A")((init,x:(a:Int,b:Double)) => init + x.a)
<console>:1: error: ')' expected but ':' found.
map.foldLeft("A")((init,x:(a:Int,b:Double)) => init + x.a)
^
这还能做到吗?
【问题讨论】: