From: "sawa (Tsuyoshi Sawada)" Date: 2021-12-08T16:38:29+00:00 Subject: [ruby-core:106552] [Ruby master Feature#18395] Introduce Array#subtract! for performance Issue #18395 has been updated by sawa (Tsuyoshi Sawada). schneems (Richard Schneeman) wrote in #note-2: > > We have Array#delete already, the only difference from your proposal (other than the receiver) being that it does not accept multiple arguments. I think a better proposal would be to let Array#delete accept multiple arguments. > > In order to preserve the original behavior of returning the deleted elements, you would need to track them which would necessitate an additional array allocation so it would be no faster than `Array#-=`. The original behavior of `Array#delete` is to return the element that last matched (= was deleted). This behavior should be retained (Please see my code example in the previous comment). There is no need to return **all** deleted elements, and there is no need to allocate an additional array. > > It is a practice in Ruby for methods that destructively remove something from the receiver to return the removed value rather than the modified receiver. > > That is very interesting. I never knew there was a pattern to the return results of bang methods. The differences have always been confusing to me, but I had never considered that they might behave differently if they are destructive. If this is the convention is it documented somewhere, could we document it somewhere? Where would be a good place to document this convention? There is a common misunderstanding in assimilating destructive methods with bang methods. As Matz has repeatedly said, there is no connection between them. My comment is concerned with destructive methods, not bang methods. I never mentioned bang methods in the previous comment. For the practice I mentioned, please see the documents of, for example, `Array#shift`, `Array#pop`, and `Array#delete`. > I appreciate your time and attention. We can close the ticket and pull request unless someone disagrees. I am thankful for your detailed response. Although I am against the idea of introducing a new method, I am not against having a way to destructively remove multiple elements from an array. ---------------------------------------- Feature #18395: Introduce Array#subtract! for performance https://bugs.ruby-lang.org/issues/18395#change-95216 * Author: schneems (Richard Schneeman) * Status: Open * Priority: Normal ---------------------------------------- PR: https://github.com/ruby/ruby/pull/5110#issuecomment-984345309 It is common to use `-=` to modify an array to remove elements. Also, there is `Array#difference` which accepts multiple arguments. Both of these methods allocate an intermediate array when the original array could be re-used. I am proposing we add an API onto Array that allows the programmer to perform a subtraction/difference operation and mutate the original array. I am proposing `Array#subtract!`. Reasons why I did not choose `Array#difference!` are discussed in the comments of the PR. I'm also happy to discuss alternative names. ``` ary = [0, 1, 1, 2, 1, 1, 3, 1, 1] ary.subtract!([1]) #=> [0, 2, 3] ary #=> [0, 2, 3] ary = [0, 1, 2, 3] ary.subtract!([3, 0], [1, 3]) #=> [2] ary #=> [2] ``` -- https://bugs.ruby-lang.org/ Unsubscribe: