This SO answer describes how scala.collection.breakOut
can be used to prevent creating wasteful intermediate collections. For example, here we create an intermediat
I don't think views
and breakOut
are identical.
A breakOut
is a CanBuildFrom
implementation used to simplify transformation operations by eliminating intermediary steps. E.g get from A to B without the intermediary collection. A breakOut
means letting Scala choose the appropriate builder object for maximum efficiency of producing new items in a given scenario. More details here.
views
deal with a different type of efficiency, the main sale pitch being: "No more new objects". Views store light references to objects to tackle different usage scenarios: lazy access etc.
Bottom line:
If you map
on a view
you may still get an intermediary collection of references created before the expected result can be produced. You could still have superior performance from:
collection.view.map(somefn)(breakOut)
Than from:
collection.view.map(someFn)