-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Proposal: fold_self
and try_fold_self
for Iterators
#60103
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1586,6 +1586,46 @@ pub trait Iterator { | |
Try::from_ok(accum) | ||
} | ||
|
||
/// The same as [`try_fold()`](#method.try_fold), but uses the first element | ||
/// in the iterator as the initial value, folding every subsequent element | ||
/// into it. If the iterator is empty, return None; otherwise, return the | ||
/// result of the fold. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Find the maximum value: | ||
/// | ||
/// ``` | ||
/// use std::cmp::Ordering; | ||
/// fn find_max<I>(iter: I) -> Option<I::Item> | ||
/// where I: Iterator, | ||
/// I::Item: PartialCmp, | ||
/// { | ||
/// iter.try_fold_self(|a, b| { | ||
/// a.partial_cmp(b).map(move |cmp| match cmp { | ||
/// Ordering::Greater | Ordering::Equal => a, | ||
/// Ordering::Less => b, | ||
/// }) | ||
/// }) | ||
/// } | ||
/// let a = [10, 20, 5, -23, 0]; | ||
/// let b = [10, 20, -23, std::f64::NAN, 12, 2.5]; | ||
/// let c = []; | ||
/// | ||
/// assert_eq!(find_max(a.iter()), Some(Some(20))); | ||
/// assert_eq!(find_max(b.iter()), Some(None)); | ||
/// assert_eq!(find_max(c.iter()), None); | ||
/// ``` | ||
#[inline] | ||
#[unstable(feature = "iterator_fold_self", issue = "60103")] | ||
fn try_fold_self<F, R>(&mut self, mut f: F) -> Option<R> | ||
where Self: Sized, | ||
F: FnMut(Self::Item, Self::Item) -> R, | ||
R: Try<Ok = Self::Item> | ||
{ | ||
self.next().map(move |first| self.try_fold(first, f)) | ||
} | ||
|
||
/// An iterator method that applies a fallible function to each item in the | ||
/// iterator, stopping at the first error and returning that error. | ||
/// | ||
|
@@ -1696,6 +1736,41 @@ pub trait Iterator { | |
self.try_fold(init, move |acc, x| Ok::<B, !>(f(acc, x))).unwrap() | ||
} | ||
|
||
/// The same as [`fold()`](#method.fold), but uses the first element in the | ||
/// iterator as the initial value, folding every subsequent element into it. | ||
/// If the iterator is empty, return `None`; otherwise, return the result | ||
/// of the fold. | ||
/// | ||
/// # Example | ||
/// | ||
/// Find the maximum value: | ||
/// | ||
/// ``` | ||
/// fn find_max<I>(iter: I) -> Option<I::Item> | ||
/// where I: Iterator, | ||
/// I::Item: Ord, | ||
/// { | ||
/// iter.fold_self(|a, b| { | ||
/// a.partial_cmp(b).map(move |cmp| match cmp { | ||
/// Ordering::Greater | Ordering::Equal => a, | ||
/// Ordering::Less => b, | ||
/// }) | ||
/// }) | ||
/// } | ||
/// let a = [10, 20, 5, -23, 0]; | ||
/// let b = []; | ||
/// | ||
/// assert_eq!(find_max(a.iter()), Some(20)); | ||
/// assert_eq!(find_max(b.iter()), None)); | ||
/// ``` | ||
#[inline] | ||
#[unstable(feature = "iterator_fold_self", issue = "60103")] | ||
fn fold_self<F>(mut self, mut f: F) -> Option<Self::Item> | ||
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. For reference, in itertools, this is https://docs.rs/itertools/0.8.0/itertools/trait.Itertools.html#method.fold1 which is inspired by https://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#v:foldl1 in Haskell. |
||
where Self: Sized, F: FnMut(Self::Item, Self::Item) -> Self::Item | ||
{ | ||
self.next().map(move |first| self.fold(first, f)) | ||
} | ||
|
||
/// Tests if every element of the iterator matches a predicate. | ||
/// | ||
/// `all()` takes a closure that returns `true` or `false`. It applies | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Aside from the behavior for
Equal
, this is justIterator::max
-- maybe a different example can be provided?