Last active
August 29, 2015 14:16
-
-
Save gszeliga/578900ad764824aabdc4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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