Feature #15976
closedAdd Array#overlap? for whether the intersection of 2 arrays is non empty?
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.
Files
Updated by fcheung (Frederick Cheung) over 5 years ago
- File 2260.patch added
- 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?
Updated by fcheung (Frederick Cheung) over 5 years ago
- File deleted (
2260.patch)
Updated by shevegen (Robert A. Heiler) over 5 years 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).
Updated by fcheung (Frederick Cheung) over 5 years ago
- File deleted (
2260.patch) - File 2260.patch 2260.patch added
Updated by fcheung (Frederick Cheung) over 5 years ago
- Description updated (diff)
Updated by fcheung (Frederick Cheung) over 5 years 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
Updated by janosch-x (Janosch Müller) over 5 years ago
I think this should be called intersect?
for consistency with Set#intersect?
and SortedSet#intersect?
.
Updated by matz (Yukihiro Matsumoto) over 5 years ago
- Status changed from Open to Feedback
I don't think overlap?
is a good name. Besides that, Array#overlap?
creates an internal hash table anyway, so that it wouldn't solve the bottleneck. In this case, creating a hash table explicitly would perform better.
Matz.
Updated by matz (Yukihiro Matsumoto) over 5 years ago
- Is duplicate of Feature #15198: Array#intersect? added
Updated by nobu (Nobuyoshi Nakada) over 3 years ago
- Status changed from Feedback to Closed
Applied in changeset git|b6bb4623ebcf1335cf0a81d279cde24a9e97bcce.
NEWS for [Feature #15198] [ci skip]
Updated by hsbt (Hiroshi SHIBATA) about 1 year ago
- Related to Feature #19839: Need a method to check if two ranges overlap added