1

我正在使用映射将某些值与元组 (Int, Double) 相关联,其中 int 是它们出现的顺序和它们显示的次数的两倍(它不是,但是使用 int 和 double 来区分这样更清晰)

棘手的部分是我想为元组的每个元素使用不同的幺半群,对于 int 我想保持最小值,记住第一次出现,而对于双倍我想使用加法幺半群所以对于现有的键我们有:

val map1 = Map("a" -> (1, 5.0), "b" -> (2, 4.0), "c" -> (3, 8.0))
val map2 = Map("b" -> (4, 1.0))

val merge = map1.toMap |+| map2.toMap
// Map(a -> (1, 5.0), b -> (2, 5.0), c -> (3, 8.0))

对于新密钥,我们有:

val map2 = Map("d" -> (4, 1.0))

val merge2 = map1.toMap |+| map2.toMap
// Map(a -> (1, 5.0), b -> (2, 4.0), c -> (3, 8.0), d -> (4, 1.0))

我找不到这样做的方法,我显然可以使用加法 monoid,我可以使用 minval ,但我看不到如何组合它们。任何帮助表示赞赏!谢谢

4

2 回答 2

1

您可以scalaz.std.tuple.tuple2Monoid显式使用您想要的两个幺半群:

import scalaz.Monoid

implicit val countMonoid: Monoid[(Int, Double)] = scalaz.std.tuple.tuple2Monoid(
  Monoid.instance[Int](math.min(_, _), Int.MaxValue),
  Monoid.instance[Double](_ + _, 0)
)

进而:

scala> import scalaz.std.map._, scalaz.syntax.monoid._
import scalaz.std.map._
import scalaz.syntax.monoid._

scala> val map1 = Map("a" -> (1, 5.0), "b" -> (2, 4.0), "c" -> (3, 8.0))
map1: scala.collection.immutable.Map[String,(Int, Double)] = Map(a -> (1,5.0), b -> (2,4.0), c -> (3,8.0))

scala> val map2 = Map("b" -> (4, 1.0))
map2: scala.collection.immutable.Map[String,(Int, Double)] = Map(b -> (4,1.0))

scala> val merge = map1.toMap |+| map2.toMap
merge: scala.collection.immutable.Map[String,(Int, Double)] = Map(a -> (1,5.0), b -> (2,5.0), c -> (3,8.0))
scala> val map2 = Map("d" -> (4, 1.0))
map2: scala.collection.immutable.Map[String,(Int, Double)] = Map(d -> (4,1.0))

scala> val merge2 = map1.toMap |+| map2.toMap
merge2: scala.collection.immutable.Map[String,(Int, Double)] = Map(a -> (1,5.0), b -> (2,4.0), c -> (3,8.0), d -> (4,1.0))

但是,这并不理想,因为该类型(Int, Double)可用于表示许多不同的事物,并且您刚刚定义了一个可能会出现在您或您的用户未预料到的地方的 monoid 实例。我个人会改用案例类:

case class Count(order: Int, number: Double)

然后在Count伴生对象中定义实例,显式地或通过countMonoid上面的和一个IsoSet[Count, (Int, Double)].

于 2018-12-14T12:54:37.467 回答
0

我跟着 Travis Brown 提出了一个围绕案例类构建的解决方案,以防止从新的幺半群溢出到每个 (Int, Double)

import scalaz._, Scalaz._, Isomorphism._
import scalaz.Monoid
import scalaz.std.map._, scalaz.syntax.monoid._

case class MonoidFromIsorphism[F, G](iso: F <=> G)(
  implicit val G: Monoid[G]
) extends IsomorphismMonoid[F, G]

case class TrafficCount(order: Int, number: Double)

object TrafficCount {
  implicit val countMonoid: Monoid[(Int, Double)] = scalaz.std.tuple.tuple2Monoid(
    Monoid.instance[Int](math.min(_, _), Int.MaxValue),
    Monoid.instance[Double](_ + _, 0)
  )
  implicit object TrafficCountMonoid extends MonoidFromIsorphism(
    new IsoSet[TrafficCount, (Int, Double)] {
      def to = (TrafficCount.unapply _) andThen (_.get)
      def from = (TrafficCount.apply _).tupled
    }
  )
}

它按预期工作:

val map1 = Map("a" -> TrafficCount(1, 5.0), "b" -> TrafficCount(2, 4.0), "c" -> TrafficCount(3, 8.0))
val map2 = Map("b" -> TrafficCount(4, 1.0))
val map3 = Map("d" -> TrafficCount(4, 1.0))

scala> val merge = map1.toMap |+| map2.toMap
merge: scala.collection.immutable.Map[String,TrafficCount] = Map(a -> TrafficCount(1,5.0), b -> TrafficCount(2,5.0), c -> TrafficCount(3,8.0))

scala> val merge2 = map1.toMap |+| map2.toMap
merge2: scala.collection.immutable.Map[String,TrafficCount] = Map(a -> TrafficCount(1,5.0), b -> TrafficCount(2,5.0), c -> TrafficCount(3,8.0))

事实上,我们可以检查幺半群的零:

scala> mzero[TrafficCount]
res0: TrafficCount = TrafficCount(2147483647,0.0)

它在不应该的地方不起作用:

val map_1 = Map("a" -> (1, 5.0), "b" -> (2, 4.0), "c" -> (3, 8.0))
val map_2 = Map("b" -> (4, 1.0))
val map_3 = Map("d" -> (4, 1.0))

scala> val merge_1 = map_1.toMap |+| map_2.toMap
<console>:38: error: value |+| is not a member of scala.collection.immutable.Map[String,(Int, Double)]
于 2018-12-15T21:56:25.643 回答