-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
The return of the GroupBy and GroupByMut iterators on slice #79895
Conversation
Hey @rylev, Thanks for the comment, I changed the tests a little bit, however, do you have an idea on how I should fix this CI tidy issue? The file is now too long what should I do? Create a new one just for those two structs?
|
@Kerollmops I'm not sure. I'd also be curious to know what the right move is here (either to suppress the warning or to break up the file). |
I think it's fine to just suppress the warning for now. Splitting |
1f1ee7e
to
59f795a
Compare
59f795a
to
b2a7076
Compare
cc @rust-lang/infra bors didn't assign anyone here r? @m-ou-se |
FYI I think you mean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This looks great!
2 years ago
Apologies again! We're working on streamlining our processes. This shouldn't happen again. :)
Create a new one just for those two structs?
We should probably split files like these into more submodules at some point (just like #78934 does). A separate file for these new structs would be good (e.g. slice/iter/group_by.rs
). But leaving that for later is also fine. Then it can be done together with the rest of the file.
Only one thing left before we can merge this:
Can you open a tracking issue for this, and add its number in the unstable
attributes? Thanks!
(By the way, feel free to rebase/squash commits.)
I just created the tracking issue, added it to the files, and squashed everything. No worries about the 2 years waiting time, I implemented it myself, it was quite fun! I also benchmarked it and the full safe Rust version is a little bit slower than the unsafe one. |
Thanks! @bors r+ |
📌 Commit 8b53be6 has been approved by |
/// Returns an iterator over the slice producing non-overlapping runs | ||
/// of elements using the predicate to separate them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this should have a stronger mark on the fact that it only groups consecutive items, especially with such an ambiguous function name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say 'runs of elements' already implies that. But I'd be happy to review your PR if you have an idea on how to improve the wording. Improvements to documentation are always welcome. :)
/// let slice = &[1, 1, 1, 3, 3, 2, 2, 2]; | ||
/// | ||
/// let mut iter = slice.group_by(|a, b| a == b); | ||
/// | ||
/// assert_eq!(iter.next(), Some(&[1, 1, 1][..])); | ||
/// assert_eq!(iter.next(), Some(&[3, 3][..])); | ||
/// assert_eq!(iter.next(), Some(&[2, 2, 2][..])); | ||
/// assert_eq!(iter.next(), None); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this gives a false perception that it groups the same items, no matter where they are. Would probably be better if the same element appeared multiple times in different groups, for example:
/// let slice = &[1, 1, 1, 3, 3, 2, 2, 2]; | |
/// | |
/// let mut iter = slice.group_by(|a, b| a == b); | |
/// | |
/// assert_eq!(iter.next(), Some(&[1, 1, 1][..])); | |
/// assert_eq!(iter.next(), Some(&[3, 3][..])); | |
/// assert_eq!(iter.next(), Some(&[2, 2, 2][..])); | |
/// assert_eq!(iter.next(), None); | |
/// let slice = &[1, 1, 1, 3, 3, 2, 2, 2, 1, 1]; | |
/// | |
/// let mut iter = slice.group_by(|a, b| a == b); | |
/// | |
/// assert_eq!(iter.next(), Some(&[1, 1, 1][..])); | |
/// assert_eq!(iter.next(), Some(&[3, 3][..])); | |
/// assert_eq!(iter.next(), Some(&[2, 2, 2][..])); | |
/// assert_eq!(iter.next(), Some(&[1, 1][..])); | |
/// assert_eq!(iter.next(), None); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand your point but I find the second example clear enough and let the user understand that the split are based on the specified function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if someone skips the second example? You could argue that's the user's fault, but I'd like to make this as much foolproof as we can
☀️ Test successful - checks-actions |
Thank you for the review @m-ou-se :) |
According to rust-lang/rfcs#2477 (comment), I am opening this PR again, this time I implemented it in safe Rust only, it is therefore much easier to read and is completely safe.
This PR proposes to add two new methods to the slice, the
group_by
andgroup_by_mut
. These two methods provide a way to iterate over non-overlapping sub-slices of a base slice that are separated by the predicate given by the user (e.g.Partial::eq
,|a, b| a.abs() < b.abs()
).An RFC was open 2 years ago but wasn't necessary.