[#99115] [Ruby master Bug#17023] How to prevent String memory to be relocated in ruby-ffi — larskanis@...
Issue #17023 has been reported by larskanis (Lars Kanis).
22 messages
2020/07/10
[#99375] [Ruby master Feature#17055] Allow suppressing uninitialized instance variable and method redefined verbose mode warnings — merch-redmine@...
Issue #17055 has been reported by jeremyevans0 (Jeremy Evans).
29 messages
2020/07/28
[#101207] [Ruby master Feature#17055] Allow suppressing uninitialized instance variable and method redefined verbose mode warnings
— merch-redmine@...
2020/12/02
Issue #17055 has been updated by jeremyevans0 (Jeremy Evans).
[#101231] Re: [Ruby master Feature#17055] Allow suppressing uninitialized instance variable and method redefined verbose mode warnings
— Austin Ziegler <halostatue@...>
2020/12/03
What does this mean?
[ruby-core:99241] [Ruby master Feature#11815] Proposal for method `Array#difference`
From:
tyler@...
Date:
2020-07-20 19:58:01 UTC
List:
ruby-core #99241
Issue #11815 has been updated by TylerRick (Tyler Rick).
I would really like to see this included in Ruby.
Is there anything we can do to move this forward?
Do we just need to find a good name for it, now that `Array#difference` has been added with set semantics the same as `Array#-`?
----------------------------------------
Feature #11815: Proposal for method `Array#difference`
https://bugs.ruby-lang.org/issues/11815#change-86625
* Author: CaryInVictoria (Cary Swoveland)
* Status: Open
* Priority: Normal
----------------------------------------
I propose that a method `Array#difference` be added to the Ruby core. *(`Array#difference`, which appears to be an alias of `Array#-`, was added to Ruby 2.6. Accordingly, I would propose that this method be named `remove`, but I will not make any changes here.)* It is similar to [Array#-](http://ruby-doc.org/core-2.2.0/Array.html#method-i-2D) but for each element of the (array) argument it removes only one matching element from the receiver. For example:
a = [1,2,3,4,3,2,2,4]
b = [2,3,4,4,4]
a - b #=> [1]
c = a.difference b #=> [1, 3, 2, 2]
As you see, `a` contains three `2`'s and `b` contains `1`, so the first `2` in `a` has been removed from `a` in constructing `c`. When `b` contains as least as many instances of an element as does `a`, `c` contains no instances of that element.
It could be implemented as follows:
class Array
def difference(other)
h = other.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }
reject { |e| h[e] > 0 && h[e] -= 1 }
end
end
Here are a few examples of its use:
*Determine if two arrays of the same size contain the same elements*
a = [2,1,4,2,5,3,3,1]
b = [3,4,1,1,2,3,5,2]
a.difference(b).empty?
#=> true
*Identify an array's unique elements*
a = [1,3,2,4,3,4]
u = a.uniq #=> [1, 2, 3, 4]
u - a.difference(u) #=> [1, 2]
*Identify a maximal number of 1-1 matches between the elements of two arrays and return an array of all elements from both arrays that were not matched*
a = [1, 2, 4, 2, 1, 7, 4, 2, 9]
b = [4, 7, 3, 2, 2, 7]
a.difference(b).concat(b.difference(a))
#=> [1, 1, 4, 2, 9, 3, 7]
To remove elements from `a` starting at the end (rather the beginning) of `a`:
a = [1,2,3,4,3,2,2,4]
b = [2,3,4,4,4]
a.reverse.difference(b).reverse #=> [1,2,3,2]
`Array#difference!` could be defined in the obvious way.
More information is in my answer to [this SO question](http://stackoverflow.com/questions/24987054/how-to-select-unique-elements).
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>