我是新手,我是这样做的
val list1 = List(1, 2, 3)
val list2 = List("a", "b", "c")
val lists = List(list1, list2)
val hlist = lists.toHList[List[Int] :: List[String] :: HNil]
这很好,但是当我不知道List 列表的大小和类型时,我想创建我的HList。可能吗?如果没有,是否有任何解决方法?
提前致谢
更新
为了给你一些背景,这是我的用例
case class Data[T: ClassTag](list: List[T])
trait Transformation {
type T
type U
def transform(data: Data[T]) : Data[U]
}
class FromInt2String extends Transformation {
override type T = Int
override type U = String
override def transform(data: Data[T]) = new Data(List("1s", "2s", "3s"))
}
class FromString2Int extends Transformation {
override type T = String
override type U = Int
override def transform(data: Data[T]) = new Data(List(4, 5, 6))
}
object andThen extends Poly2 {
implicit def functions[A, B, C] = at[A => B, B => C](_ andThen _)
}
这些是我的对象,这是我的代码
val data = new Data(List(1, 2, 3))
val int2String = new FromInt2String()
val string2Int = new FromString2Int()
val transformationsHList = int2String.transform _ :: string2Int.transform _ :: HNil
val functions = transformationsHList.reduceLeft(andThen)
val data2 = functions(data)
我试图做这样的事情
val transformationsList = List[Transformation](int2String, string2Int)
val transformations = allTransformations.toHList