Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这是我对简单聚合的实现。
val mapping = Map("AA" -> "A", "AB" -> "A", "B" -> "B") val input = Map("AA" -> 1, "AB" -> 1, "B" -> 1) val output = input.groupBy { case (k, _) => mapping(k) } .mapValues(_.values.sum)
使用 scalaz 是否有更智能的实现?
终于得到了这个,利用了一些幺半群
val output = input.toList.map { case (k, v) => Map((mapping(k), v)) }.reduce( _ |+| _)
不确定在内存和/或 CPU 方面是否更好。