我已经建立了一个这样的抽象:
class Thing(val messages: Map[String, Seq[String]]) {
def and(that: Thing): Thing = {
new Thing(this.messages ++ that.messages)
}
}
this
的地图String -> Seq[String]
需要与that
的地图合并。
这是我能想到的最好的方法:
def and(that: Thing): Thing = {
val keys = this.messages.keys ++ that.messages.keys
val tuples = keys map {
case key: String =>
val theseMessages = this.messages.getOrElse(key, Seq[String]())
val thoseMessages = that.messages.getOrElse(key, Seq[String]())
(key, theseMessages ++ thoseMessages)
}
new Thing(tuples.toMap)
}