【发布时间】:2023-03-21 09:20:02
【问题描述】:
我有references的列表:
val references: List[String]= List("S","R")
我也有variables,即:
val variables: Map[String,List[String]]=("S"->("a","b"),"R"->("west","east"))
references 是变量映射的键列表。
我想构造一个函数:
def expandReplacements(references:List[String],variables:Map[String,List[String]]):List[Map(String,String)]
这个函数基本上应该创建返回以下组合
List(Map("S"->"a"),("R"->"west"),Map("S"->"a"),("R"->"east"),Map("S"->"b"),("R"->"west"),Map("S"->"b"),("R"->"east"))
我试过这样做:
val variables: Map[String,List[String]] = Map("S" -> List("a", "b"), "R" -> List("east", "central"))
val references: List[String] = List("S","R")
def expandReplacements(references: List[String]): List[Map[String, String]] =
references match {
case ref :: refs =>
val variableValues =
variables(ref)
val x = variableValues.flatMap { variableValue =>
val remaining = expandReplacements(refs)
remaining.map(rem => rem + (ref -> variableValue))
}
x
case Nil => List.empty
}
【问题讨论】: