Only call the closure parameter of Iterator::is_sorted_by_key once per item
This commit is contained in:
parent
8ebd67e4ee
commit
98b54fbd7a
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue