Project

General

Profile

Feature #15976

Add Array#overlap? for whether the intersection of 2 arrays is non empty?

Added by fcheung (Frederick Cheung) 19 days ago. Updated 19 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:93487]

Description

This is semantically equivalent to (ary1 & ary2).any?, but more efficient and makes the intent more obvious.

For example bundler checks whether the list of requested groups and the list of groups for a dependency has any overlap - it doesn't care what the overlap is, as long as it is non empty

In my personal projects we've found this to be a bottleneck when the arrays are large and where intersections are likely - !(ary1 & ary2).empty? keeps searching for all of the intersection even after we've found the first one, & creates extra garbage because of the intermediate array.

See https://github.com/ruby/ruby/pull/2260


Files

2260.patch (7.9 KB) 2260.patch fcheung (Frederick Cheung), 07/02/2019 09:02 PM

History

#1

Updated by fcheung (Frederick Cheung) 19 days ago

  • Subject changed from Add Array#overlaps? for whether the intersection of 2 arrays is non empty? to Add Array#overlap? for whether the intersection of 2 arrays is non empty?
  • File 2260.patch added
#2

Updated by fcheung (Frederick Cheung) 19 days ago

  • File deleted (2260.patch)

Updated by shevegen (Robert A. Heiler) 19 days ago

To me the name of the method appears to make sense and I am slightly in
favour of the suggestion. I can not say how useful this method would be
in general, though, mostly because I think I needed to do something
such as (array1 & array2).any? perhaps only once or twice in a long
time in my own code; but even then I think this might be a useful
proposal and a name that makes sense (to me at the least).

#4

Updated by fcheung (Frederick Cheung) 19 days ago

#5

Updated by fcheung (Frederick Cheung) 19 days ago

  • Description updated (diff)

Updated by fcheung (Frederick Cheung) 19 days ago

shevegen (Robert A. Heiler) wrote:

To me the name of the method appears to make sense and I am slightly in
favour of the suggestion. I can not say how useful this method would be
in general, though, mostly because I think I needed to do something
such as (array1 & array2).any? perhaps only once or twice in a long
time in my own code; but even then I think this might be a useful
proposal and a name that makes sense (to me at the least).

Thanks. Matz suggested today that the name was not so clear to him. An alternative that springs to mind would be intersect?, but I am obviously open to any suggestions

Also available in: Atom PDF