Created
November 9, 2012 05:39
-
-
Save dipakc/4043930 to your computer and use it in GitHub Desktop.
Refactoring: try catch inside for comprehension
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
| import scala.util.control.Exception._ | |
| object RefactoringTryCatchFor { | |
| ///////////////////////////////////////// | |
| for { | |
| x <- List(10, 20, 30) | |
| y <- List(0, 1, 2) | |
| val zOpt = try { Some(x / y) } catch { case _ => None } | |
| if (zOpt.isDefined) | |
| } yield { | |
| zOpt.get | |
| } | |
| ////////////////////////////////////////////// | |
| for { | |
| x <- List(10, 20, 30) | |
| y <- List(0, 1, 2) | |
| z <- allCatch opt x / y | |
| } yield { | |
| z | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment