Skip to content

Commit ff155a3

Browse files
author
philipp
committed
Add all_equal
1 parent cdd0c1e commit ff155a3

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,29 @@ pub trait Itertools : Iterator {
732732
adaptors::unique(self)
733733
}
734734

735+
/// Check whether all elements compare equal.
736+
///
737+
/// Empty iterators are considered to have equal elements:
738+
///
739+
/// ```
740+
/// use itertools::Itertools;
741+
///
742+
/// let data = vec![1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5];
743+
/// assert!(!data.iter().all_equal());
744+
/// assert!(data[0..3].iter().all_equal());
745+
/// assert!(data[3..5].iter().all_equal());
746+
/// assert!(data[5..8].iter().all_equal());
747+
///
748+
/// let data : Option<usize> = None;
749+
/// assert!(data.into_iter().all_equal());
750+
/// ```
751+
fn all_equal(self) -> bool
752+
where Self: Sized,
753+
Self::Item: PartialEq,
754+
{
755+
self.dedup().nth(1).is_none()
756+
}
757+
735758
/// Return an iterator adaptor that filters out elements that have
736759
/// already been produced once during the iteration.
737760
///

tests/tests.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ fn dedup() {
185185
assert_eq!(&xs_d, &ys);
186186
}
187187

188+
#[test]
189+
fn all_equal() {
190+
assert!(!"AABBCCC".chars().all_equal());
191+
assert!("AAAAAAA".chars().all_equal());
192+
for (_key, sub) in &"AABBCCC".chars().group_by(|&x| x) {
193+
assert!(sub.all_equal());
194+
}
195+
}
196+
188197
#[test]
189198
fn unique_by() {
190199
let xs = ["aaa", "bbbbb", "aa", "ccc", "bbbb", "aaaaa", "cccc"];

0 commit comments

Comments
 (0)