对 HList 进行排序 [英] Sequencing an HList

查看:27
本文介绍了对 HList 进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 Shapeless HList,其中每个列表元素共享相同的类型构造函数,如何对 HList 进行排序?

Given a Shapeless HList where every list element shares the same type constructor, how can the HList be sequenced?

例如:

def some[A](a: A): Option[A] = Some(a)
def none[A]: Option[A] = None

val x = some(1) :: some("test") :: some(true) :: HNil
val y = sequence(x) // should be some(1 :: "test" :: true :: HNil)

def sequence[L <: HList : *->*[Option]#λ, M <: HList](l: L): Option[M] =
  ???

我试图实现这样的序列:

I tried to implement sequence like this:

object optionFolder extends Poly2 {
  implicit def caseOptionValueHList[A, B <: HList] = at[Option[A], Option[B]] { (a, b) =>
    for { aa <- a; bb <- b } yield aa :: bb
  }
}

def sequence[L <: HList : *->*[Option]#λ, M <: HList](l: L): Option[M] = {
  l.foldRight(some(HNil))(optionFolder)
}

但这不能编译:

could not find implicit value for parameter folder: shapeless.RightFolder[L,Option[shapeless.HNil.type],SequencingHList.optionFolder.type]

关于为 Option 等特定示例或任意 Applicative 实现此功能的任何提示?

Any tips on implementing this for either a specific example like Option or for an arbitrary Applicative?

推荐答案

你已经很接近了——你只需要确保你有它要求的额外证据:

You were pretty close—you just need to make sure you have the extra bit of evidence that it's asking for:

def sequence[L <: HList : *->*[Option]#λ, M <: HList](l: L)(implicit
  folder: RightFolder[L, Option[HNil], optionFolder.type]
) = l.foldRight(some(HNil: HNil))(optionFolder)

或者,如果您想要更一般的东西,并且有这样的应用程序:

Or, if you want something more general, and have an applicative implementation like this:

trait Applicative[F[_]] {
  def ap[A, B](fa: => F[A])(f: => F[A => B]): F[B]
  def point[A](a: => A): F[A]
  def map[A, B](fa: F[A])(f: A => B): F[B] = ap(fa)(point(f))
}

implicit object optionApplicative extends Applicative[Option] {
  def ap[A, B](fa: => Option[A])(f: => Option[A => B]) = f.flatMap(fa.map)
  def point[A](a: => A) = Option(a)
}

你可以写:

object applicativeFolder extends Poly2 {
  implicit def caseApplicative[A, B <: HList, F[_]](implicit
    app: Applicative[F]
  ) = at[F[A], F[B]] {
    (a, b) => app.ap(a)(app.map(b)(bb => (_: A) :: bb))
  }
}

def sequence[F[_]: Applicative, L <: HList: *->*[F]#λ, M <: HList](l: L)(implicit
  folder: RightFolder[L, F[HNil], applicativeFolder.type]
) = l.foldRight(implicitly[Applicative[F]].point(HNil: HNil))(applicativeFolder)

现在您还可以对列表等进行排序(假设您有合适的实例).

And now you can sequence lists, etc., as well (assuming you have the appropriate instances).

更新:请注意,我在这两种情况下都省略了 sequence 的返回类型注释.如果我们把它放回去,编译器就会卡住:

Update: Note that I've omitted the return type annotation for sequence in both cases here. If we put it back in, the compiler chokes:

<console>:18: error: type mismatch;
 found   : folder.Out
 required: F[M]

这是因为 RightFolder 实例将其返回类型作为抽象类型成员携带.在这种情况下,我们知道它是 F[M],但编译器并不关心我们知道什么.

This is because the RightFolder instance carries around its return type as an abstract type member. We know it's F[M] in this case, but the compiler doesn't care about what we know.

如果我们希望能够明确返回类型,我们可以改用 RightFolderAux 实例:

If we want to be able to be explicit about the return type, we can use the RightFolderAux instance instead:

def sequence[F[_]: Applicative, L <: HList: *->*[F]#λ, M <: HList](l: L)(implicit
  folder: RightFolderAux[L, F[HNil], applicativeFolder.type, F[M]]
): F[M] =
  l.foldRight(implicitly[Applicative[F]].point(HNil: HNil))(applicativeFolder)

请注意,RightFolderAux 有一个额外的类型参数,用于指示返回类型.

Note that RightFolderAux has an extra type parameter, which indicates the return type.

这篇关于对 HList 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆