Skip to content

Instantly share code, notes, and snippets.

@dipakc
Created November 9, 2012 05:39
Show Gist options
  • Select an option

  • Save dipakc/4043930 to your computer and use it in GitHub Desktop.

Select an option

Save dipakc/4043930 to your computer and use it in GitHub Desktop.
Refactoring: try catch inside for comprehension
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