Last active
April 8, 2018 02:48
-
-
Save hannestyden/d59fb4a063ff8a169c7b to your computer and use it in GitHub Desktop.
What is my name?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 one step closer to divine purity. 😉
... but the question still remains: What is its name?
@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
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