Last active
December 4, 2015 05:54
-
-
Save johnjansen/794565f6b074d5b02034 to your computer and use it in GitHub Desktop.
Reduce, using iterator guards — Scala
This file contains 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
// 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