Skip to content

Instantly share code, notes, and snippets.

@gszeliga
Last active August 29, 2015 14:16
Show Gist options
  • Save gszeliga/578900ad764824aabdc4 to your computer and use it in GitHub Desktop.
Save gszeliga/578900ad764824aabdc4 to your computer and use it in GitHub Desktop.
sealed trait BuildStep[+E, +A]{
def toEither: Either[E,A]
}
final case class Continue[A](v: A) extends BuildStep[Nothing,A] {
def toEither = Right(v)
}
final case class Failure[E](e: List[E]) extends BuildStep[List[E], Nothing] {
def toEither = Left(e)
}
def applicativeBuilder[E] = new Applicative[({type f[x] = BuildStep[List[E], x]})#f] {
def unit[A](a: A) = Continue(a)
override def map2[A, B, C](fa: BuildStep[List[E], A], fb: BuildStep[List[E], B])(f: (A, B) => C) = {
(fa, fb) match{
case (Continue(a), Continue(b)) => Continue(f(a,b))
case (Failure(l1), Failure(l2)) => Failure(l1 ++ l2)
case (f @ Failure(_), _) => f
case (_, f @ Failure(_)) => f
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment