ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:100471] [Ruby master Feature#17277] Make Enumerator#with_index yield row and col indices for Matrix
@ 2020-10-21 10:07 grzegorz.jakubiak
  2020-10-21 11:26 ` [ruby-core:100472] " marcandre-ruby-core
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: grzegorz.jakubiak @ 2020-10-21 10:07 UTC (permalink / raw)
  To: ruby-core

Issue #17277 has been reported by greggzst (Grzegorz Jakubiak).

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

* 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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-12-16 15:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [ruby-core:100492] " nobu
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

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).