[ruby-core:95247] [Ruby master Feature#16146] Array .difference allow custom comparison

From: jonathan@...
Date: 2019-10-06 16:41:02 UTC
List: ruby-core #95247
Issue #16146 has been updated by jonathanhefner (Jonathan Hefner).


Dan0042 (Daniel DeLorme) wrote:
> What you're asking for is a O(n operation. I'm not sure it's a good idea to make that so easy and transparent as a core method.

Although the given example specified a two-argument block, the original post references `uniq`, which accepts a one-argument block.  Using a one-argument block can result in an O(n) implementation.  Here is a different example:

```ruby
Dependency = Struct.new(:name, :version)

deps1 = [Dependency.new("foo", "1.1"), Dependency.new("bar", "2.0"), Dependency.new("qux", "1.0")]

deps2 = [Dependency.new("foo", "1.0"), Dependency.new("bar", "1.0")] 

deps1.difference(deps2, &:name)
# == [Dependency.new("qux", "1.0")]

deps1.difference(deps2){|dep| [dep.name, dep.version[/^\d+/]] }
# == [Dependency.new("bar", "2.0"), Dependency.new("qux", "1.0")]
```



----------------------------------------
Feature #16146: Array .difference allow custom comparison
https://bugs.ruby-lang.org/issues/16146#change-81922

* Author: ngomez (Nancy Gomez)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Hello!

I wanted to know if there's any plan to implement the ability to define comparison between individual items in arrays.

Here is where I originally asked the question on Stack Overflow: https://stackoverflow.com/questions/57316775/is-there-any-way-to-specify-how-to-compare-of-array-of-objects-for-difference-f

But specifically, I noticed nearly all the existing Array functions allow us to define ways for comparison, such as #uniq but #difference does not :( This is quite unfortunate but I'm hoping it's just because this function was introduced in 2.6.0, and hopefully this feature can be implemented?

Thanks for any and all info :)



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

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next