Only call the closure parameter of Iterator::is_sorted_by_key once per item

This commit is contained in:
Tim Vermeulen 2019-07-07 20:46:38 +02:00
parent 8ebd67e4ee
commit 98b54fbd7a
2 changed files with 5 additions and 5 deletions

View File

@ -2572,13 +2572,13 @@ pub trait Iterator {
/// ```
#[inline]
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
fn is_sorted_by_key<F, K>(self, mut f: F) -> bool
fn is_sorted_by_key<F, K>(self, f: F) -> bool
where
Self: Sized,
F: FnMut(&Self::Item) -> K,
F: FnMut(Self::Item) -> K,
K: PartialOrd
{
self.is_sorted_by(|a, b| f(a).partial_cmp(&f(b)))
self.map(f).is_sorted()
}
}

View File

@ -2459,12 +2459,12 @@ impl<T> [T] {
/// ```
#[inline]
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
pub fn is_sorted_by_key<F, K>(&self, mut f: F) -> bool
pub fn is_sorted_by_key<F, K>(&self, f: F) -> bool
where
F: FnMut(&T) -> K,
K: PartialOrd
{
self.is_sorted_by(|a, b| f(a).partial_cmp(&f(b)))
self.iter().is_sorted_by_key(f)
}
}