-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add is_sorted
to Iterator
and [T]
#55045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8dea0d0
02477f6
ce47dde
ccc027e
67729b4
54f1124
b4766f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# `is_sorted` | ||
|
||
The tracking issue for this feature is: [#53485] | ||
|
||
[#53485]: https://github.com/rust-lang/rust/issues/53485 | ||
|
||
------------------------ | ||
|
||
Add the methods `is_sorted`, `is_sorted_by` and `is_sorted_by_key` to `[T]`; | ||
add the methods `is_sorted`, `is_sorted_by` and `is_sorted_by_key` to | ||
`Iterator`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2235,3 +2235,16 @@ fn test_monad_laws_associativity() { | |
assert_eq!((0..10).flat_map(f).flat_map(g).sum::<usize>(), | ||
(0..10).flat_map(|x| f(x).flat_map(g)).sum::<usize>()); | ||
} | ||
|
||
#[test] | ||
fn test_is_sorted() { | ||
assert!([1, 2, 2, 9].iter().is_sorted()); | ||
assert!(![1, 3, 2].iter().is_sorted()); | ||
assert!([0].iter().is_sorted()); | ||
assert!(std::iter::empty::<i32>().is_sorted()); | ||
assert!(![0.0, 1.0, std::f32::NAN].iter().is_sorted()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @LukasKalbertodt shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a test case taken from the RFC for a heads up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kleimkuhler I think this is correct. These are some other tests from the discussion: [nan, nan, nan]
[1.0, nan, 2.0]
[2.0, nan, 1.0]
[2.0, nan, 1.0, 7.0]
[2.0, nan, 1.0, 0.0]
[-nan, -1.0, 0.0, 1.0, nan]
[nan, -nan, -1.0, 0.0, 1.0]
[1.0, nan, -nan, -1.0, 0.0]
[0.0, 1.0, nan, -nan, -1.0]
[-1.0, 0.0, 1.0, nan, -nan] IIRC @ExpHP also suggested: // using `Suffix::from`
["a", "aa", "aaa", "b", "bb", "bbb"]
[ "", "a", "aa", "", "b", "bb"]
// using set / subset:
[set![3], set![2]]
[set![2], set![3]]
[set![2], set![3], set![2, 3]]
[set![2], set![2, 3], set![3]]
[set![2], set![2, 3], set![5]]
[set![2, 3], set![5], set![2]] See this comment (rust-lang/rfcs#2351 (comment)) and the associated gist with the test source code (https://gist.github.com/ExpHP/c23b51f0a9f5f94f2375c93137299604). |
||
assert!([-2, -1, 0, 3].iter().is_sorted()); | ||
assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs())); | ||
assert!(!["c", "bb", "aaa"].iter().is_sorted()); | ||
assert!(["c", "bb", "aaa"].iter().is_sorted_by_key(|s| s.len())); | ||
} | ||
kleimkuhler marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1317,3 +1317,18 @@ fn test_copy_within_panics_src_inverted() { | |
// 2 is greater than 1, so this range is invalid. | ||
bytes.copy_within(2..1, 0); | ||
} | ||
|
||
#[test] | ||
fn test_is_sorted() { | ||
let empty: [i32; 0] = []; | ||
|
||
assert!([1, 2, 2, 9].is_sorted()); | ||
assert!(![1, 3, 2].is_sorted()); | ||
assert!([0].is_sorted()); | ||
assert!(empty.is_sorted()); | ||
assert!(![0.0, 1.0, std::f32::NAN].is_sorted()); | ||
assert!([-2, -1, 0, 3].is_sorted()); | ||
assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs())); | ||
assert!(!["c", "bb", "aaa"].is_sorted()); | ||
assert!(["c", "bb", "aaa"].is_sorted_by_key(|s| s.len())); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to add many optimizations in the future, so we should try to already add a comprehensive test suite, e.g., see https://github.com/gnzlbg/is_sorted/tree/master/tests for inspiration, but @LukasKalbertodt RFC and associated discussion also covered many interesting cases. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
fn main() { | ||
// Assert `Iterator` methods are feature gated | ||
assert!([1, 2, 2, 9].iter().is_sorted()); | ||
//~^ ERROR: use of unstable library feature 'is_sorted': new API | ||
assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs())); | ||
//~^ ERROR: use of unstable library feature 'is_sorted': new API | ||
|
||
// Assert `[T]` methods are feature gated | ||
assert!([1, 2, 2, 9].is_sorted()); | ||
//~^ ERROR: use of unstable library feature 'is_sorted': new API | ||
assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs())); | ||
//~^ ERROR: use of unstable library feature 'is_sorted': new API | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485) | ||
--> $DIR/feature-gate-is_sorted.rs:3:33 | ||
| | ||
LL | assert!([1, 2, 2, 9].iter().is_sorted()); | ||
| ^^^^^^^^^ | ||
| | ||
= help: add #![feature(is_sorted)] to the crate attributes to enable | ||
|
||
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485) | ||
--> $DIR/feature-gate-is_sorted.rs:5:39 | ||
| | ||
LL | assert!(![-2i32, -1, 0, 3].iter().is_sorted_by_key(|n| n.abs())); | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(is_sorted)] to the crate attributes to enable | ||
|
||
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485) | ||
--> $DIR/feature-gate-is_sorted.rs:9:26 | ||
| | ||
LL | assert!([1, 2, 2, 9].is_sorted()); | ||
| ^^^^^^^^^ | ||
| | ||
= help: add #![feature(is_sorted)] to the crate attributes to enable | ||
|
||
error[E0658]: use of unstable library feature 'is_sorted': new API (see issue #53485) | ||
--> $DIR/feature-gate-is_sorted.rs:11:32 | ||
| | ||
LL | assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs())); | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(is_sorted)] to the crate attributes to enable | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
Uh oh!
There was an error while loading. Please reload this page.