-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Set#sorted to make a sorted Seq without unnecessary copies #11711
Comments
@joshlemer wdyt? |
I think having a method in the hypothetical class object View {
class Sorted[+A](it: SomeIterableOps[A])(o: Ordering[A]) {
override def toSeq: Seq[A] =
ArraySeq.unsafeWrapArray(it.toArray.sortInPlace.array)
}
} |
@texasbruce has a different proposal which might also work for this: #11725 |
I think going through Views is more versatile and less heavy way to deal with this. For one thing, we already have SortedMultiSet (maybe one day SortedBag) in ScalaCollectionContrib, and as mentioned in the closed issue, SortedSeq doesn't really behave like a Seq (what should Second, inventing a new collection type doesn't help when you want to go from a Set to a sorted List, Vector, ArrayBuffer, Array, etc. The view approach does though: val v = Set(1,2,3).view.sorted
v.to(Vector)
v.to(ArrayDeque)
... Last, by making
|
As a subclass of Seq, it makes sense to have a more restrictive behavior than the super class, but I don't think it makes it a different type of thing. We can definitely discuss more on the "unintuitive" behaviors that may imply. Using Should we have an updated method on Seq in the first place? Seq is unindexed collection and why do we have a method that takes an index and update the element? Shouldn't we use IndexedSeq in that case? |
@texasbruce Have you considered using |
@joshlemer It could work in my use case. Just concern about the performance. Do we have documentation of the big O for the collections in the scala-collection-contrib module? Also is there any plan to merge into the core library? |
I don't know of anywhere where the performance characteristics of the collections-contrib data structures are documented, that would be a great issue to create 😉 . But basically, under the hood the @texasbruce I am not aware of any plan to merge these data structures into core. |
another possibility is to add a |
Right now it seems that to get a sorted Seq out of a Set, one has to write
.toSeq.sorted
which creates an unnecessary intermediate collection.The text was updated successfully, but these errors were encountered: