Skip to content

Instantly share code, notes, and snippets.

@hisui
Created February 16, 2017 04:40
Show Gist options
  • Save hisui/d32bea2afd320ce576f930a1ca3c7bfa to your computer and use it in GitHub Desktop.
Save hisui/d32bea2afd320ce576f930a1ca3c7bfa to your computer and use it in GitHub Desktop.
import shapeless.CNil
import shapeless.Coproduct
import shapeless.PolyDefns.Case1
import shapeless.ops.coproduct.Basis
import shapeless.ops.coproduct.Remove
trait Unfold[F, A, C <: Coproduct] {
type Out = Option[C]
def apply(a: A): Out
}
object Unfold {
type Aux[F, A, C <: Coproduct, Z] = Unfold[F, A, C] {
type Out = Z
}
implicit def case_1[F, A, H, C1 <: Coproduct, C2 <: Coproduct]
(implicit rm: Remove.Aux[C1, H, C2], gt: C1 Basis C2, poly: Case1.Aux[F, A, Option[H]], next: Unfold[F, A, C2]) = new Unfold[F, A, C1] {
override def apply(a: A): Out =
poly(a).map((rm.inverse _).compose( Left(_))) orElse
next(a).map((gt.inverse _).compose(Right(_)))
}
implicit def case_0[F, A] = new Unfold[F, A, CNil] {
override def apply(a: A): Out = None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment