简体   繁体   English

使用scalaz进行简单聚合?

[英]simple aggregation with scalaz?

Here is my implementation of a simple aggregation. 这是我对简单聚合的实现。

  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)

Is there a smarter implementation using scalaz ? 使用scalaz是否有更明智的实现?

Finally got this one, leverging a bit some monoids 终于拿到了这一个,leverging有点某些类群

val output = input.toList.map { case (k, v) => Map((mapping(k), v)) }.reduce( _ |+| _)

Not sure if it's better in term of memory and/or CPU. 不确定在内存和/或CPU方面是否更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM