在 Scala 中将 Map[String,List[String]] 扩展为笛卡尔积
Expand Map[String,List[String]] into Cartesian Product in Scala
我正在寻找基于地图类型的 Expand a Set[Set[String]] into Cartesian Product in Scala 版本:
我想开始:
val values = Map(
"id" -> List("Paul","Joe"),
"number" -> List("1","2","3"),
"Type" -> List("A","B","C")
)
并计算:
val final = Set(
Map("id" -> "Paul", "number" -> "1", "Type" -> "A" ),
Map("id" -> "Paul", "number" -> "1", "Type" -> "B" ),
Map("id" -> "Paul", "number" -> "1", "Type" -> "C" ),
Map("id" -> "Paul", "number" -> "1", "Type" -> "A" ),
Map("id" -> "Paul", "number" -> "1", "Type" -> "B" ),
Map("id" -> "Paul", "number" -> "1", "Type" -> "C" ),
Map("id" -> "Paul", "number" -> "2", "Type" -> "A" ),
Map("id" -> "Paul", "number" -> "2", "Type" -> "B" ),
Map("id" -> "Paul", "number" -> "2", "Type" -> "C" ),
....
Map("id" -> "Joe", "number" -> "3", "Type" -> "B" ),
Map("id" -> "Joe", "number" -> "3", "Type" -> "C" )
)
我尝试改造以下代码
def combine[A](xs: Traversable[Traversable[A]]): Seq[Seq[A]] =
xs.foldLeft(Seq(Seq.empty[A])){
(x, y) => for (a <- x.view; b <- y) yield a :+ b }
但我在理解如何构建要添加到大 Set() 中的所有地图时遇到了一些问题
你可以这样做:
(但请注意,对于较大的地图,这会消耗大量内存,可能值得考虑生成一个 LazyList)
def cartesianProductMap[A, B](data: Map[A, List[B]]): List[Map[A, B]] =
data.foldLeft(Map.empty[A, B] :: Nil) {
case (acc, (key, values)) =>
values.flatMap { b =>
acc.map(_ + (key -> b))
}
}
代码 运行 Scastie.
顺便说一句,如果你使用 cats 你可以这样做:values.to(SortedMap).sequence
我正在寻找基于地图类型的 Expand a Set[Set[String]] into Cartesian Product in Scala 版本:
我想开始:
val values = Map(
"id" -> List("Paul","Joe"),
"number" -> List("1","2","3"),
"Type" -> List("A","B","C")
)
并计算:
val final = Set(
Map("id" -> "Paul", "number" -> "1", "Type" -> "A" ),
Map("id" -> "Paul", "number" -> "1", "Type" -> "B" ),
Map("id" -> "Paul", "number" -> "1", "Type" -> "C" ),
Map("id" -> "Paul", "number" -> "1", "Type" -> "A" ),
Map("id" -> "Paul", "number" -> "1", "Type" -> "B" ),
Map("id" -> "Paul", "number" -> "1", "Type" -> "C" ),
Map("id" -> "Paul", "number" -> "2", "Type" -> "A" ),
Map("id" -> "Paul", "number" -> "2", "Type" -> "B" ),
Map("id" -> "Paul", "number" -> "2", "Type" -> "C" ),
....
Map("id" -> "Joe", "number" -> "3", "Type" -> "B" ),
Map("id" -> "Joe", "number" -> "3", "Type" -> "C" )
)
我尝试改造以下代码
def combine[A](xs: Traversable[Traversable[A]]): Seq[Seq[A]] =
xs.foldLeft(Seq(Seq.empty[A])){
(x, y) => for (a <- x.view; b <- y) yield a :+ b }
但我在理解如何构建要添加到大 Set() 中的所有地图时遇到了一些问题
你可以这样做:
(但请注意,对于较大的地图,这会消耗大量内存,可能值得考虑生成一个 LazyList)
def cartesianProductMap[A, B](data: Map[A, List[B]]): List[Map[A, B]] =
data.foldLeft(Map.empty[A, B] :: Nil) {
case (acc, (key, values)) =>
values.flatMap { b =>
acc.map(_ + (key -> b))
}
}
代码 运行 Scastie.
顺便说一句,如果你使用 cats 你可以这样做:values.to(SortedMap).sequence