From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.9 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id DCE071F4BD for ; Mon, 7 Oct 2019 06:57:21 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 972E91209C5; Mon, 7 Oct 2019 15:57:12 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 4B2241209A1 for ; Mon, 7 Oct 2019 15:57:10 +0900 (JST) Received: by filter0183p3mdw1.sendgrid.net with SMTP id filter0183p3mdw1-3192-5D9AE1C7-9 2019-10-07 06:57:11.136265855 +0000 UTC m=+303980.530024821 Received: from herokuapp.com (unknown [54.158.92.1]) by ismtpd0072p1iad2.sendgrid.net (SG) with ESMTP id WOHvsl7QQD2vqUZINOooRA for ; Mon, 07 Oct 2019 06:57:10.994 +0000 (UTC) Date: Mon, 07 Oct 2019 06:57:11 +0000 (UTC) From: sin@prajjwal.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70844 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16155 X-Redmine-Issue-Author: connorshea X-Redmine-Sender: prajjwal 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: =?us-ascii?Q?lEJT7ggr225JDhUuXxKa6ajUBjSSTBMqIGVlrors3nnuc+UEAfbWfX+JCGiVhk?= =?us-ascii?Q?lUu0g0gSFfzeAhr15ywVsVbp7DeEBzcSGjmpbwz?= =?us-ascii?Q?SkoWmGPUUgAK1GFOuSRdLWHhXEP7yOHfgEZZGzL?= =?us-ascii?Q?xsVQ4vJFA0VG6T8zlGj0Y2SLgO7HIW2Js+fpyPL?= =?us-ascii?Q?tMQW1t3glEtFx?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95257 Subject: [ruby-core:95257] [Ruby master Feature#16155] Add an Array#intersection method 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 #16155 has been updated by prajjwal (Prajjwal Singh). Implementation is currently based on `Array#&`, which is elegant but might end up allocating a whole bunch of arrays holding intermediate results. If needed I can implement `Array#intersection` so it only allocates the result array once, but then I would like to rewrite `Array#&` in terms of `Array#intersection` to keep things DRY. ```ruby static VALUE rb_ary_intersection_multi(int argc, VALUE *argv, VALUE ary) { VALUE result = rb_ary_dup(ary); int i; for (i = 0; i < argc; i++) { result = rb_ary_and(result, argv[i]); } return result; } ``` Let me know what you think. ---------------------------------------- Feature #16155: Add an Array#intersection method https://bugs.ruby-lang.org/issues/16155#change-81934 * Author: connorshea (Connor Shea) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- `Array#union` and `Array#difference` were added in Ruby 2.6 ([see this bug](https://bugs.ruby-lang.org/issues/14097)), but an equivalent for `&` (intersection) was not. I'd like to propose `Array#intersection`. This would essentially just be a more readable alias for `Array#&`, in the same way that `Array#|` and `Array#-` have `Array#union` and `Array#difference`. I think it'd make sense for Ruby to have a more readable name for this method :) Current syntax: ``` ruby [ 1, 1, 3, 5 ] & [ 3, 2, 1 ] #=> [ 1, 3 ] [ 'a', 'b', 'b', 'z' ] & [ 'a', 'b', 'c' ] #=> [ 'a', 'b' ] ``` What I'd like to see added: ```ruby [ 1, 1, 3, 5 ].intersection([ 3, 2, 1 ]) #=> [ 1, 3 ] [ 'a', 'b', 'b', 'z' ].intersection([ 'a', 'b', 'c' ]) #=> [ 'a', 'b' ] ``` mame asks about `intersection` [in this comment](https://bugs.ruby-lang.org/issues/14097#note-26) on the `union`/`difference` bug, but as far as I can tell it was never addressed. [`Set#intersection`](http://ruby-doc.org/stdlib-2.6.2/libdoc/set/rdoc/Set.html#method-i-intersection) already exists and is an alias for `Set#&`, so there's precedent for such a method to exist. Thanks for Ruby, I enjoy using it a lot! :) Related links: - [PR for `union`](https://github.com/ruby/ruby/pull/1747) - [PR for `difference`](https://github.com/ruby/ruby/pull/1758) - [Bug for adding `union` and `difference` methods](https://bugs.ruby-lang.org/issues/14097) -- https://bugs.ruby-lang.org/