Created
July 6, 2015 08:37
-
-
Save AndreasKostler/19256ff613799bb5cb03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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