From 5c056ed2f5d118cfd5d936b10a9c34d8813a6c09 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 7 Dec 2020 21:23:29 +0100 Subject: [PATCH 1/3] Rename Iterator::fold_first to reduce. --- compiler/rustc_hir/src/hir.rs | 2 +- library/core/src/iter/traits/iterator.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 1c16dc026670b..6487b23a6a60a 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -358,7 +358,7 @@ impl GenericArgs<'_> { .iter() .filter(|arg| !arg.is_synthetic()) .map(|arg| arg.span()) - .fold_first(|span1, span2| span1.to(span2)) + .reduce(|span1, span2| span1.to(span2)) } /// Returns span encompassing arguments and their surrounding `<>` or `()` diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index c961e2964a943..7a3700d692364 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -2138,7 +2138,7 @@ pub trait Iterator { /// where I: Iterator, /// I::Item: Ord, /// { - /// iter.fold_first(|a, b| { + /// iter.reduce(|a, b| { /// if a >= b { a } else { b } /// }) /// } @@ -2150,7 +2150,7 @@ pub trait Iterator { /// ``` #[inline] #[unstable(feature = "iterator_fold_self", issue = "68125")] - fn fold_first(mut self, f: F) -> Option + fn reduce(mut self, f: F) -> Option where Self: Sized, F: FnMut(Self::Item, Self::Item) -> Self::Item, @@ -2647,7 +2647,7 @@ pub trait Iterator { move |x, y| cmp::max_by(x, y, &mut compare) } - self.fold_first(fold(compare)) + self.reduce(fold(compare)) } /// Returns the element that gives the minimum value from the @@ -2707,7 +2707,7 @@ pub trait Iterator { move |x, y| cmp::min_by(x, y, &mut compare) } - self.fold_first(fold(compare)) + self.reduce(fold(compare)) } /// Reverses an iterator's direction. From 26af55f5c63e248757ec2be72061e2bcfbc78331 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 7 Dec 2020 21:23:50 +0100 Subject: [PATCH 2/3] Improve documentation of Iterator::{fold, reduce}. --- library/core/src/iter/traits/iterator.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 7a3700d692364..648895e12f1dd 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -2028,7 +2028,8 @@ pub trait Iterator { self.try_fold((), call(f)) } - /// An iterator method that applies a function, producing a single, final value. + /// Folds every element into an accumulator by applying an operation, + /// returning the final result. /// /// `fold()` takes two arguments: an initial value, and a closure with two /// arguments: an 'accumulator', and an element. The closure returns the value that @@ -2049,6 +2050,9 @@ pub trait Iterator { /// may not terminate for infinite iterators, even on traits for which a /// result is determinable in finite time. /// + /// Note: [`reduce()`] can be used to use the first element as the initial + /// value, if the accumulator type and item type is the same. + /// /// # Note to Implementors /// /// Several of the other (forward) methods have default implementations in @@ -2104,6 +2108,8 @@ pub trait Iterator { /// // they're the same /// assert_eq!(result, result2); /// ``` + /// + /// [`reduce()`]: Iterator::reduce #[doc(alias = "reduce")] #[doc(alias = "inject")] #[inline] @@ -2120,10 +2126,15 @@ pub trait Iterator { accum } - /// The same as [`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. + /// Reduces the elements to a single one, by repeatedly applying a reducing + /// operation. + /// + /// If the iterator is empty, returns [`None`]; otherwise, returns the + /// result of the reduction. + /// + /// For iterators with at least one element, this is the same as [`fold()`] + /// with the first element of the iterator as the initial value, folding + /// every subsequent element into it. /// /// [`fold()`]: Iterator::fold /// From 24e09401696e6b2689ae4c78d9a24504a17f1e3a Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 7 Dec 2020 21:24:07 +0100 Subject: [PATCH 3/3] Stabilize feature(iterator_fold_self): Iterator::reduce --- compiler/rustc_ast/src/lib.rs | 1 - compiler/rustc_hir/src/lib.rs | 1 - library/core/src/iter/traits/iterator.rs | 4 +--- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/compiler/rustc_ast/src/lib.rs b/compiler/rustc_ast/src/lib.rs index 0f411eda49dbc..ddf52caed088a 100644 --- a/compiler/rustc_ast/src/lib.rs +++ b/compiler/rustc_ast/src/lib.rs @@ -14,7 +14,6 @@ #![feature(const_fn_transmute)] #![feature(const_panic)] #![feature(crate_visibility_modifier)] -#![feature(iterator_fold_self)] #![feature(label_break_value)] #![feature(nll)] #![feature(or_patterns)] diff --git a/compiler/rustc_hir/src/lib.rs b/compiler/rustc_hir/src/lib.rs index efc516a662fb7..c69a9b063aeca 100644 --- a/compiler/rustc_hir/src/lib.rs +++ b/compiler/rustc_hir/src/lib.rs @@ -6,7 +6,6 @@ #![feature(const_fn)] // For the unsizing cast on `&[]` #![feature(const_panic)] #![feature(in_band_lifetimes)] -#![feature(iterator_fold_self)] #![feature(once_cell)] #![feature(or_patterns)] #![recursion_limit = "256"] diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 648895e12f1dd..6b42d456205cd 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -2143,8 +2143,6 @@ pub trait Iterator { /// Find the maximum value: /// /// ``` - /// #![feature(iterator_fold_self)] - /// /// fn find_max(iter: I) -> Option /// where I: Iterator, /// I::Item: Ord, @@ -2160,7 +2158,7 @@ pub trait Iterator { /// assert_eq!(find_max(b.iter()), None); /// ``` #[inline] - #[unstable(feature = "iterator_fold_self", issue = "68125")] + #[stable(feature = "iterator_fold_self", since = "1.51.0")] fn reduce(mut self, f: F) -> Option where Self: Sized,