Yann Moisan
Yann Moisan

Reputation: 8281

How to explain Map.map result

When I look a the Map.map scaladoc, I can see

map[B](f: (A) ⇒ B): Map[B]

But the simple following code doesn't return a Map

scala> Map("answer" -> 42).map { case(k, v) => v }
res40: scala.collection.immutable.Iterable[Int] = List(42)

Can you explain ?

Upvotes: 2

Views: 257

Answers (2)

som-snytt
som-snytt

Reputation: 39577

The simple answer is that you need key-value pairs to build Maps.

scala> Map("answer" -> 42).map { case(k, v) => (k, 43) }
res6: scala.collection.immutable.Map[String,Int] = Map(answer -> 43)

Upvotes: 2

Ende Neu
Ende Neu

Reputation: 15783

Scaladocs are simplified, if you want to see the full signature you have to expand and click on show full signature and you'll see that the real one is this:

def map[B, That](f: ((A, B)) ⇒ B)(implicit bf: CanBuildFrom[Map[A, B], B, That]): That

Which allows to return a That which can be a Map but also a List.

Upvotes: 3

Related Questions