Skip to content

Commit

Permalink
Only call the closure parameter of Iterator::is_sorted_by_key once pe…
Browse files Browse the repository at this point in the history
…r item
  • Loading branch information
timvermeulen committed Jul 7, 2019
1 parent 8ebd67e commit 98b54fb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/libcore/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 98b54fb

Please sign in to comment.