【问题标题】:scanLeft using HMap in shapelessscanLeft 在 shapeless 中使用 HMap
【发布时间】:2016-11-03 22:03:22
【问题描述】:

我使用 shapeless HMap 作为案例类的底层结构,充当共享同一父对象的其他对象的聚合器。父 trait 允许访问名为 arity 的 Int 值属性。我想在HMap 上使用scanLeft 来计算HList 中对象的累积aritys。我成功编写了Poly 对象,允许使用foldLeft 计算所有aritys 的总和,但是当尝试将相同的概念应用于scanLeft 时,它不再起作用了。

Q1:有人知道我应该如何修改pointAccumulate 以支持scanLeft 操作吗?我期待lookup 是这样的Nat(2) :: Nat(5) :: Nat(8) :: HNil(使用下面的示例)。

Q2:之后我将使用lookup 来搜索知道组合结构中的累积数量的元素的索引。鉴于此,是否可以使用 HList 或者我应该确保我得到 lookup 作为 List[Int]?

import shapeless._
import ops.hlist.{At, LeftFolder, LeftScanner}

object Point { type Point = (Double, Double) }

import Point._

sealed trait Shape {
    val arity: Int
}

case class Line(p0: Point, p1: Point) extends Shape {
    val arity = 2
}

case class Triangle(p0: Point, p1: Point, p2: Point) extends Shape {
    val arity = 3
}

case class Canvas[L <: HList](shapes: L)
                             (implicit
                              val ev: LUBConstraint[L, Shape],
                              val lf: LeftFolder.Aux[L, Int, pointAccumulate.type, Int],
                              val ls: LeftScanner[L, Int, pointAccumulate.type]) {

    lazy val parameterCount: Int = shapes.foldLeft(0)(pointAccumulate)

    lazy val lookup = shapes.scanLeft(0)(pointAccumulate)
}

object pointCount extends Poly1 {
    implicit def default[T <: Shape] = at[T](_.arity)
}

object pointAccumulate extends Poly2 {
    implicit def default[T <: Shape](implicit pt: pointCount.Case.Aux[T, Int]) =
        at[Int, T] { (i, p) => i + pointCount(p) }
}

object App {

    def main(args: Array[String]): Unit = {

        val l0 = Line((-2, 2), (2, -2))
        val tr1 = Triangle((0,0), (0, 1), (1, 0))
        val tr2 = Triangle((1,1), (1, 2), (2, 1))

        val c = Canvas(l0 :: tr1 :: tr2 :: HNil)
        println(c.lookup)
    }
}

【问题讨论】:

  • 对于scanLeft,Poly2参数必须切换:at[T, Int] { (p, i) =&gt; i + pointCount(p)
  • @devkat 你是对的,它适用于你的改变。谢谢你!如果需要,您可以将您的回复添加为答案

标签: scala shapeless


【解决方案1】:

我使用@devkat 的提示设法解决了这个问题。这是工作代码

import shapeless._
import ops.hlist.{At, LeftFolder, LeftScanner}

object Point { type Point = (Double, Double) }

import Point._

sealed trait Shape {
    val arity: Int
}

case class Line(p0: Point, p1: Point) extends Shape {
    val arity = 2
}

case class Triangle(p0: Point, p1: Point, p2: Point) extends Shape {
    val arity = 3
}

case class Canvas[L <: HList, LL <: HList](shapes: L)
                             (implicit
                              val ev: LUBConstraint[L, Shape],
                              val lf: LeftFolder.Aux[L, Int, pointAccumulate.type, Int],
                              val ls: LeftScanner.Aux[L, Int, pointAccumulate.type, LL],
                              val ev2: LUBConstraint[LL, Int]) {

    lazy val parameterCount: Int = shapes.foldLeft(0)(pointAccumulate)

    lazy val lookup = shapes.scanLeft(0)(pointAccumulate)
}

object pointCount extends Poly1 {
    implicit def default[T <: Shape] = at[T](_.arity)
}

object pointAccumulate extends Poly2 {
    implicit def default1[T <: Shape](implicit pt: pointCount.Case.Aux[T, Int]) =
        at[T, Int] { (p, i) => i + pointCount(p) }

    implicit def default2[T <: Shape](implicit pt: pointCount.Case.Aux[T, Int]) =
        at[Int, T] { (i, p) => i + pointCount(p) }
}

object App2 {

    def main(args: Array[String]): Unit = {

        val l0 = Line((-2, 2), (2, -2))
        val tr1 = Triangle((0,0), (0, 1), (1, 0))
        val tr2 = Triangle((1,1), (1, 2), (2, 1))

        val c = Canvas(l0 :: tr1 :: tr2 :: HNil)
        println(c.parameterCount)
        println(c.lookup)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2019-10-28
    • 2019-02-23
    • 2016-02-17
    • 1970-01-01
    • 2015-03-28
    相关资源
    最近更新 更多