From: "mame (Yusuke Endoh)" Date: 2021-12-08T15:26:18+00:00 Subject: [ruby-core:106551] [Ruby master Feature#18395] Introduce Array#subtract! for performance Issue #18395 has been updated by mame (Yusuke Endoh). Description updated It would be very helpful if you could write a code example in the description to help us understand your proposal at a glance. I copied (and a bit tweaked) it from the rdoc you write in PR. Feel free to change the example if you don't like it. ---------------------------------------- Feature #18395: Introduce Array#subtract! for performance https://bugs.ruby-lang.org/issues/18395#change-95215 * 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: