Skip to content

Instantly share code, notes, and snippets.

@cvogt
Last active January 19, 2017 11:20
Show Gist options
  • Save cvogt/33ee15b4922eadbca49f to your computer and use it in GitHub Desktop.
Save cvogt/33ee15b4922eadbca49f to your computer and use it in GitHub Desktop.
Scala collections breakOut
// 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