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=AWL,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 31BFC1F4C0 for ; Tue, 22 Oct 2019 13:44:56 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 1EC291208FA; Tue, 22 Oct 2019 22:44:46 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 26B401208F1 for ; Tue, 22 Oct 2019 22:44:41 +0900 (JST) Received: by filter0086p3las1.sendgrid.net with SMTP id filter0086p3las1-23560-5DAF07CA-6E 2019-10-22 13:44:42.40180593 +0000 UTC m=+56373.582534724 Received: from herokuapp.com (unknown [54.175.237.173]) by ismtpd0040p1iad2.sendgrid.net (SG) with ESMTP id eISQpKnvStaWa-frV7ZO9Q for ; Tue, 22 Oct 2019 13:44:42.325 +0000 (UTC) Date: Tue, 22 Oct 2019 13:44:42 +0000 (UTC) From: daniel@dan42.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 71056 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16261 X-Redmine-Issue-Author: zverok X-Redmine-Sender: Dan0042 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?8sy4RigFvRTdBfCVJrT9zb2J88PC92TMQwdNgaWYaq4flu1zZSR5=2F2yyeswsAJ?= =?us-ascii?Q?WSEgpL=2FEpSlgB+TdE4fZZ7RRUV=2FtlyzZt9t6W5g?= =?us-ascii?Q?d8XxLFFwaEnXSXA6EZNGePndzB+a0mNGMSO5ClY?= =?us-ascii?Q?0PQanVutT+KZHroOvxdKaZE6U4XZAGMeVDbzP92?= =?us-ascii?Q?aHjjcCt3t8m49ZKr+l0ID3g4aU=2Fhl0Mznkw=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95464 Subject: [ruby-core:95464] [Ruby master Feature#16261] Enumerable#each_tuple 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 #16261 has been updated by Dan0042 (Daniel DeLorme). It's worth pointing out the desired difference with regards to lambdas a bit more explicitly: ```ruby [1, 2, 3].zip([4, 5, 6]).map(&:+) # ArgumentError (wrong number of arguments (given 0, expected 1)) [1, 2, 3].zip([4, 5, 6]).each_tuple.map(&:+) # => [5, 7, 9] ``` But in that case it seems to me the behavior you want is the *opposite* of a tuple. Where a tuple is a struct-like set of n elements like `[1, 4]`, what you want here is to destructure that tuple in order to pass each element as an argument of a lambda. So it should be called maybe `each_splat` and the *inverse* operation would be called `each_tuple`. Or how about something like this based on Enumerator? (hopefully without the hacks) ```ruby class Enumerator def splat return to_enum(:splat) unless block_given? #each{ |item| yield(*item) } #this doesn't always work each{ |first,*rest| yield(first,*rest) } #hacky solution end def tuple return to_enum(:tuple) unless block_given? #each{ |*item| yield(item) } #this doesn't always work each{ |first,*rest| yield([first,*rest]) } #hacky solution end end pairs = [10, 20, 30].zip([10, 16, 20]) pairs.each.map(&:to_s) #=> ["[10, 10]", "[20, 16]", "[30, 20]"] pairs.each.tuple.map(&:to_s) #=> ["[10, 10]", "[20, 16]", "[30, 20]"] pairs.each.splat.map(&:to_s) #=> ["10", "14", "1a"] %i[a b c].each_with_index.map(&:inspect) # ArgumentError (wrong number of arguments (given 1, expected 0)) %i[a b c].each_with_index.tuple.map(&:inspect) # => ["[:a, 0]", "[:b, 1]", "[:c, 2]"] %i[a b c].each_with_index.splat.map(&:inspect) # ArgumentError (wrong number of arguments (given 1, expected 0)) ``` ---------------------------------------- Feature #16261: Enumerable#each_tuple https://bugs.ruby-lang.org/issues/16261#change-82234 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- New method proposal. Prototype code: ```ruby module Enumerable def each_tuple return to_enum(__method__) unless block_given? each { |item| yield(*item) } # unpacking possible array into several args end end ``` Supposed documentation/explanation: > For enumerable with Array items, passes all items in the block provided as a separate arguments. t could be useful if the provided block has lambda semantics, e.g. doesn't unpack arguments automatically. For example: ```ruby files = ["README.md", "LICENSE.txt", "Contributing.md"] content = [fetch_readme, fetch_license, fetch_contributing] # somehow make a content for the files files.zip(content).each_tuple(&File.:write) # writes to each file its content ``` > When no block passed, returns enumerator of the tuples: ```ruby [1, 2, 3].zip([4, 5, 6]).each_tuple.map(&:+) # => [5, 7, 9] ``` -- https://bugs.ruby-lang.org/