-
Notifications
You must be signed in to change notification settings - Fork 13.4k
The return of the GroupBy and GroupByMut iterators on slice #79895
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
Changes from all commits
a891f6e
1c55a73
e16eaea
005912f
1b406af
0ebf8e1
6a5a600
5190fe4
9940c47
45693b4
7952ea5
b2a7076
a2d55d7
8b53be6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -57,6 +57,9 @@ pub use iter::{ArrayChunks, ArrayChunksMut}; | |||||||||||||||||||||||||||||||||||
#[unstable(feature = "array_windows", issue = "75027")] | ||||||||||||||||||||||||||||||||||||
pub use iter::ArrayWindows; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
#[unstable(feature = "slice_group_by", issue = "80552")] | ||||||||||||||||||||||||||||||||||||
pub use iter::{GroupBy, GroupByMut}; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
#[unstable(feature = "split_inclusive", issue = "72360")] | ||||||||||||||||||||||||||||||||||||
pub use iter::{SplitInclusive, SplitInclusiveMut}; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
@@ -1207,6 +1210,96 @@ impl<T> [T] { | |||||||||||||||||||||||||||||||||||
RChunksExactMut::new(self, chunk_size) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/// Returns an iterator over the slice producing non-overlapping runs | ||||||||||||||||||||||||||||||||||||
/// of elements using the predicate to separate them. | ||||||||||||||||||||||||||||||||||||
Comment on lines
+1213
to
+1214
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. :) |
||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// The predicate is called on two elements following themselves, | ||||||||||||||||||||||||||||||||||||
/// it means the predicate is called on `slice[0]` and `slice[1]` | ||||||||||||||||||||||||||||||||||||
/// then on `slice[1]` and `slice[2]` and so on. | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// # Examples | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||||
/// #![feature(slice_group_by)] | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// 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); | ||||||||||||||||||||||||||||||||||||
Comment on lines
+1225
to
+1232
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 |
||||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||||
Kerollmops marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// This method can be used to extract the sorted subslices: | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||||
/// #![feature(slice_group_by)] | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// let slice = &[1, 1, 2, 3, 2, 3, 2, 3, 4]; | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// let mut iter = slice.group_by(|a, b| a <= b); | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), Some(&[1, 1, 2, 3][..])); | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), Some(&[2, 3][..])); | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), Some(&[2, 3, 4][..])); | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), None); | ||||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||||
#[unstable(feature = "slice_group_by", issue = "80552")] | ||||||||||||||||||||||||||||||||||||
#[inline] | ||||||||||||||||||||||||||||||||||||
pub fn group_by<F>(&self, pred: F) -> GroupBy<'_, T, F> | ||||||||||||||||||||||||||||||||||||
where | ||||||||||||||||||||||||||||||||||||
F: FnMut(&T, &T) -> bool, | ||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||
GroupBy::new(self, pred) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/// Returns an iterator over the slice producing non-overlapping mutable | ||||||||||||||||||||||||||||||||||||
/// runs of elements using the predicate to separate them. | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// The predicate is called on two elements following themselves, | ||||||||||||||||||||||||||||||||||||
/// it means the predicate is called on `slice[0]` and `slice[1]` | ||||||||||||||||||||||||||||||||||||
/// then on `slice[1]` and `slice[2]` and so on. | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// # Examples | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||||
/// #![feature(slice_group_by)] | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// let slice = &mut [1, 1, 1, 3, 3, 2, 2, 2]; | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// let mut iter = slice.group_by_mut(|a, b| a == b); | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), Some(&mut [1, 1, 1][..])); | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), Some(&mut [3, 3][..])); | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), Some(&mut [2, 2, 2][..])); | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), None); | ||||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// This method can be used to extract the sorted subslices: | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||||
/// #![feature(slice_group_by)] | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// let slice = &mut [1, 1, 2, 3, 2, 3, 2, 3, 4]; | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// let mut iter = slice.group_by_mut(|a, b| a <= b); | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), Some(&mut [1, 1, 2, 3][..])); | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), Some(&mut [2, 3][..])); | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), Some(&mut [2, 3, 4][..])); | ||||||||||||||||||||||||||||||||||||
/// assert_eq!(iter.next(), None); | ||||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||||
#[unstable(feature = "slice_group_by", issue = "80552")] | ||||||||||||||||||||||||||||||||||||
#[inline] | ||||||||||||||||||||||||||||||||||||
pub fn group_by_mut<F>(&mut self, pred: F) -> GroupByMut<'_, T, F> | ||||||||||||||||||||||||||||||||||||
where | ||||||||||||||||||||||||||||||||||||
F: FnMut(&T, &T) -> bool, | ||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||
GroupByMut::new(self, pred) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/// Divides one slice into two at an index. | ||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||
/// The first will contain all indices from `[0, mid)` (excluding | ||||||||||||||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.