Skip to content

Commit

Permalink
Add fold to ParseResult
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs committed Jun 3, 2016
1 parent 824774e commit 5adbeec
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ sealed trait ParseResult[+A] {
f(get)
}

/**
* Handle both success and failure.
* @param onFailure Function to handle a [[ParseFailure]].
* @param onSuccess Function to process a successful parse.
*/
def fold[B](onFailure: Seq[ParseError] => B)(onSuccess: A => B): B = {
if (isSuccessful) {
onSuccess(get)
} else {
onFailure(errors)
}
}

/**
* Apply a PartialFunction to the result value.
* If the `pf` isn't defined for the result value, a [[ParseFailure]] with the `otherwise` error
Expand Down

0 comments on commit 5adbeec

Please sign in to comment.