From what I can tell the main things to tools to use are map, fold and lift.
When you want to get something out of a validation regardless of
whether it is a success or a failure
(analogous to option's 'getOrElse') use fold:
scala> val s = 1.success[String]
s: scalaz.Validation[String,Int] = Success(1)
scala> val f = "fail".fail[Int]
f: scalaz.Validation[java.lang.String,Int] = Failure(fail)
scala> f.fold( x => "", _.toString)
res38: java.lang.String =
scala> s.fold( x => "", _.toString)
res39: java.lang.String = 1
Or you could just convert to an option:
scala> s.toOption.getOrElse("")
res34: Any = 1
If you need to modify the result validation if it's a success:
scala> s.map( _ + 1)
res40: scalaz.Validation[String,Int] = Success(2)
Similarly for a failure:
scala> s.fail.map( _ + ":").validation
res41: scalaz.Validation[java.lang.String,Int] = Success(1)
scala> f.fail.map( _ + ":").validation
res42: scalaz.Validation[java.lang.String,Int] = Failure(fail:)
Lastly use lift for wrapping either the success or failure in something else:
scala> s.lift[Option, Int]
res43: scalaz.Validation[String,Option[Int]] = Success(Some(1))
scala> f.fail.lift[Option, String]
res45: scalaz.Validation[Option[String],Int] = Failure(Some(fail))
Using these methods rather than pattern matching does seem to make for
cleaner code.
Hope that helps,
Rob
> --
> You received this message because you are subscribed to the Google Groups
> "scalaz" group.
> To post to this group, send email to sca...@googlegroups.com.
> To unsubscribe from this group, send email to
> scalaz+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/scalaz?hl=en.
>
--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To post to this group, send email to sca...@googlegroups.com.
To unsubscribe from this group, send email to scalaz+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/scalaz?hl=en.