scalaz/scalaz — pull request #360 by oxbowlakes (2013-05-07 15:19:14+00:00)
Title: Added ability to "filter" validations based on the result of a partial function

Ability to "filter" a `Validation[E, A]` based on a `PartialFunction[A, E]` (`pf`) such that if this validation is a `Success(a)` and if `pf` is defined at `a`, then return a `Failure(pf(a))`; otherwise return `this`

This alllows for user code like:
```scala
strings map (_.parseInt excepting {
case i if i < 0 => new Exception(s"Int must be positive: $i") }
)
```
The functionality is similar to `ensure` but has two advantages:

1. The returned failure can depend on the current (successful) value [*]
2. The user code reveals its intent more obviously [*]

In fact, `ensure` can be implemented as:
```scala
def ensure[EE >: E](onFailure: => EE)(f: A => Boolean): Validation[EE, A] =
excepting({ case a if !f(a) => onFailure})
```

[*] - compare the above with:
```scala
string map (_.parseInt.ensure(new Exception("something failed"))(_ >= 0))
```
The commit also includes a test