Skip to content

Commit

Permalink
Added findValid method on Validated that accumulates errors. Closes t…
Browse files Browse the repository at this point in the history
  • Loading branch information
mgzuber committed Nov 14, 2016
1 parent bcc751f commit e66f7a1
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions core/src/main/scala/cats/data/Validated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,27 @@ sealed abstract class Validated[+E, +A] extends Product with Serializable {

/**
* Return this if it is Valid, or else fall back to the given default.
* The functionality is similar to that of [[findValid]] except for failure accumulation,
* where here only the error on the right is preserved and the error on the left is ignored.
*/
def orElse[EE, AA >: A](default: => Validated[EE, AA]): Validated[EE, AA] =
this match {
case v @ Valid(_) => v
case Invalid(_) => default
}

/**
* If `this` is valid return `this`, otherwise if `that` is valid return `that`, otherwise combine the failures.
* This is similar to [[orElse]] except that here failures are accumulated.
*/
def findValid[EE >: E, AA >: A](that: => Validated[EE, AA])(implicit EE: Semigroup[EE]): Validated[EE, AA] = this match {
case v @ Valid(_) => v
case Invalid(e) => that match {
case v @ Valid(_) => v
case Invalid(ee) => Invalid(EE.combine(e, ee))
}
}

/**
* Converts the value to an Either[E, A]
*/
Expand Down

0 comments on commit e66f7a1

Please sign in to comment.