-
Notifications
You must be signed in to change notification settings - Fork 324
.unique() and .unique_by(..) operations #37
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
Conversation
Functions make iterators that return unique elements, storing visited elements in a hash set. `.unique_by(..)` uses supplied function to make keys, and `.unique()` uses `.clone()`.
Updated PR with explicit test. |
Thanks! You're taking over the task from PR #30, I guess. I'll leave some comments inline. Style wording, etc can all be fixed by me later if you don't want to, but incorrect size hint, that won't pass :-) |
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.iter.size_hint() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use the upper bound of the adapted iterator, and use a minimum of 0. Unless the hashset is empty, then use either 0 or 1 as lower bound depending on the adapted itertator.
Simply put -- our full information about how many elements remain, keeping in mind that they may all be duplicates or all unique.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I made a mistake.
I think 0 is fine for lower bound. I think in 99% cases size_hint
is used on fresh new iterator, so hash-set would be empty.
Changed PR according to your comments. I didn't see #30 before you pointed to it. It's funny I made the same request independently. |
You managed to hit the requirements very well without having seen that PR then! Thanks for the contribution |
A possible follow up might be: A method to extract the hash set. You might as well allow it to be used after it was constructed? Not sure. |
.unique() and .unique_by(..) operations
Released in 0.3.16. |
Marvellous! Thanks for doing this :) |
Functions make iterators that return unique elements, storing visited
elements in a hash set.
.unique_by(..)
uses supplied function to make keys, and.unique()
uses
.clone()
.