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

Something like all_same? #175

Closed
phimuemue opened this issue Dec 8, 2016 · 8 comments
Closed

Something like all_same? #175

phimuemue opened this issue Dec 8, 2016 · 8 comments

Comments

@phimuemue
Copy link
Member

Do you think it would be helpful to have something that determines whether all elements of an iterator are the same?

@bluss
Copy link
Member

bluss commented Dec 8, 2016

Hm first approach, is there a neat way to do that already?

There is:

iter.tuple_windows().all(|(x, y)| x == y)
// but this requires cloning elements (irrelevant if they are integers or references)

There is (weird one):

all_equal == iter.dedup().nth(1).is_none() // only zero or one unique elements

I haven't had the need for this operation, I can see that current itertools doesn't cover it very well. .dedup() is pretty close in fact, but it's not intuitive.

@phimuemue
Copy link
Member Author

Thank you for your reply. Actually, I found out that in my concrete case, there is another - more natural - way avoiding all_same, so it is not very urgent for me.

However, I still think that it might be useful, especially within assert! (imagine e.g. you're using group_by and need a representative from each group).

I do not know how the dedup actually works, so I can not tell whether dedup().nth(1) solution does more work than necessary.

@bluss
Copy link
Member

bluss commented Dec 10, 2016

dedup is lazy and only looks at adjacent elements so it should be close to doing the minimal work required (this is without me cross checking with the implementation).

@bluss
Copy link
Member

bluss commented Dec 22, 2016

This method needs a better name. With .all_equal() I think it would be a go. I'd consider implementing it using dedup.

@phimuemue
Copy link
Member Author

I think you are correct that a dedup-based solution is convenient.

However, I saw that dedup internally remembers the last seen element (it works via CoalesceCore which has Ok(joined) => last = joined, i.e. assigning last in each internal step). I think remembering last might be more work than necessary, or am I mistaken?

@bluss
Copy link
Member

bluss commented Apr 10, 2017

I think it needs to remember last. Otherwise, why not prove it with an implementation 😄

@phimuemue
Copy link
Member Author

Should we close this?

@bluss
Copy link
Member

bluss commented Apr 14, 2017

Sure. Fixed in #196

@bluss bluss closed this as completed Apr 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants