Skip to content
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

Add Iterator::{sum_nonempty, product_nonempty} #50884

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use super::{Flatten, FlatMap, flatten_compat};
use super::{Inspect, Map, Peekable, Scan, Skip, SkipWhile, StepBy, Take, TakeWhile, Rev};
use super::{Zip, Sum, Product};
use super::{ChainState, FromIterator, ZipImpl};
use super::sources::once;

fn _assert_is_object_safe(_: &Iterator<Item=()>) {}

Expand Down Expand Up @@ -2265,6 +2266,37 @@ pub trait Iterator {
Sum::sum(self)
}

/// Iterates over the entire iterator, adding all the elements
///
/// An empty iterator returns `None`, otherwise `Some(sum)`.
///
/// # Panics
///
/// When calling `sum_nonempty()` and a primitive integer type is being returned, this
/// method will panic if the computation overflows and debug assertions are
/// enabled.
///
/// # Examples
///
/// ```
/// #![feature(nonempty_iter_arith)]
/// let empty_sum = (1..1).sum_nonempty::<i32>();
/// assert_eq!(empty_sum, None);
///
/// let nonempty_sum = (1..=10).sum_nonempty::<i32>();
/// assert_eq!(nonempty_sum, Some(55));
/// ```
#[unstable(feature = "nonempty_iter_arith",
reason = "recently added unstable API",
issue = "0")]
fn sum_nonempty<S>(mut self) -> Option<S>
where Self: Sized,
S: Sum<Self::Item>,
{
self.next()
.map(|first| once(first).chain(self).sum())
}

/// Iterates over the entire iterator, multiplying all the elements
///
/// An empty iterator returns the one value of the type.
Expand Down Expand Up @@ -2293,6 +2325,36 @@ pub trait Iterator {
Product::product(self)
}

/// Iterates over the entire iterator, multiplying all the elements
///
/// An empty iterator returns `None`, otherwise `Some(product)`.
///
/// # Panics
///
/// When calling `product_nonempty()` and a primitive integer type is being returned,
/// method will panic if the computation overflows and debug assertions are
/// enabled.
///
/// # Examples
/// ```
/// #![feature(nonempty_iter_arith)]
/// let empty_product = (1..1).product_nonempty::<i32>();
/// assert_eq!(empty_product, None);
///
/// let nonempty_product = (1..=10).product_nonempty::<i32>();
/// assert_eq!(nonempty_product, Some(3628800));
/// ```
#[unstable(feature = "nonempty_iter_arith",
reason = "recently added unstable API",
issue = "0")]
fn product_nonempty<P>(mut self) -> Option<P>
where Self: Sized,
P: Product<Self::Item>,
{
self.next()
.map(|first| once(first).chain(self).product())
}

/// Lexicographically compares the elements of this `Iterator` with those
/// of another.
#[stable(feature = "iter_order", since = "1.5.0")]
Expand Down
18 changes: 18 additions & 0 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,15 @@ fn test_iterator_sum() {
assert_eq!(v[..0].iter().cloned().sum::<i32>(), 0);
}

#[test]
fn test_iterator_sum_nonempty() {
let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
assert_eq!(v[..0].iter().cloned().sum_nonempty::<i32>(), None);
assert_eq!(v[1..2].iter().cloned().sum_nonempty::<i32>(), Some(1));
assert_eq!(v[1..3].iter().cloned().sum_nonempty::<i32>(), Some(3));
assert_eq!(v.iter().cloned().sum_nonempty::<i32>(), Some(55));
}

#[test]
fn test_iterator_sum_result() {
let v: &[Result<i32, ()>] = &[Ok(1), Ok(2), Ok(3), Ok(4)];
Expand All @@ -1051,6 +1060,15 @@ fn test_iterator_product() {
assert_eq!(v[..0].iter().cloned().product::<i32>(), 1);
}

#[test]
fn test_iterator_product_nonempty() {
let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
assert_eq!(v[..0].iter().cloned().product_nonempty::<i32>(), None);
assert_eq!(v[..1].iter().cloned().product_nonempty::<i32>(), Some(0));
assert_eq!(v[1..3].iter().cloned().product_nonempty::<i32>(), Some(2));
assert_eq!(v[1..5].iter().cloned().product_nonempty::<i32>(), Some(24));
}

#[test]
fn test_iterator_product_result() {
let v: &[Result<i32, ()>] = &[Ok(1), Ok(2), Ok(3), Ok(4)];
Expand Down
1 change: 1 addition & 0 deletions src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#![feature(flt2dec)]
#![feature(fmt_internals)]
#![feature(hashmap_internals)]
#![feature(nonempty_iter_arith)]
#![feature(pattern)]
#![feature(range_is_empty)]
#![feature(raw)]
Expand Down