Created
April 28, 2015 01:41
-
-
Save kkismd/bf9f05e33cbe833bef46 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 fail(i: Int): Either[String,Int] = Left(s"error occored from $i") | |
// 成功する処理 | |
def success(i: Int): Either[String,Int] = Right(i + 1) | |
println("すべて成功する (e is 5)") | |
val result0 = for { | |
a <- success(0).right | |
b <- success(a).right | |
c <- success(b).right | |
d <- success(c).right | |
e <- success(d).right | |
} yield e | |
println(result0) | |
println("成功して最後に失敗する (d is 4)") | |
val result1 = for { | |
a <- success(0).right | |
b <- success(a).right | |
c <- success(b).right | |
d <- success(c).right | |
e <- fail(d).right | |
} yield e | |
println(result1) | |
println("途中で失敗する (b is 2)") | |
val result2 = for { | |
a <- success(0).right | |
b <- success(a).right | |
c <- fail(b).right | |
d <- success(c).right | |
e <- success(d).right | |
} yield e | |
println(result2) | |
println("最初で失敗する (a is 0)") | |
val result3 = for { | |
a <- fail(0).right | |
b <- success(a).right | |
c <- success(b).right | |
d <- success(c).right | |
e <- success(d).right | |
} yield e | |
println(result3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment