Skip to content

Instantly share code, notes, and snippets.

@mpilquist
Created September 18, 2014 15:47
Show Gist options
  • Save mpilquist/a15e2e387c88018f137e to your computer and use it in GitHub Desktop.
Save mpilquist/a15e2e387c88018f137e to your computer and use it in GitHub Desktop.
Reasons for none
def noReasons(x: Int): Option[Int] = for {
y <- Option(x).filter(_ > 0)
z <- Option(y / 2).filter(_ > 1)
} yield z
(0 to 4) map noReasons
// scala.collection.immutable.IndexedSeq[Option[Int]] = Vector(None, None, None, None, Some(2))
import scalaz.\/
import scalaz.syntax.std.option._
def reasons(x: Int): String \/ Int = for {
y <- Option(x).filter(_ > 0).toRightDisjunction("x was <= 0")
z <- Option(y / 2).filter(_ > 1).toRightDisjunction("y / 2 was 1")
} yield z
(0 to 4) map reasons
// scala.collection.immutable.IndexedSeq[scalaz.\/[String,Int]] = Vector(-\/(x was <= 0), -\/(y / 2 was 1), -\/(y / 2 was 1), -\/(y / 2 was 1), \/-(2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment