itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Proposed method: all_equal_value`

Open Lucretiel opened this issue 5 years ago • 1 comments

It'd be nice to have a version of all_equal that returns the value in the iterator where all the items in the iterator compare equal to that value. Proposed implementation:

/// Return the first item in the iterator if it compares equal to all the other
/// items in the iterator. Return `None` if the iterator is empty, or if it
/// contains any items that do not equal the first item.
fn all_equal_value(mut self) -> Option<Self::Item> {
    let first = self.next()?;

    if self.all(|item| first == item) {
        Some(first)
    } else {
        None
    }
}

Lucretiel avatar Nov 22 '20 01:11 Lucretiel

Sounds like a nice complement to https://github.com/rust-itertools/itertools/pull/241!

jswrenn avatar Nov 22 '20 22:11 jswrenn