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

add callback to uniq method of collection #847

Merged
merged 1 commit into from Sep 25, 2015

Conversation

CandyAngel
Copy link
Contributor

This change adds a callback to the uniq method in Mojo::Collection, allowing users to define what constitutes a unique element. This permits collections of hashes and arrays to be de-duplicated, rather than just elements which stringify reasonably (strings, numbers, overloaded objects).

my %seen;
return $_[0]->new(grep { !$seen{$_}++ } @{$_[0]});
return $self->new(map { (!$seen{ $cb->($_) }++) ? $_ : () } @$self)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use grep instead of map?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a (bad) habit, I use map far more than grep, so I use it by default. Fixed.

@jhthorsen
Copy link
Member

I'm neutral, leaning toward +1.

@zoffixznet
Copy link
Contributor

I'd suggest using a better example in the docs. I like the method call example in List::UtilsBy::uniq_by

@kraih
Copy link
Member

kraih commented Sep 25, 2015

👍 from me.

kraih added a commit that referenced this pull request Sep 25, 2015
add callback to uniq method of collection
@kraih kraih merged commit eaec9b5 into mojolicious:master Sep 25, 2015
@CandyAngel CandyAngel deleted the collection_uniq_callback branch October 17, 2015 00:32
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.

None yet

4 participants