如何从HList创建镜头的HList

时间:2014-08-09 19:56:00

标签: scala shapeless

我正在编写一个通用表查看器,它应该能够显示任何类型的Seq[Row] Row <: HList。 Table类看起来像这样:

class Table[TH<:HList, TR<:HList](val hdrs: TH, val rows: Seq[TR])

当有人点击表格查看器的列时,我想重新绘制按该列的排序顺序排列的整个表格。为此,我需要一个能够分配一个函数来对特定列上的表进行排序。使用镜头似乎是一个合理的选择。

def sort[Elem<:Nat](lens: Lens[R, Elem]) = {
   ...
   table.rows.sortBy(lens.get(_)) //sort the rows of the table using the lens
}

然后我需要将该函数绑定到表头上的click事件。在第一次天真的尝试中,我将使用scalajs-react

构建这样的html标头
def header = {
    tr(for (ci <- 0 to tab.hdrs.runtimeLength) yield 
          th(onclick --> B.sort(hlistNthLens[R,nat(ci)]))(tab.hdrs(ci).toString))
}

即使在表标题上设置onClick也可以调用上面的sort方法。但这不起作用,因为使用名为Int的{​​{1}}丢失了类型结构信息。人们确实需要保留所有类型信息,以便编译器知道HList的特定字段是列表的ci元素,因此镜头的构造可以在类型级别工作。

所以这当然是没有形状编程的难点。

需要的函数是一个将我从n的任何子类带到一个透镜HList的函数,每个透镜都会为HList的特定子类的任何实例选择该元素。

HList

(理想情况下,人们可以将其概括为允许组合这些镜头,以便用户可以选择主要排序,然后选择辅助排序顺序。)

1 个答案:

答案 0 :(得分:1)

好的,我想我找到了答案。第一次测试似乎证实了这一点。

scala> :paste
// Entering paste mode (ctrl-D to finish)

import shapeless._
import shapeless.ops.hlist.At
import shapeless.syntax.std.tuple._

final class myHListOps[L <: HList](l: L) {

  import hlistaux._

  def extractors(implicit extractor : Extractor[_0, L,L]) : extractor.Out = extractor()
}

object hlistaux {
  trait Extractor[HF<:Nat, In <: HList, Remaining<: HList] extends DepFn0 { type Out <: HList }

  object Extractor {
    def apply[HL <: HList]
    (implicit extractor: Extractor[_0, HL,HL]):
       Aux[_0, HL, HL, extractor.Out] = extractor

    type Aux[HF<:Nat, In <: HList, Remaining<: HList, Out0 <: HList] = Extractor[HF, In, Remaining] { type Out = Out0 }

    //To deal with case where HNil is passed. not sure if this is right.
    implicit def hnilExtractor: Aux[_0, HNil, HNil, HNil] =
      new Extractor[_0, HNil, HNil] {
        type Out = HNil
        def apply(): Out = HNil
      }

    implicit def hSingleExtractor1[N<:Nat, In<:HList, H ]
    (implicit att : At[In, N]): Aux[N, In, H::HNil, At[In,N]::HNil] =
      new Extractor[N, In, H::HNil] {
        type Out = At[In,N]::HNil
        def apply(): Out = att::HNil
      }


    implicit def hlistExtractor1[N <: Nat, In<:HList, H, Tail<: HList]
    (implicit mt : Extractor[Succ[N], In, Tail],
              att : At[In, N])
    :Aux[N, In, H::Tail, At[In,N]::mt.Out] = {
      new Extractor[N, In, H::Tail] {
        type Out = At[In,N]::mt.Out

        def apply(): Out = {
          att :: mt()
        }
      }
    }
  }
}

// Exiting paste mode, now interpreting.

import shapeless._
import shapeless.ops.hlist.At
import shapeless.syntax.std.tuple._
defined class myHListOps
defined object hlistaux

scala> val l = "Hello"::HNil
l: shapeless.::[String,shapeless.HNil] = Hello :: HNil

scala> val lo = new myHListOps(l).extractors
lo: shapeless.::[shapeless.ops.hlist.At[shapeless.::[String,shapeless.HNil],shapeless._0],shapeless.HNil] = shapeless.ops.hlist$At$$anon$54@12d33d1c :: HNil

scala> lo.head(l)
res0: lo.head.Out = Hello

scala> val m = 42::l
m: shapeless.::[Int,shapeless.::[String,shapeless.HNil]] = 42 :: Hello :: HNil

scala> val mo = new myHListOps(m).extractors
mo: shapeless.::[shapeless.ops.hlist.At[shapeless.::[Int,shapeless.::[String,shapeless.HNil]],shapeless._0],shapeless.::[shapeless.ops.hlist.At[shapeless.::[Int,shapeless.::[String,shapeless.HNil]],shapeless.Succ[shapeless._0]],shapeless.HNil]] = shapeless.ops.hlist$At$$anon$54@5e181eeb :: shapeless.ops.hlist$At$$anon$55@1960690 :: HNil

scala> mo.head(m)
res3: mo.head.Out = 42

scala> mo.tail.head(m)
res4: mo.tail.head.Out = Hello