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=-3.7 required=3.0 tests=AWL,BAYES_00, 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=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 353441F4B4 for ; Wed, 21 Oct 2020 12:08:07 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id BEA081209A6; Wed, 21 Oct 2020 21:07:24 +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 581151209A3 for ; Wed, 21 Oct 2020 21:07:21 +0900 (JST) Received: by filterdrecv-p3las1-68f969d758-m6lcv with SMTP id filterdrecv-p3las1-68f969d758-m6lcv-19-5F902499-DC 2020-10-21 12:07:53.820837397 +0000 UTC m=+40933.939348382 Received: from herokuapp.com (unknown) by ismtpd0093p1iad2.sendgrid.net (SG) with ESMTP id vIJEDTuGQ9mnwhxKMN_4vg for ; Wed, 21 Oct 2020 12:07:53.603 +0000 (UTC) Date: Wed, 21 Oct 2020 12:07:53 +0000 (UTC) From: grzegorz.jakubiak@outlook.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 76343 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17277 X-Redmine-Issue-Author: greggzst X-Redmine-Sender: greggzst 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?VGo1Yotd4zCzfg3lZZGknPzkEWcl6BbwQWWlBuZDX4Y7dCWTFW5Sa13VeKSq+h?= =?us-ascii?Q?C93oQEnOELXq6fdlQp8TLwgCrBi3ExGxhMi5VZn?= =?us-ascii?Q?rpROQEGcfYLT3kM9sxxN6Fyrk2hPNlDmFDxzEB+?= =?us-ascii?Q?CFegZo4bv3QWf6zXHgaxAnoIJO9D2X9HXbo3q3l?= =?us-ascii?Q?QfHQKD1+hXFa4v600AD0VgS0lk15hqGWkFOCgnj?= =?us-ascii?Q?fjGvYmWbf9nDyJT8c=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 100473 Subject: [ruby-core:100473] [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 greggzst (Grzegorz Jakubiak). I also noticed when combining `each_with_index` with `inject` it passes `element, row_index and col_index` as one argument to the block ```ruby Matrix[[1,2,4,5],[7,8,9,2]].each_with_index.inject({}) { |acc, e, row, col| puts "#{acc}, #{e}, #{row}, #{col}"; acc[[e[1],e[2]]] = e[0]; acc } {}, [1, 0, 0], , {[0, 0]=>1}, [2, 0, 1], , {[0, 0]=>1, [0, 1]=>2}, [4, 0, 2], , {[0, 0]=>1, [0, 1]=>2, [0, 2]=>4}, [5, 0, 3], , {[0, 0]=>1, [0, 1]=>2, [0, 2]=>4, [0, 3]=>5}, [7, 1, 0], , {[0, 0]=>1, [0, 1]=>2, [0, 2]=>4, [0, 3]=>5, [1, 0]=>7}, [8, 1, 1], , {[0, 0]=>1, [0, 1]=>2, [0, 2]=>4, [0, 3]=>5, [1, 0]=>7, [1, 1]=>8}, [9, 1, 2], , {[0, 0]=>1, [0, 1]=>2, [0, 2]=>4, [0, 3]=>5, [1, 0]=>7, [1, 1]=>8, [1, 2]=>9}, [2, 1, 3], , => {[0, 0]=>1, [0, 1]=>2, [0, 2]=>4, [0, 3]=>5, [1, 0]=>7, [1, 1]=>8, [1, 2]=>9, [1, 3]=>2} ``` marcandre (Marc-Andre Lafortune) wrote in #note-1: > What about chained enumerators? > > ```ruby > matrix.each(:diagonal).each_const(2).with_index do |elem, ?| > ``` > > It's not clear to me how that could be implemented efficiently, do you have an idea? I tried with refinement for Enumerator in Matrix and use `each_with_index` to yield `row` and `col` index but couldn't get it working... > Finally, what is the use case? I would like to access matrix indices in enumerable methods to perform some operations based on them. > I would rather add `map_with_index`.... I guess this would be helpful but I think it doesn't give enough flexibility because if you want to use `with_index` in `select` you don't need the `map` part and so on ---------------------------------------- Feature #17277: Make Enumerator#with_index yield row and col indices for Matrix https://bugs.ruby-lang.org/issues/17277#change-88092 * Author: greggzst (Grzegorz Jakubiak) * Status: Open * Priority: Normal ---------------------------------------- Currently this code seems to be counting index based on the internal array of arrays and it's not correct for the matrix which should return row and col indices ``` Matrix[[0,2,3,4], [6,7,8,9], [1,4,5,8]].each.with_index { |e, index| print "#{index} " } ; puts 0 1 2 3 4 5 6 7 8 9 10 11 => nil ``` I'm aware of the fact that you could do following and you get the correct results: ``` Matrix[[0,2,3,4], [6,7,8,9], [1,4,5,8]].each_with_index { |e, row, col| print "[#{row}, #{col}] " } ; puts [0, 0] [0, 1] [0, 2] [0, 3] [1, 0] [1, 1] [1, 2] [1, 3] [2, 0] [2, 1] [2, 2] [2, 3] => nil ``` You can even chain `each_with_index` with other enumerators and access indices within them e.g. ``` Matrix[[0,2,3,4], [6,7,8,9], [1,4,5,8]].each_with_index.filter_map { |e, row, col| [row, col] if e % 4 == 0} => [[0, 0], [0, 3], [1, 2], [2, 1], [2, 3]] ``` However, I feel we should override `with_index` for Matrix so it returns row and col indices. -- https://bugs.ruby-lang.org/