[#100309] How to use backport custom field — Jun Aruga <jaruga@...>
Please allow my ignorance.
9 messages
2020/10/06
[#100310] Re: How to use backport custom field
— "NARUSE, Yui" <naruse@...>
2020/10/06
IkJhY2twb3J0IGN1c3RvbSBmaWVsZCIgaXMgb25seSBhdmFpbGFibGUgZm9yIHRpY2tldHMgd2hv
[#100311] Re: How to use backport custom field
— Jun Aruga <jaruga@...>
2020/10/06
On Tue, Oct 6, 2020 at 4:44 PM NARUSE, Yui <[email protected]> wrote:
[#100314] Re: How to use backport custom field
— "NARUSE, Yui" <naruse@...>
2020/10/06
VGhhbmsgeW91IGZvciBjb25maXJtYXRpb24uCkkgY2hlY2tlZCBhZ2FpbiBhbmQgdG8gZWRpdCBi
[#100322] Re: How to use backport custom field
— Jun Aruga <jaruga@...>
2020/10/07
On Tue, Oct 6, 2020 at 7:25 PM NARUSE, Yui <[email protected]> wrote:
[#100326] Re: How to use backport custom field
— "NARUSE, Yui" <naruse@...>
2020/10/07
SSBhZGRlZCB5b3UgdG8gIlJlcG9ydGVyIiByb2xlIGluIHRoZSBwcm9qZWN0CgoyMDIw5bm0MTDm
[#100327] Re: How to use backport custom field
— Jun Aruga <jaruga@...>
2020/10/07
On Wed, Oct 7, 2020 at 1:42 PM NARUSE, Yui <[email protected]> wrote:
[ruby-core:100473] [Ruby master Feature#17277] Make Enumerator#with_index yield row and col indices for Matrix
From:
grzegorz.jakubiak@...
Date:
2020-10-21 12:07:53 UTC
List:
ruby-core #100473
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/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>