2

According to the API, function signature is def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): List[B] with B being the type of the element of the returned collection

then why doesn't val xs = Map("a" -> List(11,111), "b" -> List(22,222)).flatMap[Int](_._2) compile? It is giving a 'wrong number of type parameters for method' error, seems to suggest the signature is different to what is in the API doc?

ekinrf
  • 59
  • 5

1 Answers1

6

Yes, the signature given in the API docs is intentionally wrong, to conceal the implicit CanBuildFrom parameter because it upsets people. Click "Full Signature" at the bottom of the method documentation to see the actual signature:

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

In this case, you can just leave off the type parameter.

scala> val xs = Map("a" -> List(11,111), "b" -> List(22,222)).flatMap(_._2)
xs: scala.collection.immutable.Iterable[Int] = List(11, 111, 22, 222)
Chris Martin
  • 30,334
  • 10
  • 78
  • 137