Skip to content

Instantly share code, notes, and snippets.

@dipakc
dipakc / RefactoringTryCatchFor.scala
Created November 9, 2012 05:39
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 {
@dipakc
dipakc / Refactoring_Nested_for_and_flatten.scala
Created November 9, 2012 05:20
Refactoring. Nested `for` and `flatten`
object NestedForAndFlatten {
//////////////////////////////
(for (x <- Range(1, 5)) yield {
for (y <- Range(2, 6)) yield {
(x, y)
}
}).flatten
/////////////////////////////
for {