Scala 2.13 release – The Updated Collection library

Scala 2.13.0-M5 has been released recently. With this release, the collection library has been updated to simplify the usage. In this blog, I’ll be discussing the notable changes in the collection library that are introduced in this version.

Major collection library changes that we have in the new release are listed below:

  • The parallel collections are now a separate module in the new release.
  • In Scala 2.12, the map method in the TraversableLike trait is defined as below:

    canBuildFrom has been removed from the current release to make it easier to understand.In the current release the map method is defined as:

    The advantage of using this is that no extra time is wasted for searching the implicit value and the type constructor C is refined to a concrete collection type in almost every use case.

  • In the new release, Traversable has been removed from the top of type hierarchy and only Iterable exists. The reason for the change is that Traversable only provides internal iteration (i.e. foreach) and Iterable provides more powerful external iteration that is widely used.
  • scala.Seq is now an alias for scala.collection.immutable.Seq (no longer scala.collection.Seq) in the new release.
  • collection.breakout in Scala2.12 that converts the collection from one type to another directly is absent in the new release. The use case for the collection.breakout is shown below:
    In Scala 2.12:

    In the new release, views can be used to achieve the above functionality using the below line of code:

    We can also use to(Collection) method to achieve the same. Views exist in Scala2.12 but are not used much. In the new release, they have been simplified and work reliably now.

  • Immutable hash sets and hash maps have a new implementation (ChampHashSet and ChampHashMap, based on the “CHAMP” encoding).
  • New collection types:
    • immutable.ArraySeq is an effectively immutable sequence that wraps an array
    • immutable.LazyList is a lazy linked list that is lazy in both the head and the tail. immutable.Stream (only lazy in the tail) is deprecated.
  • Collections now have .lengthIs, .sizeCompare and .sizeIs methods in the new release.
  • Deprecated collections are also removed (MutableList, immutable.Stack, others) from the new release.

I have listed some of the major changes in the collection library. For more details , please have a look at the references below.

References:


knoldus-advt-sticker


Leave a Reply

%d bloggers like this: