scala – 拆分使用Prepend [A,B]连接的HList
发布时间:2020-12-16 08:55:44 所属栏目:安全 来源:网络整理
导读:我基本上寻找类型类Prepend [A,B]的反面. 如果我有类似的东西: type A = String :: Int :: HNiltype B = Boolean :: Double :: HNilval a: A = "a" :: 1 :: HNilval b: B = false :: 2.1 :: HNilscala val ab = a ++ bab: shapeless.::[String,shapeless.::
我基本上寻找类型类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 我有一个A类型的HList和一个B类型的HList,我可以找到一个前置:Prepend [A,B]这样我可以用b连接它们. 但是如果我有一个类型为prepend的HList,那么,我怎样才能提取原始的A和B?我似乎无法找到一个完成这项工作的类型类,也许没有一个.看起来我需要像Trait Cut [A<:HList,B<:HList,c<:HList]这样的东西,证明C是由预先挂起的A到B创建的,尽管我不确定我将如何生成证人. 非常像: def Cut[A <: HList,B <: HList,C <: HList](c: C)(implicit cut: Cut[A,B,C]): (A,B) = ??? 解决方法
您可以使用Split相当直接地执行此操作:
import shapeless._,ops.hlist.{ Length,Prepend,Split } class UndoPrependHelper[A <: 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,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,3) :: Some(0.0) :: HNil scala> val c: C = a ++ b c: C = 1 :: foo :: 'bar :: List(1,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,3) :: Some(0.0) :: HNil 我recently added是一个删除类型类的“撤销”操作,并且在Prepend中包含类似内容可能是有意义的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |