【发布时间】:2021-06-25 12:39:30
【问题描述】:
我正在寻找基于地图类型的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() 中
【问题讨论】:
标签: list scala combinations cartesian-product cartesian