Skip to content

Instantly share code, notes, and snippets.

@johnjansen
Last active December 4, 2015 05:54
Show Gist options
  • Save johnjansen/794565f6b074d5b02034 to your computer and use it in GitHub Desktop.
Save johnjansen/794565f6b074d5b02034 to your computer and use it in GitHub Desktop.
Reduce, using iterator guards — Scala
// create a Range
val y = 1 to 20 // => Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
// Reduce the list (ok its not a List, but you get the idea), just because!
val threes = for (i <- y if i % 3 == 0) yield i // => Vector(3, 6, 9, 12, 15, 18)
// and Map
val squares = for (i <- y if i % 3 == 0) yield i * i // => Vector(9, 36, 81, 144, 225, 324)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment