Skip to content

Instantly share code, notes, and snippets.

@stasimus
Created June 10, 2012 21:00
Show Gist options
  • Save stasimus/2907294 to your computer and use it in GitHub Desktop.
Save stasimus/2907294 to your computer and use it in GitHub Desktop.
trait Semigroup[A] {
def append(a1: A, a2: A): A
}
sealed abstract class Validation[F: Semigroup] {
final def and(other: Validation[F]) = (this, other) match {
case (Failure(e1), Failure(e2)) => Failure(implicitly[Semigroup[F]].append(e1, e2))
case (Failure(e1), Success()) => Failure(e1)
case (Success(), Failure(e2)) => Failure(e2)
case (Success(), Success()) => Success
}
}
final case class Failure[F: Semigroup](e: F) extends Validation[F]
final case class Success[F: Semigroup]() extends Validation[F]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment