Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scalaz validation

I'm trying to use scalaz validation in our project and have ran into a following situation:

def rate(username: String, params: Map[String, String]): ValidationNEL[String, Int] = {
  val voteV:Validation[String, RateVote] = validate(params, "vote") flatMap {v: String => RateVote(v)}
  val voterV:Validation[String, Sring] = validate(params, "voter")

  ... 
}

Now I have to return ValidationNEL containing possible parameter errors, if there were any, or use validated parameters to call the method:

storage.rate(username, voter, vote): Validation[String, Int]

I know, I could use |@| for the first part, but this code

(voterV.liftFailNel |@| voteV.liftFailNel) { (voter, rv) =>
  storage.rate(username, voter, rv)
}

will return ValidationNEL[String, Validation[String, Int]]. Is there a way to "flatten" this result, to get the ValidationNEL[String, Int]?

like image 208
Digal Avatar asked Jan 13 '11 10:01

Digal


1 Answers

You can flatten the result with a fold.

result.fold(e => e.fail, x => x.liftFailNel)
like image 121
retronym Avatar answered Oct 09 '22 02:10

retronym