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: Array#intersection method #2533

Merged
merged 1 commit into from
Oct 7, 2019
Merged

Conversation

Prajjwal
Copy link
Contributor

@Prajjwal Prajjwal commented Oct 7, 2019

Closes Feature #16155.

[ 'a', 'b', 'b', 'z' ].intersection([ 'a', 'b', 'c' ])           #=> [ 'a', 'b' ]
[ 'a', 'b', 'b', 'z' ].intersection([ 'a', 'b', 'c' ], [ 'b' ])  #=> [ 'b' ]
[ 'a' ].intersection                                             #=> [ 'a' ]

@nobu is this acceptable?

Implementation is currently based on Array#&, which is elegant but might end up allocating a whole bunch of arrays holding intermediate results. If needed I can implement Array#intersection so it only allocates the result array once, but then I would like to rewrite Array#& in terms of Array#intersection to keep things DRY.

Let me know what you think.

@nobu nobu merged commit c8542ab into ruby:master Oct 7, 2019
@Prajjwal Prajjwal deleted the add-array-intersection branch October 7, 2019 07:07
@connorshea
Copy link

This is awesome, thank you!

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

Successfully merging this pull request may close these issues.

3 participants