Skip to content

Commit

Permalink
Add valueOr to Validated, analog to Xor.valueOr
Browse files Browse the repository at this point in the history
  • Loading branch information
mkotsbakws committed Jun 14, 2016
1 parent b149835 commit 5765281
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/data/Validated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ sealed abstract class Validated[+E, +A] extends Product with Serializable {
*/
def getOrElse[B >: A](default: => B): B = fold(_ => default, identity)

/**
* Return the Valid value, or the result of f if Invalid
*/
def valueOr[B >: A](f: E => B): B = fold(f, identity)

/**
* Is this Valid and matching the given predicate
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/ValidatedTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ class ValidatedTests extends CatsSuite {
}
}

test("valueOr consistent with swap then map then merge") {
forAll { (v: Validated[String, Int], f: Int => String) =>
v.valueOr(f) should === (v.swap.map(f).merge)
}
}

test("toEither then fromEither is identity") {
forAll { (v: Validated[String, Int]) =>
Validated.fromEither(v.toEither) should === (v)
Expand Down

0 comments on commit 5765281

Please sign in to comment.