Created
September 18, 2014 15:47
-
-
Save mpilquist/a15e2e387c88018f137e to your computer and use it in GitHub Desktop.
Reasons for none
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
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