我是scala的新手,我正在尝试使用现有列表创建一个列表。
我目前的名单
val votesByLang: scala.collection.immutable.Map[String,Seq[(String, Int)]] = Map(pythom -> List((pythom,10)), scala -> List((scala,1), (scala,10), (scala,1)), java -> List((java,4)))
votesByLang.map{
case (witch,counts) => (witch,counts.foldLeft(0)((x,y)=>x+y))
}.toSeq
这会产生错误:
<console>:13: error: overloaded method value + with alternatives:
(x: Int)Int <and>
(x: Char)Int <and>
(x: Short)Int <and>
(x: Byte)Int
cannot be applied to ((String, Int))
votesByLang.map{case (witch,counts)=>(witch,counts.foldLeft(0) ((x,y)=>x+y))}.toSeq
但如果我使用_用于任何相同的工作正常和结果
votesByLang.map{
case (witch,counts) => (witch,counts.foldLeft(0)((_+_._2)))
}.toSeq
结果: Seq [(String,Int)] = Vector((pythom,10),(scala,12),(java,4))
第一种方法有什么问题
答案 0 :(得分:0)
正如Marth建议以下作品。
votesByLang.map{
case (witch,counts)=>(witch,counts.foldLeft(0)((x:Int,y)=>x+y._2))}.toSeq