Last active
January 19, 2017 11:20
-
-
Save cvogt/33ee15b4922eadbca49f to your computer and use it in GitHub Desktop.
Scala collections breakOut
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
// a List isn't a Map | |
scala> val x: Map[Int,Int] = List(1,2,3).map(i => i -> i) | |
<console>:16: error: type mismatch; | |
found : List[(Int, Int)] | |
required: Map[Int,Int] | |
val x: Map[Int,Int] = List(1,2,3).map(i => i -> i) | |
^ | |
// first create List, then create Map. Intermediate collection overhead | |
scala> val x: Map[Int,Int] = List(1,2,3).map(i => i -> i).toMap | |
x: Map[Int,Int] = Map(1 -> 1, 2 -> 2, 3 -> 3) | |
// Create Map instantly, no intermediate, type Map[Int,Int] inferred from expected | |
scala> val x: Map[Int,Int] = List(1,2,3).map(i => i -> i)(scala.collection.breakOut) | |
x: Map[Int,Int] = Map(1 -> 1, 2 -> 2, 3 -> 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment