Given,
case class User(name: String, roles: List[String])
val users: List[User] = ...
I’d like to calculate a map —
val roleToUsers: Map[String, List[User]] = ???
I could do the concise:
(for (user <- users; role <- user.roles) yield (role, user)).groupBy(_._1).mapValues(_.map(_._2))
But the underscores make it a bit too cryptic for my liking. Is there a neater way to do this that doesn’t make it much more verbose?
Edit: List[Role] -> List[User]
Maybe this:
Or, without for-comprehensions and with a minor optimization