-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Some way to perform in-place sorting in iterator chains? #38882
Comments
What would be the advantages of this addition, aside from bringing Rust more in line with Python and D? You can already sort in various ways: |
Python doesn't have chains of lazy iterables like D, Rust, F# and other languages, so this change is unrelated to Python. The purposes of this change are:
some_iterator Some use cases are covered by adding an iterator method like sorted() that calls collect::<Vec<_>> inside itself. While this doesn't cover all cases, it's backward compatible, and it's simpler to use correctly. |
Sorting of slices in the Rust standard library is not actually in place. :-) The usual process for adding things to the standard library is to write and submit an rfc. The steps are explained here. Something similar to what you propose is already available in the itertools crate, so part of evaluating whether this should be in the standard library could be researching how many crates have itertools as a dependency and use that method. Personally I think maybe a trait extending |
Right, "in place sorting" has a technical meaning. What I was trying to say is "modifies the input data, instead of copying it and sorting the copy". Yes, there are some things in the itertools crate that in my opinion should be moved in the std library. The sorted/sorted_by of itertools are indeed the partial solutions I was thinking about. But the "full solution" is more like the sort() of D language, that modifies the input data and returns some view of it, instead of copying it and sorting the copy as sorted().
I think listing some usage examples could help show why people would find it useful in practice. But this is for the RFC... |
I agree that this could be handled better by the standard library. The approach in D is really neat! I think the best path forward would be an RFC to suggest a concrete API for this in Rust. |
In the Python language sort() doesn't return the sorted items, probably to better remind the programmer that sort is a procedure that works in-place (there is a sorted() function, that returns the sorted items, but it copies the input, so it's not in-place):
In D language the chains of ranges (similar to the Rust chains of iterators) are quite common, so the D standard library uses an intermediate solution:
In D sort() doesn't return the sorted items, but a SortedRange struct that denotes that the items it refers to are sorted (and this struct allows nice binary searches and other quite useful things, that are not possible equally nicely in Rust) and you can pull out the sorted items using the SortedRange.release() method. This allows to both use sort() in range chains and avoid bugs.
Currently in Rust you can't use sort() in iterator chains():
Can we improve this situation in some way?
The text was updated successfully, but these errors were encountered: