Skip to content

Commit 87fdf35

Browse files
authored
Rollup merge of #65222 - Lucretiel:fold_self, r=kodrAus
Proposal: `fold_self` and `try_fold_self` for Iterators This pull request proposes & implements two new methods on Iterators: `fold_self` and `try_fold_self`. These are variants of `fold` and `try_fold` that use the first element in the iterator as the initial accumulator. Let me know if a public feature like this requires an RFC, or if this pull request is sufficient as place for discussion.
2 parents 697d6b3 + 268408f commit 87fdf35

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

src/libcore/iter/traits/iterator.rs

+39-16
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,43 @@ pub trait Iterator {
20052005
self.try_fold(init, ok(f)).unwrap()
20062006
}
20072007

2008+
/// The same as [`fold()`](#method.fold), but uses the first element in the
2009+
/// iterator as the initial value, folding every subsequent element into it.
2010+
/// If the iterator is empty, return `None`; otherwise, return the result
2011+
/// of the fold.
2012+
///
2013+
/// # Example
2014+
///
2015+
/// Find the maximum value:
2016+
///
2017+
/// ```
2018+
/// #![feature(iterator_fold_self)]
2019+
///
2020+
/// fn find_max<I>(iter: I) -> Option<I::Item>
2021+
/// where I: Iterator,
2022+
/// I::Item: Ord,
2023+
/// {
2024+
/// iter.fold_first(|a, b| {
2025+
/// if a >= b { a } else { b }
2026+
/// })
2027+
/// }
2028+
/// let a = [10, 20, 5, -23, 0];
2029+
/// let b: [u32; 0] = [];
2030+
///
2031+
/// assert_eq!(find_max(a.iter()), Some(&20));
2032+
/// assert_eq!(find_max(b.iter()), None);
2033+
/// ```
2034+
#[inline]
2035+
#[unstable(feature = "iterator_fold_self", issue = "68125")]
2036+
fn fold_first<F>(mut self, f: F) -> Option<Self::Item>
2037+
where
2038+
Self: Sized,
2039+
F: FnMut(Self::Item, Self::Item) -> Self::Item,
2040+
{
2041+
let first = self.next()?;
2042+
Some(self.fold(first, f))
2043+
}
2044+
20082045
/// Tests if every element of the iterator matches a predicate.
20092046
///
20102047
/// `all()` takes a closure that returns `true` or `false`. It applies
@@ -2497,7 +2534,7 @@ pub trait Iterator {
24972534
move |x, y| cmp::max_by(x, y, &mut compare)
24982535
}
24992536

2500-
fold1(self, fold(compare))
2537+
self.fold_first(fold(compare))
25012538
}
25022539

25032540
/// Returns the element that gives the minimum value from the
@@ -2561,7 +2598,7 @@ pub trait Iterator {
25612598
move |x, y| cmp::min_by(x, y, &mut compare)
25622599
}
25632600

2564-
fold1(self, fold(compare))
2601+
self.fold_first(fold(compare))
25652602
}
25662603

25672604
/// Reverses an iterator's direction.
@@ -3214,20 +3251,6 @@ pub trait Iterator {
32143251
}
32153252
}
32163253

3217-
/// Fold an iterator without having to provide an initial value.
3218-
#[inline]
3219-
fn fold1<I, F>(mut it: I, f: F) -> Option<I::Item>
3220-
where
3221-
I: Iterator,
3222-
F: FnMut(I::Item, I::Item) -> I::Item,
3223-
{
3224-
// start with the first element as our selection. This avoids
3225-
// having to use `Option`s inside the loop, translating to a
3226-
// sizeable performance gain (6x in one case).
3227-
let first = it.next()?;
3228-
Some(it.fold(first, f))
3229-
}
3230-
32313254
#[stable(feature = "rust1", since = "1.0.0")]
32323255
impl<I: Iterator + ?Sized> Iterator for &mut I {
32333256
type Item = I::Item;

0 commit comments

Comments
 (0)