Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala.collection.breakOut vs views

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 Views?

like image 398
theon Avatar asked Jan 22 '14 14:01

theon


1 Answers

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.

like image 91
om-nom-nom Avatar answered Sep 23 '22 20:09

om-nom-nom