Skip to content

add is_sorted, is_sorted_by, is_sorted_by_key #261

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

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,77 @@ pub trait Itertools : Iterator {
peeking_take_while::peeking_take_while(self, accept)
}

/// Returns `true` if the elements of the iterator are sorted
/// in increasing order.
///
/// ```
/// # use itertools::Itertools;
/// let v = vec![0, 1, 2 , 3];
/// assert!(v.iter().is_sorted());
///
/// let v = vec![0, 1, 2 , -1];
/// assert!(!v.iter().is_sorted());
/// ```
fn is_sorted(self) -> bool
where Self: Sized,
Self::Item: Ord,
{
self.is_sorted_by(|a, b| Ord::cmp(&a, &b))
}

/// Returns `true` if the elements of the iterator
/// are sorted according to the `comparison` function.
///
/// ```
/// # use itertools::Itertools;
/// # use std::cmp::Ordering;
/// // Is an iterator sorted in decreasing order?
/// fn decr<T: Ord>(a: &T, b: &T) -> Ordering {
/// a.cmp(b).reverse()
/// }
///
/// let v = vec![3, 2, 1 , 0];
/// assert!(v.iter().is_sorted_by(decr));
///
/// let v = vec![3, 2, 1 , 4];
/// assert!(!v.iter().is_sorted_by(decr));
/// ```
fn is_sorted_by<F>(mut self, mut compare: F) -> bool
where Self: Sized,
Self::Item: Ord,
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
{
let first = self.next();
if let Some(mut first) = first {
while let Some(second) = self.next() {
if compare(&first, &second) == Ordering::Greater {
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a while-let with an early return, should this be &mut self instead, like all and any?

(nit: can it be written in terms of all, until try_fold is stable?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give these a try!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottmcm these should be fixed now

}
first = second;
}
}
true
}

/// Returns `true` if the elements of the iterator
/// are sorted according to the `key` extraction function.
///
/// ```
/// # use itertools::Itertools;
/// let v = vec![0_i32, -1, 2, -3];
/// assert!(v.iter().is_sorted_by_key(|v| v.abs()));
///
/// let v = vec![0_i32, -1, 2, 0];
/// assert!(!v.iter().is_sorted_by_key(|v| v.abs()));
/// ```
fn is_sorted_by_key<F, B>(self, mut key: F) -> bool
where Self: Sized,
B: Ord,
F: FnMut(&Self::Item) -> B,
{
self.map(|v| key(&v)).is_sorted()
}

/// Return an iterator adaptor that borrows from a `Clone`-able iterator
/// to only pick off elements while the predicate `accept` returns `true`.
///
Expand Down