【发布时间】:2018-05-07 13:34:51
【问题描述】:
我有两个序列,即prices: Seq[Price] 和overrides: Seq[Override]。我需要对它们做一些魔术,但只针对基于共享 id 的子集。
所以我通过groupBy将它们都分组为Map:
我通过以下方式进行分组:
val pricesById = prices.groupBy(_.someId) // Int => Seq[Cruise]
val overridesById = overrides.groupBy(_.someId) // // Int => Seq[Override]
我希望能够通过flatMap 创建我想要的序列:
val applyOverrides = (someId: Int, prices: Seq[Price]): Seq[Price] => {
val applicableOverrides = overridesById.getOrElse(someId, Seq())
magicMethod(prices, applicableOverrides) // returns Seq[Price]
}
val myPrices: Seq[Price] = pricesById.flatMap(applyOverrides)
我预计myPrices 只包含一个大的Seq[Price]。
但我在 flatMap 方法中遇到了一个奇怪的类型不匹配,我无法解决 NonInferedB。
【问题讨论】:
标签: scala dictionary tuples sequence flatmap