Given the following
case class A(value:Int)
case class B(value:String)
val h:Option[A] :: A :: Option[B] :: Option[A] :: HNil = Some(A(1)) :: A(2) :: Some(B("two")) :: (None:Option[B]) :: HNil
How can I get the following ?
A(1) :: A(2) :: B("two") :: HNil
My attempt below
trait a extends Poly1 {
implicit def any[T] = at[T](_ :: HNil)
}
object f extends a {
implicit def some[T] = at[Option[T]](t => if (t.isDefined) t.get :: HNil else HNil)
}
works for map
h map f
> A(1) :: HNil :: A(2) :: HNil :: B(two) :: HNil :: HNil :: HNil
but fail for flatMap
h flatMap f
> could not find implicit value for parameter mapper: shapeless.ops.hlist.FlatMapper[f.type,shapeless.::[Option[A],shapeless.::[A,shapeless.::[Option[B],shapeless.::[Option[B],shapeless.HNil]]]]]
f
? What does the argument toflatMap
look like? Also as definedh
is going to be statically typed as havingSome[A]
, etc. elements, which is pretty much never useful. – Travis Brown