diff options
| author | bors <bors@rust-lang.org> | 2019-09-21 04:21:25 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-09-21 04:21:25 +0000 |
| commit | 5349e69ae207c4d11245e75463c091eded3ad13c (patch) | |
| tree | edad552f0cdb10530194e80fc9010dac0dbda2b5 /src/libcore/tests | |
| parent | 97e58c0d32bcb8730f8246d25f3d2fa8092b450a (diff) | |
| parent | 72175915d6ae5abbc45cf2860a90508d2b4a38ea (diff) | |
| download | rust-5349e69ae207c4d11245e75463c091eded3ad13c.tar.gz rust-5349e69ae207c4d11245e75463c091eded3ad13c.zip | |
Auto merge of #64047 - timvermeulen:cmp_min_max_by, r=cuviper
Add `cmp::{min_by, min_by_key, max_by, max_by_key}`
This adds the following functions to `core::cmp`:
- `min_by`
- `min_by_key`
- `max_by`
- `max_by_key`
`min_by` and `max_by` are somewhat trivial to implement, but not entirely because `min_by` returns the first value in case the two are equal (and `max_by` the second). `min` and `max` can be implemented in terms of `min_by` and `max_by`, but not as easily the other way around.
To give an example of why I think these functions could be useful: the `Iterator::{min_by, min_by_key, max_by, max_by_key}` methods all currently hard-code the behavior mentioned above which is an ever so small duplication of logic. If we delegate them to `cmp::{min_by, max_by}` methods instead, we get the correct behavior for free. (edit: this is now included in the PR)
I added `min_by_key` / `max_by_key` for consistency's sake but I wouldn't mind removing them. I don't have a particular use case in mind for them, and `min_by` / `max_by` seem to be more useful.
Tracking issue: #64460
Diffstat (limited to 'src/libcore/tests')
| -rw-r--r-- | src/libcore/tests/cmp.rs | 24 | ||||
| -rw-r--r-- | src/libcore/tests/lib.rs | 1 |
2 files changed, 24 insertions, 1 deletions
diff --git a/src/libcore/tests/cmp.rs b/src/libcore/tests/cmp.rs index 4e624e5eb12..5e6778e222a 100644 --- a/src/libcore/tests/cmp.rs +++ b/src/libcore/tests/cmp.rs @@ -1,4 +1,4 @@ -use core::cmp::Ordering::{Less, Greater, Equal}; +use core::cmp::{self, Ordering::*}; #[test] fn test_int_totalord() { @@ -29,6 +29,28 @@ fn test_ord_max_min() { } #[test] +fn test_ord_min_max_by() { + let f = |x: &i32, y: &i32| x.abs().cmp(&y.abs()); + assert_eq!(cmp::min_by(1, -1, f), 1); + assert_eq!(cmp::min_by(1, -2, f), 1); + assert_eq!(cmp::min_by(2, -1, f), -1); + assert_eq!(cmp::max_by(1, -1, f), -1); + assert_eq!(cmp::max_by(1, -2, f), -2); + assert_eq!(cmp::max_by(2, -1, f), 2); +} + +#[test] +fn test_ord_min_max_by_key() { + let f = |x: &i32| x.abs(); + assert_eq!(cmp::min_by_key(1, -1, f), 1); + assert_eq!(cmp::min_by_key(1, -2, f), 1); + assert_eq!(cmp::min_by_key(2, -1, f), -1); + assert_eq!(cmp::max_by_key(1, -1, f), -1); + assert_eq!(cmp::max_by_key(1, -2, f), -2); + assert_eq!(cmp::max_by_key(2, -1, f), 2); +} + +#[test] fn test_ordering_reverse() { assert_eq!(Less.reverse(), Greater); assert_eq!(Equal.reverse(), Equal); diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index 050195cd2ef..35661356028 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -34,6 +34,7 @@ #![feature(iter_partition_in_place)] #![feature(iter_is_partitioned)] #![feature(iter_order_by)] +#![feature(cmp_min_max_by)] extern crate test; |
