From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.6 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.1 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 284851F453 for ; Sat, 29 Sep 2018 14:14:36 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 3B5A2120A68; Sat, 29 Sep 2018 23:14:33 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id BEBA3120A47 for ; Sat, 29 Sep 2018 23:14:30 +0900 (JST) Received: by filter0131p3las1.sendgrid.net with SMTP id filter0131p3las1-31688-5BAF88C1-23 2018-09-29 14:14:25.810840225 +0000 UTC m=+342221.073774240 Received: from herokuapp.com (ec2-54-224-81-232.compute-1.amazonaws.com [54.224.81.232]) by ismtpd0009p1iad2.sendgrid.net (SG) with ESMTP id L9g_SOfKSBWxuwZU9awg8A Sat, 29 Sep 2018 14:14:25.771 +0000 (UTC) Date: Sat, 29 Sep 2018 14:14:26 +0000 (UTC) From: florian.ebeling@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 64603 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11815 X-Redmine-Issue-Author: CaryInVictoria X-Redmine-Sender: febeling X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS7LlqNvKmof69LxpBgyoZx/1HU2RZtcnMCypq wqnrxy1Wpw95n1120G4XAi+BXsYj3f9alqERe4IB4YH4pNA7H8kmV8w1RnTEJ4x8fJgWXqXlA6i97o lcrjscXIXX9OLLSr/7W/dkF15X/Uu3FRLvr3IJNmpdSTxaUMxHhenuHopQ== X-ML-Name: ruby-core X-Mail-Count: 89215 Subject: [ruby-core:89215] [Ruby trunk Feature#11815] Proposal for method `Array#difference` X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #11815 has been updated by febeling (Florian Ebeling). I could use this method for fixing this bug [1] in ActiveRecord. To me it looks like a valuable addition. It's about replacing collection associations which can be partially persisted, with persisted records being handled differently as an optimization. 1 https://github.com/rails/rails/issues/33942 ---------------------------------------- Feature #11815: Proposal for method `Array#difference` https://bugs.ruby-lang.org/issues/11815#change-74242 * Author: CaryInVictoria (Cary Swoveland) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- I propose that a method `Array#difference` be added to the Ruby core. 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/