Skip to content

Instantly share code, notes, and snippets.

@AndreasKostler
Created July 6, 2015 08:37
Show Gist options
  • Save AndreasKostler/19256ff613799bb5cb03 to your computer and use it in GitHub Desktop.
Save AndreasKostler/19256ff613799bb5cb03 to your computer and use it in GitHub Desktop.
trait LeftFolder[C <: Coproduct, In, F] extends Serializable {
def apply(c: C, in: In): In
}
object LeftFolder {
def apply[C <: Coproduct, In, F](implicit folder: LeftFolder[C, In, F]) = folder
implicit def hdLeftFolder[H, T <: Coproduct, In, F]
(implicit f: Case2.Aux[F, In, H, In]): LeftFolder[H :+: T, In, F] = new LeftFolder[H :+: T, In, F] {
def apply(c: H :+: T, in: In): In = c match {
case Inl(h) => f(in, h)
case Inr(t) => in
}
}
implicit def tlLeftFolder[H, T <: Coproduct, In, F]
(implicit st: LeftFolder[T, In, F]): LeftFolder[H :+: T, In, F] = new LeftFolder[H :+: T, In, F] {
def apply(c: H :+: T, in: In) =
c match {
case Inl(h) => in
case Inr(t) => st(t,in)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment