ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: matz@ruby.or.jp
To: ruby-core@ruby-lang.org
Subject: [ruby-core:94985] [Ruby master Feature#16155] Add an Array#intersection method
Date: Thu, 19 Sep 2019 08:31:41 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-81604.20190919083141.7383bc0f112e6fd8@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16155.20190909045243@ruby-lang.org

Issue #16155 has been updated by matz (Yukihiro Matsumoto).


Accepted. PR welcome.

Matz.

----------------------------------------
Feature #16155: Add an Array#intersection method
https://bugs.ruby-lang.org/issues/16155#change-81604

* Author: connorshea (Connor Shea)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
`Array#union` and `Array#difference` were added in Ruby 2.6 ([see this bug](https://bugs.ruby-lang.org/issues/14097)), but an equivalent for `&` (intersection) was not.

I'd like to propose `Array#intersection`. This would essentially just be a more readable alias for `Array#&`, in the same way that `Array#|` and `Array#-` have `Array#union` and `Array#difference`.

I think it'd make sense for Ruby to have a more readable name for this method :)

Current syntax:

``` ruby
[ 1, 1, 3, 5 ] & [ 3, 2, 1 ]                 #=> [ 1, 3 ]
[ 'a', 'b', 'b', 'z' ] & [ 'a', 'b', 'c' ]   #=> [ 'a', 'b' ]
```

What I'd like to see added:

```ruby
[ 1, 1, 3, 5 ].intersection([ 3, 2, 1 ])                #=> [ 1, 3 ]
[ 'a', 'b', 'b', 'z' ].intersection([ 'a', 'b', 'c' ])  #=> [ 'a', 'b' ]
```

mame asks about `intersection` [in this comment](https://bugs.ruby-lang.org/issues/14097#note-26) on the `union`/`difference` bug, but as far as I can tell it was never addressed.

[`Set#intersection`](http://ruby-doc.org/stdlib-2.6.2/libdoc/set/rdoc/Set.html#method-i-intersection) already exists and is an alias for `Set#&`, so there's precedent for such a method to exist.

Thanks for Ruby, I enjoy using it a lot! :)

Related links:

- [PR for `union`](https://github.com/ruby/ruby/pull/1747)
- [PR for `difference`](https://github.com/ruby/ruby/pull/1758)
- [Bug for adding `union` and `difference` methods](https://bugs.ruby-lang.org/issues/14097)



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2019-09-19  8:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16155.20190909045243@ruby-lang.org>
2019-09-09  4:52 ` [ruby-core:94855] [Ruby master Feature#16155] Add an Array#intersection method connor.james.shea
2019-09-09  9:40 ` [ruby-core:94857] " shevegen
2019-09-09 19:10 ` [ruby-core:94871] " connor.james.shea
2019-09-09 21:16 ` [ruby-core:94874] " matthew
2019-09-19  8:31 ` matz [this message]
2019-10-07  5:18 ` [ruby-core:95255] " sin
2019-10-07  6:57 ` [ruby-core:95257] " sin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-81604.20190919083141.7383bc0f112e6fd8@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).