This works on scalaz 7.0.6, but not on the latest release of scalaz, 7.2.6.
import scalaz._, Scalaz._
def allDigits(s: String): Validation[String, String] =
if (s.forall(_.isDigit)) s.success else "Not all digits".failure
def maxSizeOfTen(s: String): Validation[String, String] =
if (s.length <= 10) s.success else "Too big".failure
def toInt(s: String) = try s.toInt.success catch {
case _: NumberFormatException => "Still not an integer".failure
}
val validated1 = for {
x <- allDigits("4234")
y <- maxSizeOfTen(x)
z <- toInt(y)
} yield z
I get these errors on scalaz 7.2.6:
value flatMap is not a member of scalaz.Validation[String,String]
x <- allDigits("4234")
value flatMap is not a member of scalaz.Validation[String,String]
y <- maxSizeOfTen(x)
...
How do I make it work on the latest version of scalaz?
Update: Solution based on the accepted answer:
import scalaz._, Scalaz._
def allDigits(s: String): \/[String, String] =
if (s.forall(_.isDigit)) s.right else "Not all digits".left
def maxSizeOfTen(s: String): \/[String, String] =
if (s.length <= 10) s.right else "Too big".left
def toInt(s: String) = try s.toInt.right catch {
case _: NumberFormatException => "Still not an integer".left
}
val validated1 = for {
x <- allDigits("4234")
y <- maxSizeOfTen(x)
z <- toInt(y)
} yield z
As previous answer pointed Validation is useful when you want to run things in parallel and get all the errors at once.
Applicative approach:
def allDigits(s: String): Validation[String, String] =
if (s.forall(_.isDigit)) s.success else s"|error: '$s' Not all digits ".failure
def maxSizeOfTen(s: String): Validation[String, String] =
if (s.length <= 10) s.success else s"|error: '$s' Too big".failure
def toInt(s: String) = try s.toInt.success catch {
case _: NumberFormatException => "|Still not an integer".failure
}
val input = "4234"
val validated1 = (allDigits(input) |@| maxSizeOfTen(input)) { (x, _) => toInt(x) }
println(validated1)
val input2 = "123456789ten"
val validated2 = (allDigits(input2) |@| maxSizeOfTen(input2)) { (x, _) => toInt(x) }
println(validated2)
You will have next output:
Success(Success(4234))
Failure(|error: '123456789ten' Not all digits |error: '123456789ten' Too big)