ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:94855] [Ruby master Feature#16155] Add an Array#intersection method
       [not found] <redmine.issue-16155.20190909045243@ruby-lang.org>
@ 2019-09-09  4:52 ` connor.james.shea
  2019-09-09  9:40 ` [ruby-core:94857] " shevegen
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: connor.james.shea @ 2019-09-09  4:52 UTC (permalink / raw)
  To: ruby-core

Issue #16155 has been reported by connorshea (Connor Shea).

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

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:94857] [Ruby master Feature#16155] Add an Array#intersection method
       [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 ` shevegen
  2019-09-09 19:10 ` [ruby-core:94871] " connor.james.shea
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: shevegen @ 2019-09-09  9:40 UTC (permalink / raw)
  To: ruby-core

Issue #16155 has been updated by shevegen (Robert A. Heiler).


I sort of agree with your reasoning. A slight add-on, though,
on that part:

> This would essentially just be a more readable alias for Array#&

It is probably easier to search for it (e. g. a google-search for
.intersection) than for &. But if we compare relative merits then
we also have to remember an advantage for & being that it is
short/succinct.

It's an aside, though, because I agree with your reasoning anyway;
it would make sense to also have .intersection, IMO. :)

You could consider adding this to the upcoming developer meeting:

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

And ask matz whether he would be fine with the functionality 
itself; or, more importantly, the name for the method
(#intersection) since the functionality already exists.

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

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:94871] [Ruby master Feature#16155] Add an Array#intersection method
       [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 ` connor.james.shea
  2019-09-09 21:16 ` [ruby-core:94874] " matthew
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: connor.james.shea @ 2019-09-09 19:10 UTC (permalink / raw)
  To: ruby-core

Issue #16155 has been updated by connorshea (Connor Shea).


shevegen (Robert A. Heiler) wrote:
> You could consider adding this to the upcoming developer meeting:
> 
> https://bugs.ruby-lang.org/issues/16152
> 
> And ask matz whether he would be fine with the functionality 
> itself; or, more importantly, the name for the method
> (#intersection) since the functionality already exists.

Thanks, I will :)

After thinking about the proposal a bit more, one other thing I wanted to ask was about whether we should allow multiple arrays to be passed as arguments. `Array#difference` and `Array#union` both allow this, so it might make sense to have `Array#intersection` also take multiple arguments.

For example:

```ruby
# intersection with multiple arguments.
[ 'a', 'b', 'b', 'z' ].intersection([ 'a', 'b', 'c' ], [ 'b' ])  #=> [ 'b' ]
```

Should we keep `intersection` as a simple alias for `&` or have it allow multiple arrays, to match the `difference` and `union` methods?

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

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:94874] [Ruby master Feature#16155] Add an Array#intersection method
       [not found] <redmine.issue-16155.20190909045243@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-09-09 19:10 ` [ruby-core:94871] " connor.james.shea
@ 2019-09-09 21:16 ` matthew
  2019-09-19  8:31 ` [ruby-core:94985] " matz
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: matthew @ 2019-09-09 21:16 UTC (permalink / raw)
  To: ruby-core

Issue #16155 has been updated by phluid61 (Matthew Kerwin).


connorshea (Connor Shea) wrote:
> shevegen (Robert A. Heiler) wrote:
> 
> Should we keep `intersection` as a simple alias for `&` or have it allow multiple arrays, to match the `difference` and `union` methods?

The question should be: is it needed? 

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

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:94985] [Ruby master Feature#16155] Add an Array#intersection method
       [not found] <redmine.issue-16155.20190909045243@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2019-09-09 21:16 ` [ruby-core:94874] " matthew
@ 2019-09-19  8:31 ` matz
  2019-10-07  5:18 ` [ruby-core:95255] " sin
  2019-10-07  6:57 ` [ruby-core:95257] " sin
  6 siblings, 0 replies; 7+ messages in thread
From: matz @ 2019-09-19  8:31 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:95255] [Ruby master Feature#16155] Add an Array#intersection method
       [not found] <redmine.issue-16155.20190909045243@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2019-09-19  8:31 ` [ruby-core:94985] " matz
@ 2019-10-07  5:18 ` sin
  2019-10-07  6:57 ` [ruby-core:95257] " sin
  6 siblings, 0 replies; 7+ messages in thread
From: sin @ 2019-10-07  5:18 UTC (permalink / raw)
  To: ruby-core

Issue #16155 has been updated by prajjwal (Prajjwal Singh).


Hi all,

I've added an initial implementation of this based on `Array#&` on github (https://github.com/ruby/ruby/pull/2533).

One thing that stands out to me is the naming inconsistency between `rb_ary_and` and `rb_ary_intersection_multi`, and I think either could be changed to be consistent with the other. Thoughts?

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

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:95257] [Ruby master Feature#16155] Add an Array#intersection method
       [not found] <redmine.issue-16155.20190909045243@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2019-10-07  5:18 ` [ruby-core:95255] " sin
@ 2019-10-07  6:57 ` sin
  6 siblings, 0 replies; 7+ messages in thread
From: sin @ 2019-10-07  6:57 UTC (permalink / raw)
  To: ruby-core

Issue #16155 has been updated by prajjwal (Prajjwal Singh).


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.

```ruby
static VALUE
rb_ary_intersection_multi(int argc, VALUE *argv, VALUE ary)
{
    VALUE result = rb_ary_dup(ary);
    int i;

    for (i = 0; i < argc; i++) {
        result = rb_ary_and(result, argv[i]);
    }

    return result;
}
```

Let me know what you think.

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

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-10-07  6:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 ` [ruby-core:94985] " matz
2019-10-07  5:18 ` [ruby-core:95255] " sin
2019-10-07  6:57 ` [ruby-core:95257] " sin

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