2

Possible Duplicate:
Tuple Unpacking in Map Operations

Lets say that I have the following Map[Int,Double]:

scala> map
res19: scala.collection.immutable.Map[Int,Double] = Map(1 -> 1.1, 2 -> 2.2)

I can run the following foldLeft on it:

scala> map.foldLeft("A")((initVal,x:(Int,Double)) => initVal + x._1)
res20: java.lang.String = A12

But I can't find a way to assign the tuple's values to named variables:

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)
                               ^

Can this even be done?

Community
  • 1
  • 1
Armin
  • 1,367
  • 1
  • 12
  • 17

1 Answers1

7

You could use case

map.foldLeft("A") {case (init, (a,b)) => init + a}
Ratan Sebastian
  • 1,892
  • 14
  • 23