diff options
| author | Tim Vermeulen <tvermeulen@me.com> | 2019-07-07 20:46:38 +0200 |
|---|---|---|
| committer | Tim Vermeulen <tvermeulen@me.com> | 2019-07-07 20:46:38 +0200 |
| commit | 98b54fbd7afd55f40531bdb350a5adb870958111 (patch) | |
| tree | fd7f92e287a57b68df6f652e33e8d26cf1bfc6b0 /src | |
| parent | 8ebd67e4ee394cad9441a801f2022724ae7e07db (diff) | |
| download | rust-98b54fbd7afd55f40531bdb350a5adb870958111.tar.gz rust-98b54fbd7afd55f40531bdb350a5adb870958111.zip | |
Only call the closure parameter of Iterator::is_sorted_by_key once per item
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/iter/traits/iterator.rs | 6 | ||||
| -rw-r--r-- | src/libcore/slice/mod.rs | 4 |
2 files changed, 5 insertions, 5 deletions
diff --git a/src/libcore/iter/traits/iterator.rs b/src/libcore/iter/traits/iterator.rs index 30923c74145..b9a98236f18 100644 --- a/src/libcore/iter/traits/iterator.rs +++ b/src/libcore/iter/traits/iterator.rs @@ -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() } } diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index c6d44324ef5..0cd725d9b36 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -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) } } |
