This SO answer describes how scala.collection.breakOut
can be used to prevent creating wasteful intermediate collections. For example, here we create an intermediate Seq[(String,String)]
:
val m = List("A", "B", "C").map(x => x -> x).toMap
By using breakOut
we can prevent the creation of this intermediate Seq
:
val m: Map[String,String] = List("A", "B", "C").map(x => x -> x)(breakOut)
Views solve the same problem and in addition access elements lazily:
val m = (List("A", "B", "C").view map (x => x -> x)).toMap
I am assuming the creation of the View
wrappers is fairly cheap, so my question is: Is there any real reason to use breakOut
over View
s?
You're going to make a trip from England to France.
With view: you're taking a set of notes in your notebook and boom, once you've called .force() you start making all of them: buy a ticket, board on the plane, ....
With breakOut: you're departing and boom, you in the Paris looking at the Eiffel tower. You don't remember how exactly you've arrived there, but you did this trip actually, just didn't make any memories.
Bad analogy, but I hope this give you a taste of what is the difference between them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With