Created
November 28, 2010 16:30
-
-
Save debasishg/719072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> case class Foo(f1: String, f2: Int, f3: Int) | |
defined class Foo | |
scala> def validF2(f: Int): Validation[String, Int] = { | |
| if (f < 0) "f2 must be >= 0".fail | |
| else f.success | |
| } | |
validF2: (f: Int)scalaz.Validation[String,Int] | |
scala> def validF3(f: Int): Validation[String, Int] = { | |
| if (f > 100) "f3 must be <= 100".fail | |
| else f.success | |
| } | |
validF3: (f: Int)scalaz.Validation[String,Int] | |
// Can I have an alternative for f1.success.liftFailNel | |
// will be too boilerplaty if I have multiple fields that don;t require validation | |
scala> def makeFoo(f1: String, f2: Int, f3: Int) = { | |
| (f1.success.liftFailNel |@| validF2(f2).liftFailNel |@| validF3(f3).liftFailNel){(f1,f2,f3) => Foo(f1, f2, f3)} | |
| } | |
makeFoo: (f1: String,f2: Int,f3: Int)scalaz.Validation[scalaz.NonEmptyList[String],Foo] | |
scala> makeFoo("dg", -2, 200) | |
res19: scalaz.Validation[scalaz.NonEmptyList[String],Foo] = Failure(NonEmptyList(f2 must be >= 0, f3 must be <= 100)) | |
scala> makeFoo("dg", 2, 100) | |
res20: scalaz.Validation[scalaz.NonEmptyList[String],Foo] = Success(Foo(dg,2,100)) |
Oh, there is also:
f1.successNel[String]
Thanks .. current status : feeling silly :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not: