scala.collection.breakOut vs views

后端 未结 4 955
北海茫月
北海茫月 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:31

    As of Scala 2.13, this is no longer a concern. Breakout has been removed and views are the recommended replacement.


    Scala 2.13 Collections Rework

    Views are also the recommended replacement for collection.breakOut. For example,

    val s: Seq[Int] = ... 
    val set: Set[String] = s.map(_.toString)(collection.breakOut)
    

    can be expressed with the same performance characteristics as:

    val s: Seq[Int] = ... 
    val set = s.view.map(_.toString).to(Set)
    

提交回复
热议问题