Skip to content

Instantly share code, notes, and snippets.

@bvenners
Last active August 29, 2015 14:06
Show Gist options
  • Save bvenners/9596bfc5712c8061762e to your computer and use it in GitHub Desktop.
Save bvenners/9596bfc5712c8061762e to your computer and use it in GitHub Desktop.
Using Scalactic Or and Validation
scala> import org.scalactic._
import org.scalactic._
scala> def reasons(x: Int): Int Or ErrorMessage =
| for {
| y <- Good(x) filter { i => if (i > 0) Pass else Fail(s"$i was <= 0") }
| z <- Good(y / 2) filter { i => if (i > 1) Pass else Fail(s"$y / 2 was <= 1") }
| } yield z
reasons: (x: Int)org.scalactic.Or[Int,org.scalactic.ErrorMessage]
scala> (0 to 4) map reasons
res0: scala.collection.immutable.IndexedSeq[org.scalactic.Or[Int,org.scalactic.ErrorMessage]] =
Vector(Bad(0 was <= 0), Bad(1 / 2 was <= 1), Bad(2 / 2 was <= 1), Bad(3 / 2 was <= 1), Good(2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment