ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nobu@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:100492] [Ruby master Feature#17277] Make Enumerator#with_index yield row and col indices for Matrix
Date: Thu, 22 Oct 2020 05:30:46 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-88111.20201022053045.13458@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17277.20201021100709.13458@ruby-lang.org

Issue #17277 has been updated by nobu (Nobuyoshi Nakada).

Assignee set to marcandre (Marc-Andre Lafortune)
Status changed from Open to Assigned

----------------------------------------
Feature #17277: Make Enumerator#with_index yield row and col indices for Matrix
https://bugs.ruby-lang.org/issues/17277#change-88111

* Author: greggzst (Grzegorz Jakubiak)
* Status: Assigned
* Priority: Normal
* Assignee: marcandre (Marc-Andre Lafortune)
----------------------------------------
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/

  parent reply	other threads:[~2020-10-22  5:30 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-21 10:07 [ruby-core:100471] [Ruby master Feature#17277] Make Enumerator#with_index yield row and col indices for Matrix grzegorz.jakubiak
2020-10-21 11:26 ` [ruby-core:100472] " marcandre-ruby-core
2020-10-21 12:07 ` [ruby-core:100473] " grzegorz.jakubiak
2020-10-22  5:30 ` nobu [this message]
2020-10-22  6:02 ` [ruby-core:100493] " grzegorz.jakubiak
2020-10-22 15:16 ` [ruby-core:100507] " sawadatsuyoshi
2020-10-30 10:07 ` [ruby-core:100666] " grzegorz.jakubiak
2020-12-16 15:47 ` [ruby-core:101467] " marcandre-ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-88111.20201022053045.13458@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).