Skip to content

Instantly share code, notes, and snippets.

@hannestyden
Last active April 8, 2018 02:48
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hannestyden/d59fb4a063ff8a169c7b to your computer and use it in GitHub Desktop.
What is my name?
scala> def whatIsMyName[A,B](map: Map[A, Seq[B]]): List[(A, B)] =
| map.map { case (k, v) => v.map(k -> _) }.flatten.toList
whatIsMyName: [A, B](map: Map[A,Seq[B]])List[(A, B)]
scala> whatIsMyName(Map('a -> Seq(1,2), 'b -> Seq(3,4)))
res0: List[(Symbol, Int)] = List(('a,1), ('a,2), ('b,3), ('b,4))
@folone
Copy link

folone commented Feb 26, 2015

You can rewrite it as def whatIsMyName[A,B](map: Map[A, Seq[B]]): List[(A, B)] = map.flatMap{ case (k, v) => v.map(k -> _) }.toList

@hannestyden
Copy link
Author

@folone one step closer to divine purity. 😉

... but the question still remains: What is its name?

@hannestyden
Copy link
Author

@folone JFYI your suggestion would change the behavior to:

scala> def whatIsMyName2[A,B](map: Map[A, Seq[B]]): List[(A, B)] =
     | map.flatMap { case (k, v) => v.map(k -> _) }.toList
whatIsMyName2: [A, B](map: Map[A,Seq[B]])List[(A, B)]

scala> whatIsMyName2(Map('a -> Seq(1,2), 'b -> Seq(3,4)))
res2: List[(Symbol, Int)] = List(('a,2), ('b,4))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment