Skip to content

.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

Merged
merged 1 commit into from
Jun 17, 2015

Conversation

stepancheg
Copy link
Contributor

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().

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()`.
@stepancheg
Copy link
Contributor Author

Updated PR with explicit test.

@bluss
Copy link
Member

bluss commented Jun 17, 2015

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()
Copy link
Member

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.

Copy link
Contributor Author

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.

@stepancheg
Copy link
Contributor Author

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.

@bluss
Copy link
Member

bluss commented Jun 17, 2015

You managed to hit the requirements very well without having seen that PR then!

Thanks for the contribution

@bluss
Copy link
Member

bluss commented Jun 17, 2015

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.

bluss added a commit that referenced this pull request Jun 17, 2015
.unique() and .unique_by(..) operations
@bluss bluss merged commit 3c1cd9a into rust-itertools:master Jun 17, 2015
@bluss
Copy link
Member

bluss commented Jun 17, 2015

Released in 0.3.16.

@Wilfred
Copy link

Wilfred commented Jun 17, 2015

Marvellous! Thanks for doing this :)

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 this pull request may close these issues.

3 participants