Skip to content
Merged
Changes from 2 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
55 changes: 48 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4071,17 +4071,17 @@ pub trait Itertools: Iterator {
/// use itertools::Itertools;
/// use itertools::MinMaxResult::{MinMax, NoElements, OneElement};
///
/// let a: [i32; 0] = [];
/// let a: [(i32, char); 0] = [];
/// assert_eq!(a.iter().minmax(), NoElements);
///
/// let a = [1];
/// assert_eq!(a.iter().minmax(), OneElement(&1));
/// let a = [(1, 'a')];
/// assert_eq!(a.iter().minmax(), OneElement(&(1, 'a')));
///
/// let a = [1, 2, 3, 4, 5];
/// assert_eq!(a.iter().minmax(), MinMax(&1, &5));
/// let a = [(0, 'a'), (1, 'b')];
/// assert_eq!(a.iter().minmax(), MinMax(&(0, 'a'), &(1, 'b')));
///
/// let a = [1, 1, 1, 1];
/// assert_eq!(a.iter().minmax(), MinMax(&1, &1));
/// let a = [(1, 'a'), (1, 'b'), (1, 'c')];
/// assert_eq!(a.iter().minmax(), MinMax(&(1, 'a'), &(1, 'c')));
Comment on lines 4074 to 4101
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave them simple numbers, and add one representative example with tuples as an extra case. (Tuple comparisons are harder to understand, so we should keep the simple cases.)

/// ```
///
/// The elements can be floats but no particular result is guaranteed
Expand All @@ -4105,6 +4105,26 @@ pub trait Itertools: Iterator {
///
/// The keys can be floats but no particular result is guaranteed
/// if a key is NaN.
///
/// # Examples
/// ```
/// use itertools::Itertools;
/// use itertools::MinMaxResult::{MinMax, NoElements, OneElement};
///
/// let cmp_key = |x: &&(i32, char)| x.0;
///
/// let a: [(i32, char); 0] = [];
/// assert_eq!(a.iter().minmax_by_key(cmp_key), NoElements);
///
/// let a = [(1, 'a')];
/// assert_eq!(a.iter().minmax_by_key(cmp_key), OneElement(&(1, 'a')));
///
/// let a = [(0, 'a'), (1, 'b')];
/// assert_eq!(a.iter().minmax_by_key(cmp_key), MinMax(&(0, 'a'), &(1, 'b')));
///
/// let a = [(1, 'a'), (1, 'b'), (1, 'c')];
/// assert_eq!(a.iter().minmax_by_key(cmp_key), MinMax(&(1, 'a'), &(1, 'c')));
/// ```
fn minmax_by_key<K, F>(self, key: F) -> MinMaxResult<Self::Item>
where
Self: Sized,
Expand All @@ -4122,6 +4142,27 @@ pub trait Itertools: Iterator {
/// For the minimum, the first minimal element is returned. For the maximum,
/// the last maximal element wins. This matches the behavior of the standard
/// [`Iterator::min`] and [`Iterator::max`] methods.
///
/// # Examples
///
/// ```
/// use itertools::Itertools;
/// use itertools::MinMaxResult::{MinMax, NoElements, OneElement};
///
/// let abs_cmp = |x: &&(i32, char), y: &&(i32, char)| x.0.cmp(&y.0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abs_cmp does not seem a reasonable name for the closure.

///
/// let a: [(i32, char); 0] = [];
/// assert_eq!(a.iter().minmax_by(abs_cmp), NoElements);
///
/// let a = [(1, 'a')];
/// assert_eq!(a.iter().minmax_by(abs_cmp), OneElement(&(1, 'a')));
///
/// let a = [(0, 'a'), (1, 'b')];
/// assert_eq!(a.iter().minmax_by(abs_cmp), MinMax(&(0, 'a'), &(1, 'b')));
///
/// let a = [(1, 'a'), (1, 'b'), (1, 'c')];
/// assert_eq!(a.iter().minmax_by(abs_cmp), MinMax(&(1, 'a'), &(1, 'c')));
/// ```
fn minmax_by<F>(self, mut compare: F) -> MinMaxResult<Self::Item>
where
Self: Sized,
Expand Down
Loading