Skip to content
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

Proposed method: all_equal_value` #495

Closed
Lucretiel opened this issue Nov 22, 2020 · 1 comment · Fixed by #648
Closed

Proposed method: all_equal_value` #495

Lucretiel opened this issue Nov 22, 2020 · 1 comment · Fixed by #648

Comments

@Lucretiel
Copy link

Lucretiel commented Nov 22, 2020

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
    }
}
@jswrenn
Copy link
Member

jswrenn commented Nov 22, 2020

Sounds like a nice complement to #241!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants