Skip to content

Commit

Permalink
Auto merge of #35856 - phimuemue:master, r=brson
Browse files Browse the repository at this point in the history
Introduce max_by/min_by on iterators

See rust-lang/rfcs#1722 for reference.

It seems that there is `min`, `max` (simple computation of min/max), `min_by_key`, `max_by_key` (min/max by comparing mapped values) but no `min_by` and `max_by` (min/max according to comparison function). However, e.g. on vectors or slices there is `sort`, `sort_by_key` and `sort_by`.
  • Loading branch information
bors authored Sep 3, 2016
2 parents 100b309 + 5928be1 commit d128e6b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,32 @@ pub trait Iterator {
.map(|(_, x)| x)
}

/// Returns the element that gives the maximum value with respect to the
/// specified comparison function.
///
/// Returns the rightmost element if the comparison determines two elements
/// to be equally maximum.
///
/// # Examples
///
/// ```
/// #![feature(iter_max_by)]
/// let a = [-3_i32, 0, 1, 5, -10];
/// assert_eq!(*a.iter().max_by(|x, y| x.cmp(y)).unwrap(), 5);
/// ```
#[inline]
#[unstable(feature = "iter_max_by", issue="36105")]
fn max_by<F>(self, mut compare: F) -> Option<Self::Item>
where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,
{
select_fold1(self,
|_| (),
// switch to y even if it is only equal, to preserve
// stability.
|_, x, _, y| Ordering::Greater != compare(x, y))
.map(|(_, x)| x)
}

/// Returns the element that gives the minimum value from the
/// specified function.
///
Expand All @@ -1681,6 +1707,33 @@ pub trait Iterator {
.map(|(_, x)| x)
}

/// Returns the element that gives the minimum value with respect to the
/// specified comparison function.
///
/// Returns the latest element if the comparison determines two elements
/// to be equally minimum.
///
/// # Examples
///
/// ```
/// #![feature(iter_min_by)]
/// let a = [-3_i32, 0, 1, 5, -10];
/// assert_eq!(*a.iter().min_by(|x, y| x.cmp(y)).unwrap(), -10);
/// ```
#[inline]
#[unstable(feature = "iter_min_by", issue="36105")]
fn min_by<F>(self, mut compare: F) -> Option<Self::Item>
where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,
{
select_fold1(self,
|_| (),
// switch to y even if it is strictly smaller, to
// preserve stability.
|_, x, _, y| Ordering::Greater == compare(x, y))
.map(|(_, x)| x)
}


/// Reverses an iterator's direction.
///
/// Usually, iterators iterate from left to right. After using `rev()`,
Expand Down
12 changes: 12 additions & 0 deletions src/libcoretest/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,12 +664,24 @@ fn test_max_by_key() {
assert_eq!(*xs.iter().max_by_key(|x| x.abs()).unwrap(), -10);
}

#[test]
fn test_max_by() {
let xs: &[isize] = &[-3, 0, 1, 5, -10];
assert_eq!(*xs.iter().max_by(|x, y| x.abs().cmp(&y.abs())).unwrap(), -10);
}

#[test]
fn test_min_by_key() {
let xs: &[isize] = &[-3, 0, 1, 5, -10];
assert_eq!(*xs.iter().min_by_key(|x| x.abs()).unwrap(), 0);
}

#[test]
fn test_min_by() {
let xs: &[isize] = &[-3, 0, 1, 5, -10];
assert_eq!(*xs.iter().min_by(|x, y| x.abs().cmp(&y.abs())).unwrap(), 0);
}

#[test]
fn test_by_ref() {
let mut xs = 0..10;
Expand Down
2 changes: 2 additions & 0 deletions src/libcoretest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#![feature(try_from)]
#![feature(unicode)]
#![feature(unique)]
#![feature(iter_max_by)]
#![feature(iter_min_by)]

extern crate core;
extern crate test;
Expand Down

0 comments on commit d128e6b

Please sign in to comment.