Skip to content

Commit 98b54fb

Browse files
committed
Only call the closure parameter of Iterator::is_sorted_by_key once per item
1 parent 8ebd67e commit 98b54fb

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/libcore/iter/traits/iterator.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2572,13 +2572,13 @@ pub trait Iterator {
25722572
/// ```
25732573
#[inline]
25742574
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
2575-
fn is_sorted_by_key<F, K>(self, mut f: F) -> bool
2575+
fn is_sorted_by_key<F, K>(self, f: F) -> bool
25762576
where
25772577
Self: Sized,
2578-
F: FnMut(&Self::Item) -> K,
2578+
F: FnMut(Self::Item) -> K,
25792579
K: PartialOrd
25802580
{
2581-
self.is_sorted_by(|a, b| f(a).partial_cmp(&f(b)))
2581+
self.map(f).is_sorted()
25822582
}
25832583
}
25842584

src/libcore/slice/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2459,12 +2459,12 @@ impl<T> [T] {
24592459
/// ```
24602460
#[inline]
24612461
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
2462-
pub fn is_sorted_by_key<F, K>(&self, mut f: F) -> bool
2462+
pub fn is_sorted_by_key<F, K>(&self, f: F) -> bool
24632463
where
24642464
F: FnMut(&T) -> K,
24652465
K: PartialOrd
24662466
{
2467-
self.is_sorted_by(|a, b| f(a).partial_cmp(&f(b)))
2467+
self.iter().is_sorted_by_key(f)
24682468
}
24692469
}
24702470

0 commit comments

Comments
 (0)