Skip to content

Instantly share code, notes, and snippets.

@tpolecat
Last active December 20, 2015 12:09
Show Gist options
  • Save tpolecat/6128771 to your computer and use it in GitHub Desktop.
Save tpolecat/6128771 to your computer and use it in GitHub Desktop.
TPayne's problem from http://dpaste.com/1325681/
object TPayne extends App {
val m = Map(
"2" -> List(List("1")),
"3" -> List(List("9"), List("5", "6", "7", "8")),
"1" -> List(List("8", "9"), List("2"), List("2", "3")))
def combos[A, B](xs: Map[A, List[B]]): List[Map[A, B]] =
(List(Map[A, B]()) /: xs)((m, p) => p._2.flatMap(v => m.map(_ + (p._1 -> v))))
combos(m).foreach(println)
// Map(2 -> List(1), 3 -> List(9), 1 -> List(8, 9))
// Map(2 -> List(1), 3 -> List(5, 6, 7, 8), 1 -> List(8, 9))
// Map(2 -> List(1), 3 -> List(9), 1 -> List(2))
// Map(2 -> List(1), 3 -> List(5, 6, 7, 8), 1 -> List(2))
// Map(2 -> List(1), 3 -> List(9), 1 -> List(2, 3))
// Map(2 -> List(1), 3 -> List(5, 6, 7, 8), 1 -> List(2, 3))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment