Given a Shapeless HList where every list element shares the same type constructor, how can the HList be sequenced?
For example:
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)
}
But that does not compile:
could not find implicit value for parameter folder: shapeless.RightFolder[L,Option[shapeless.HNil.type],SequencingHList.optionFolder.type]
Any tips on implementing this for either a specific example like Option or for an arbitrary Applicative?