Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save dipakc/4043856 to your computer and use it in GitHub Desktop.
Refactoring. Nested `for` and `flatten`
object NestedForAndFlatten {
//////////////////////////////
(for (x <- Range(1, 5)) yield {
for (y <- Range(2, 6)) yield {
(x, y)
}
}).flatten
/////////////////////////////
for {
x <- Range(1, 5)
y <- Range(2, 6)
} yield {
(x, y)
}
/////////////////////////
Range(1, 5) flatMap { x =>
Range(2, 6) map { y =>
(x, y)
}
}
/////////////////////////
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment