这是一个新问题。
我想做这样的事情:
val a = 1 :: "hi" :: HNil
val b = "foo" :: 2.2 :: HNil
val c = 3 :: 4 :: HNil
val d = (a, b, c).zip // Like "zipped" on tuples of regular lists.
在上文中,d
应具有以下值:
(1, "foo", 3) :: ("hi", 2.2, 4) :: HNil
有干净的方法吗?
答案 0 :(得分:3)
您需要先将元组转换为HList
。在1.2.x中:
import shapeless._, Tuples._
val a = 1 :: "hi" :: HNil
val b = "foo" :: 2.2 :: HNil
val c = 3 :: 4 :: HNil
(a, b, c).hlisted.zipped
在2.0.0中你有更多选择:
import shapeless._, syntax.std.tuple._
val a = 1 :: "hi" :: HNil
val b = "foo" :: 2.2 :: HNil
val c = 3 :: 4 :: HNil
(a, b, c).productElements.zip
或者:
import shapeless._, syntax.std.tuple._
val a = (1, "hi")
val b = ("foo", 2.2)
val c = (3, 4)
(a, b, c).zip
这最后一个将返回一个3元组的元组,这可能适用于你也可能不适用。