From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id A78571AE03CD for ; Fri, 16 Sep 2016 01:11:18 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 583BFB5D9F8 for ; Fri, 16 Sep 2016 01:43:38 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 4ECCA18CC819 for ; Fri, 16 Sep 2016 01:43:38 +0900 (JST) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 580C1120504; Fri, 16 Sep 2016 01:43:36 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o10.shared.sendgrid.net (o10.shared.sendgrid.net [173.193.132.135]) by neon.ruby-lang.org (Postfix) with ESMTPS id 14DC61204D1 for ; Fri, 16 Sep 2016 01:43:32 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=+WFvWLQm/PTc7uke/vlXWHmJmas=; b=Ey5W5LPO/1ET+/+8BL dx43YDfYM7kmMSlsss9dXQdepxHVcn0RLEzr/uC3nm6V/6A1z/uw7u4nSRZcK65V 3DiVF2fxRa/ayPELj+geb5gpbhwxcuUzMN88OzBP5cpJmVPgeEEF8qzEfW1brB+C mAZWPkEuYpeUYCCImyc4slK38= Received: by filter0963p1mdw1.sendgrid.net with SMTP id filter0963p1mdw1.3120.57DACFA836 2016-09-15 16:43:20.480159291 +0000 UTC Received: from herokuapp.com (ec2-54-81-252-50.compute-1.amazonaws.com [54.81.252.50]) by ismtpd0006p1iad1.sendgrid.net (SG) with ESMTP id jtdLKg8RR3-oIzDG0HzFag Thu, 15 Sep 2016 16:43:20.410 +0000 (UTC) Date: Thu, 15 Sep 2016 16:43:20 +0000 From: cary@swoveland.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 52103 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11815 X-Redmine-Issue-Author: CaryInVictoria X-Redmine-Sender: CaryInVictoria 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS5wYnpzpg1+i0/lZP2CvrgVAmTs03rnKQoV2b YX47pkv+liJeoltVWMTjxFMEti17tLXbfehYOzKYgwZDhh8xtxModGF9jlyC73h1NDrzoEUBovECEx BdZo3qH0ymznQkgvsK8p41p4gdJqCLMt7GX8pvOqHK/e/AuvRwt1fdhzLQ== X-ML-Name: ruby-core X-Mail-Count: 77285 Subject: [ruby-core:77285] [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 Cary Swoveland. Description updated ---------------------------------------- Feature #11815: Proposal for method `Array#difference` https://bugs.ruby-lang.org/issues/11815#change-60519 * Author: Cary Swoveland * Status: Open * Priority: Normal * Assignee: ---------------------------------------- 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). Note: I initially wrote the method as follows: class Array def difference(other) dup.tap do |cpy| other.each do |e| ndx = cpy.index(e) cpy.delete_at(ndx) if ndx end end end end -- https://bugs.ruby-lang.org/