本文介绍了拆分使用 Prepend[A, B] 连接的 HList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本质上是在寻找类型类 Prepend[A, B] 的反面.

I'm essentially looking for the opposite of the type class Prepend[A, B].

如果我有类似的东西:

type A = String :: Int :: HNil
type B = Boolean :: Double :: HNil

val a: A = "a" :: 1 :: HNil
val b: B = false :: 2.1 :: HNil

scala> val ab = a ++ b
ab: shapeless.::[String,shapeless.::[Int,shapeless.::[Boolean,shapeless.::[Double,shapeless.HNil]]]] = a :: 1 :: false :: 2.1 :: HNil

我有一个 HList a 类型 A 和一个 HList bB 类型,我可以找到一个 prepend: Prepend[A, B] 这样我就可以将它们与 a ++ b 连接起来.

I have an HList a of type A and an HList b of type B, I can find a prepend: Prepend[A, B] such that I can concatenate them with a ++ b.

但是如果我有一个 HList ab 类型的 prepend.Out,我如何提取原始的 A> 和 B?我似乎找不到可以完成这项工作的类型类,也许没有.似乎我需要像 trait Cut[A <: HList, B <: HList, c <: HList] 这样的东西来证明 C 已经被创建通过将 A 前置到 B,虽然我不确定我将如何生成证人.

But if I have an HList ab of type prepend.Out, how can I extract the original A and B? I can't seem to find a type class that does the job, and perhaps there isn't one. It seems like I would need something like trait Cut[A <: HList, B <: HList, c <: HList] that witnesses that C has been created by pre-pending A to B, though I'm not sure how I would go about generating witnesses.

非常像:

def Cut[A <: HList, B <: HList, C <: HList](c: C)(implicit cut: Cut[A, B, C]): (A, B)  = ???

推荐答案

您可以使用 Split 非常简单地做到这一点:

You can do this fairly straightforwardly with Split:

import shapeless._, ops.hlist.{ Length, Prepend, Split }

class UndoPrependHelper[A <: HList, B <: HList, C <: HList, N <: Nat] {
  def apply(c: C)(implicit split: Split.Aux[C, N, A, B]): (A, B) = split(c)
}

def undoPrepend[A <: HList, B <: HList](implicit
  prepend: Prepend[A, B],
  length: Length[A]
) = new UndoPrependHelper[A, B, prepend.Out, length.Out]

然后:

scala> type A = Int :: String :: Symbol :: HNil
defined type alias A

scala> type B = List[Int] :: Option[Double] :: HNil
defined type alias B

scala> type C = Int :: String :: Symbol :: List[Int] :: Option[Double] :: HNil
defined type alias C

scala> val a: A = 1 :: "foo" :: 'bar :: HNil
a: A = 1 :: foo :: 'bar :: HNil

scala> val b: B = List(1, 2, 3) :: Option(0.0) :: HNil
b: B = List(1, 2, 3) :: Some(0.0) :: HNil

scala> val c: C = a ++ b
c: C = 1 :: foo :: 'bar :: List(1, 2, 3) :: Some(0.0) :: HNil

scala> val (newA: A, newB: B) = undoPrepend[A, B].apply(c)
newA: A = 1 :: foo :: 'bar :: HNil
newB: B = List(1, 2, 3) :: Some(0.0) :: HNil

最近为Remove添加了一个撤消"操作code> 类型的类,并且在 Prepend 中内置类似的东西可能是有意义的.

I recently added an "undo" operation for the Remove type class, and it might make sense to have something similar built into Prepend.

这篇关于拆分使用 Prepend[A, B] 连接的 HList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:51