Scala: how to destructure a basic map entry

Viewed 1675

Here is a vanilla scala map:

scala> val m = Map( 'a'-> '1', 'b' -> 2)
m: scala.collection.immutable.Map[Char,AnyVal] = Map(a -> 1, b -> 2)

The Map iterator() method returns a tuple representing the (key,value). So if we want to see, say, just the values of the map we can do this:

scala> m.map( a => a._2)
res0: scala.collection.immutable.Iterable[AnyVal] = List(1, 2)

But how do we destructure the map entry ? The following does not work:

scala> m.map( (a,b) =>  b)
<console>:10: error: wrong number of parameters; expected = 1
              m.map( (a,b) =>  b)
                       ^
2 Answers
Related