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-Status: No, score=-2.6 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=no 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 E2D171F4B4 for ; Thu, 22 Oct 2020 15:16:40 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id C1DD7120BA3; Fri, 23 Oct 2020 00:16:00 +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 C27FE120BA1 for ; Fri, 23 Oct 2020 00:15:58 +0900 (JST) Received: by filterdrecv-p3mdw1-7975856966-wvf77 with SMTP id filterdrecv-p3mdw1-7975856966-wvf77-20-5F91A253-1B 2020-10-22 15:16:35.197223149 +0000 UTC m=+76363.577762011 Received: from herokuapp.com (unknown) by ismtpd0010p1iad2.sendgrid.net (SG) with ESMTP id 2lYcVvsfRiaCaTt7g7ySDw for ; Thu, 22 Oct 2020 15:16:35.130 +0000 (UTC) Date: Thu, 22 Oct 2020 15:16:35 +0000 (UTC) From: sawadatsuyoshi@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 76377 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17277 X-Redmine-Issue-Author: greggzst X-Redmine-Issue-Assignee: marcandre X-Redmine-Sender: sawa 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?jFXA8Rt481sXUUIO9tYW1AJlMOZdNdlSw=2F5TfLCefGvaTVOa9ukdoV=2F6FqbO2t?= =?us-ascii?Q?W6SL+xU2puvJVME4jfHQhOFAQcdifa00XrzGJd2?= =?us-ascii?Q?LZmrjFOVPMpjg4wAS3utCoWkXet6gpdBmjqsJTT?= =?us-ascii?Q?UgF+YtA4Wqr4pf1hSx2L7cUSw1bjxKvJFMxhmPm?= =?us-ascii?Q?1DzEtSpmqYFPIssJ7t3OYx7bsxus9xASVmsyDgz?= =?us-ascii?Q?yY6ByIFnjyEZeHiUg=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 100507 Subject: [ruby-core:100507] [Ruby master Feature#17277] Make Enumerator#with_index yield row and col indices for Matrix 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 #17277 has been updated by sawa (Tsuyoshi Sawada). I think the current behaviour is natural. You cannot play around with `with_index` since its receiver is `Enumerator`, not `Matrix`, and the information as a matrix is already gone. You can retrieve the row and column indices easily from the flattened indices: ```ruby m.each.with_index{|e, index| p index.divmod(m.column_size)} [0, 0] [0, 1] [0, 2] [0, 3] [1, 0] [1, 1] [1, 2] [1, 3] [2, 0] [2, 1] [2, 2] [2, 3] ``` ---------------------------------------- Feature #17277: Make Enumerator#with_index yield row and col indices for Matrix https://bugs.ruby-lang.org/issues/17277#change-88130 * Author: greggzst (Grzegorz Jakubiak) * Status: Assigned * Priority: Normal * Assignee: marcandre (Marc-Andre Lafortune) ---------------------------------------- Given a matrix: ```ruby matrix = Matrix[[0,2,3,4], [6,7,8,9], [1,4,5,8]] ``` You could get the row and col indices of a matrix using `Matrix#each_with_index`: ```ruby matrix .each_with_index { |e, row, col| p [row, col] } [0, 0] [0, 1] [0, 2] [0, 3] [1, 0] [1, 1] [1, 2] [1, 3] [2, 0] [2, 1] [2, 2] [2, 3] ``` You can chain it with other enumerators and access indices within them: ```ruby matrix .each_with_index .filter_map { |e, row, col| [row, col] if e % 4 == 0} # => [[0, 0], [0, 3], [1, 2], [2, 1], [2, 3]] ``` Meanwhile, `with_index` after `Matrix#each` returns flattened indices, not row or column indices, which does not look right: ```ruby matrix .each.with_index { |e, index| p index } 0 1 2 3 4 5 6 7 8 9 10 11 ``` I feel we should override `with_index` for `Matrix` so it returns row and column indices. -- https://bugs.ruby-lang.org/