Skip to content

Instantly share code, notes, and snippets.

@drstevens
Forked from OleTraveler/gist:4347051
Last active December 10, 2015 13:08
Show Gist options
  • Save drstevens/4438371 to your computer and use it in GitHub Desktop.
Save drstevens/4438371 to your computer and use it in GitHub Desktop.
This demonstrates some of the ways to combine scalaz6 Validaitons. It is the result of answers to question http://stackoverflow.com/questions/13961940/chaining-scalaz-validation-functions-function1a-validatione-b. See answers for more, including combining functions using Kliesi in scalaz6 and scalaz7.
def allDigits: (String) => ValidationNEL[String, String]
def maxSizeOfTen: (String) => ValidationNEL[String, String]
def toInt: (String) => ValidationNEL[String, Int]
val validInt: String => ValidationNEL[String, Int] = s =>
for {
validStr <- (allDigits(s) |@| maxSizeOfTen(s))((_,x) => x)
i <- toInt(validStr)
} yield(i)
val validInt: String => ValidationNEL[String, Int] = s =>
for {
_ <- (allDigits(s) <|*|> maxSizeOfTen(s))
i <- toInt(s)
} yield(i)
//or
val validInt: String => ValidationNEL[String, Int] = s =>
for {
validStr <- (allDigits(s) *> maxSizeOfTen(s))
i <- toInt(validStr)
} yield(i)
//or
val validInt: String => ValidationNEL[String, Int] = s =>
for {
validStr <- (allDigits(s) <* maxSizeOfTen(s))
i <- toInt(validStr)
} yield(i)
//or
val validInt: String => ValidationNEL[String, Int] = s =>
(allDigits(s) *> maxSizeOfTen(s)) flatMap toInt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment