scala.collection.breakOut vs views

后端 未结 4 956
北海茫月
北海茫月 2021-02-19 08:01

This SO answer describes how scala.collection.breakOut can be used to prevent creating wasteful intermediate collections. For example, here we create an intermediat

4条回答
  •  走了就别回头了
    2021-02-19 08:29

    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)
    

提交回复
热议问题