你不能在这里使用列表推导式for
或map
和flatMap
的组合(正如其他答案所建议的那样),因为HList
上的这些方法需要高阶函数。如果你只有两个静态类型的列表,这很容易:
import shapeless._
val xs = 1 :: 'b :: 'c' :: HNil
val ys = 4.0 :: "e" :: HNil
object eachFirst extends Poly1 {
implicit def default[A] = at[A] { a =>
object second extends Poly1 { implicit def default[B] = at[B](a -> _) }
ys map second
}
}
val cartesianProductXsYs = xs flatMap eachFirst
以下是(适当类型化的)结果:
这给我们带来了以下结果:
(1,4.0) :: (1,e) :: ('b,4.0) :: ('b,e) :: (c,4.0) :: (c,e) :: HNil
编写一个使用HList
参数执行此操作的方法比较棘手。这里有一个快速示例,展示如何使用一些稍微通用的机制实现它。
首先需要指出的是,我们可以将两个普通列表的笛卡尔积视为将一个接受两个参数并将它们作为元组返回的函数“提升”到列表的可应用函子中。例如,您可以在Haskell中编写以下代码:
import Control.Applicative (liftA2)
cartesianProd :: [a] -> [b] -> [(a, b)]
cartesianProd = liftA2 (,)
我们可以在这里编写一个与(,)
对应的多态二元函数:
import shapeless._
object tuple extends Poly2 {
implicit def whatever[A, B] = at[A, B] { case (a, b) => (a, b) }
}
为了完整起见,我们再次定义示例列表:
val xs = 1 :: 'b :: 'c' :: HNil
val ys = 4.0 :: "e" :: HNil
现在,我们将着手编写一个名为liftA2
的方法,使我们能够编写如下代码:
liftA2(tuple)(xs, ys)
获取正确结果。名称liftA2
有点误导性,因为我们实际上没有一个适用的函子实例,并且它不是通用的。我正在使用HList
上命名为flatMap
和map
的方法模型,并且接受更好建议。
现在我们需要一个类型类,允许我们获取Poly2
,对其进行部分应用,并将生成的一元函数映射到HList
上:
trait ApplyMapper[HF, A, X <: HList, Out <: HList] {
def apply(a: A, x: X): Out
}
object ApplyMapper {
implicit def hnil[HF, A] = new ApplyMapper[HF, A, HNil, HNil] {
def apply(a: A, x: HNil) = HNil
}
implicit def hlist[HF, A, XH, XT <: HList, OutH, OutT <: HList](implicit
pb: Poly.Pullback2Aux[HF, A, XH, OutH],
am: ApplyMapper[HF, A, XT, OutT]
) = new ApplyMapper[HF, A, XH :: XT, OutH :: OutT] {
def apply(a: A, x: XH :: XT) = pb(a, x.head) :: am(a, x.tail)
}
}
现在有一个类型类可用于帮助提升:
trait LiftA2[HF, X <: HList, Y <: HList, Out <: HList] {
def apply(x: X, y: Y): Out
}
object LiftA2 {
implicit def hnil[HF, Y <: HList] = new LiftA2[HF, HNil, Y, HNil] {
def apply(x: HNil, y: Y) = HNil
}
implicit def hlist[
HF, XH, XT <: HList, Y <: HList,
Out1 <: HList, Out2 <: HList, Out <: HList
](implicit
am: ApplyMapper[HF, XH, Y, Out1],
lift: LiftA2[HF, XT, Y, Out2],
prepend : PrependAux[Out1, Out2, Out]
) = new LiftA2[HF, XH :: XT, Y, Out] {
def apply(x: XH :: XT, y: Y) = prepend(am(x.head, y), lift(x.tail, y))
}
}
最后是我们的方法本身:
def liftA2[HF, X <: HList, Y <: HList, Out <: HList](hf: HF)(x: X, y: Y)(implicit
lift: LiftA2[HF, X, Y, Out]
) = lift(x, y)
现在,liftA2(tuple)(xs, ys)
就是这样了。
scala> type Result =
| (Int, Double) :: (Int, String) ::
| (Symbol, Double) :: (Symbol, String) ::
| (Char, Double) :: (Char, String) :: HNil
defined type alias Result
scala> val res: Result = liftA2(tuple)(xs, ys)
res: Result = (1,4.0) :: (1,e) :: ('b,4.0) :: ('b,e) :: (c,4.0) :: (c,e) :: HNil
就像我们想要的那样。