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 2812119E005C for ; Fri, 18 Dec 2015 18:45:40 +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 700B1B5D828 for ; Fri, 18 Dec 2015 19:17:47 +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 1C2B118CC7D0 for ; Fri, 18 Dec 2015 19:17:48 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 311CB1205AA; Fri, 18 Dec 2015 19:17:48 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o2.heroku.sendgrid.net (o2.heroku.sendgrid.net [67.228.50.55]) by neon.ruby-lang.org (Postfix) with ESMTPS id C7C22120595 for ; Fri, 18 Dec 2015 19:17:44 +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=D17MgQysJgPMrkPU3cdeyfOTny0=; b=CMQJsZYV97drfCwT+E /qfiEEQxKYpenG01MKEWD9SeckkqdaRpvjYEQcx1V6KPYv72g86ceOjSnx+u1pM4 c4Z5UgoW+slxE2RYN4uaQM8EH4iIdU1ECnbcej7JxT8nC5ZmLqLIXO269ngR2CmJ 83BoFfboGQTV3GnxNF5llpEx4= Received: by filter0532p1mdw1.sendgrid.net with SMTP id filter0532p1mdw1.12462.5673DD4321 2015-12-18 10:17:39.365192043 +0000 UTC Received: from herokuapp.com (ec2-54-242-87-41.compute-1.amazonaws.com [54.242.87.41]) by ismtpd0003p1iad1.sendgrid.net (SG) with ESMTP id 3GuvkUwIR066677_eb_Szg Fri, 18 Dec 2015 10:17:39.184 +0000 (UTC) Date: Fri, 18 Dec 2015 10:17:39 +0000 From: duerst@it.aoyama.ac.jp To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Redmine-MailingListIntegration-Message-Ids: 46958 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11815 X-Redmine-Issue-Author: CaryInVictoria X-Redmine-Sender: duerst 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS4xjzusta6YohY46klyY3cNiF7lu77e7IXWBc cgTXz5aHVGmOHGGwSIVz5h8REVwAbUpj7Sw3JDMK4HLuZf53dq/rilVIGp+ckqvNQsnbL4c15X+9ks y/vA/uTUEHXkko62FSgSuQiwXWp67YmJ+iweXQ4ee7Eyuh6mN1v70SgrRQ== X-ML-Name: ruby-core X-Mail-Count: 72363 Subject: [ruby-core:72363] [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: , Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #11815 has been updated by Martin D=C3=BCrst. Ryan Beltran wrote: > I think I have a pretty good example. I'm implementing a function in Ruby= that finds triples in an array for use in a pokerbot (recognizes if a hand= is a triple). I already defined a function to check for doubles (which is = relatively trivial to implement by comparing the original array to array.un= iq), but triples are a little bit harder. There are several ways I could im= plement checking for triples, but a concise and efficient option would be t= o simply call: >=20 > `getDoubles(array.difference(array.uniq))`=20 >=20 > ~~~ ruby > doubles =3D array.difference(array.uniq) > triples =3D doubles.difference(doubles) > fours =3D triples.difference(triples) > ~~~ An even more straightforward way is to use group_by: ~~~ ruby n_tuples =3D array.group_by {|e| e}.values.group_by(&:length) doubles =3D n_tuples[2] triples =3D n_tuples[3] fours =3D n_tuples[4] # and so on ~~~ ruby We need group_by two times because the first one groups items with the same= value, and the second organizes these by numbers. As an example, if we sta= rt with [1,2,2,3,3,3,4,4,4,4,5,5,5,7,8,8] then after the first group_by, we= have {1=3D>[1], 2=3D>[2, 2], 3=3D>[3, 3, 3], 4=3D>[4, 4, 4, 4], 5=3D>[5, 5, 5], = 7=3D>[7], 8=3D>[8, 8]}. Of this, we only need the values: [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5= , 5, 5], [7], [8, 8]]. Then we group by length and get: {1=3D>[[1], [7]], 2=3D>[[2, 2], [8, 8]], 3=3D>[[3, 3, 3], [5, 5, 5]], 4=3D>= [[4, 4, 4, 4]]} To get an unique value (i.e. 3 instead of [3, 3, 3]), just use .map(&:first= ). ---------------------------------------- Feature #11815: Proposal for method `Array#difference` https://bugs.ruby-lang.org/issues/11815#change-55656 * Author: Cary Swoveland * Status: Open * Priority: Normal * Assignee:=20 ---------------------------------------- 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 =3D [1,2,3,4,3,2,2,4] b =3D [2,3,4,4,4] a - b #=3D> [1] c =3D a.difference b #=3D> [1, 3, 2, 2]=20 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 instanc= es of that element.=20 It could be implemented as follows: class Array def difference(other) dup.tap do |cpy| other.each do |e| ndx =3D cpy.index(e) cpy.delete_at(ndx) if ndx end end end end Here are a few examples of its use: *Identify an array's unique elements* a =3D [1,3,2,4,3,4] u =3D a.uniq #=3D> [1, 2, 3, 4] u - a.difference(u) #=3D> [1, 2] *Determine if two words of the same size are anagrams of each other* w1, w2 =3D "stop", "pots" w1.chars.difference(w2.chars).empty? #=3D> true *Identify a maximal number of 1-1 matches between the elements of two array= s and return an array of all elements from both arrays that were not matche= d* a =3D [1, 2, 4, 2, 1, 7, 4, 2, 9]=20 b =3D [4, 7, 3, 2, 2, 7]=20 a.difference(b).concat(b.difference(a)) #=3D> [1, 1, 4, 2, 9, 3, 7]=20 =20=20 To remove elements from `a` starting at the end (rather the beginning) of `= a`: a =3D [1,2,3,4,3,2,2,4] b =3D [2,3,4,4,4] a.reverse.difference(b).reverse #=3D> [1,2,3,2] `Array#difference!` could be defined in the obvious way. --=20 https://bugs.ruby-lang.org/