Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created November 28, 2010 16:30
Show Gist options
  • Save debasishg/719072 to your computer and use it in GitHub Desktop.
Save debasishg/719072 to your computer and use it in GitHub Desktop.
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))
@retronym
Copy link

Why not:

def makeFoo(f1: String, f2: Int, f3: Int) = {
    (validF2(f2).liftFailNel |@| validF3(f3).liftFailNel){(f2,f3) => Foo(f1, f2, f3)}
}

@retronym
Copy link

Oh, there is also:

f1.successNel[String]

@debasishg
Copy link
Author

Thanks .. current status : feeling silly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment