Skip to content

Add a SortedSeq collection type #11725

Closed
Closed
@neontorrent

Description

@neontorrent

This is a new proposal for a previously closed issue: scala/collection-strawman#132


Use case:

In the past we had CanBuildFrom that we can pass to map/collect/etc. methods, and turn the collection into something else. I personally use coll.map[B, That] {/* lambda */}(new CanBuildFrom[Coll, B, That] {/* impl */}).sortedBy(_.intField) to sort any Iterable into a sorted Seq.

This is a better performant version than coll.map{/**/}.toSeq.sortedBy(_.intField) as it does not create an intermediate collection Seq

However after 2.13, we no longer have CanBuildFrom to pass into map/collect/etc., and there is no alternative to convert an Iterable to a sorted collection without creating an intermediate Seq.

Proposal:

Since CanBuildFrom is gone for good, I propose we should have a SortedSeq so that the use case above can be achieved with less performance penalty. Then we can do:

val coll: Iterable[String] = List("100", "11")
coll.to(SortedSeq.evidenceIterableFactory(Ordering.by((_: String).toInt)))

This allows duplicate items in the collection, which SortedSet cannot do.

An example of possible implementation can be found at: https://contributors.scala-lang.org/t/new-collection-sortedlist/314/21

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions