Actions
Feature #10254
closedArray#each and Array#map for nested arrays
Status:
Feedback
Assignee:
-
Target version:
-
Description
In order to allow iteration over elements of arrays nested within an array, I propose to pass Array#each
and Array#map
an optional argument that expresses the depth to iterate over.
Conventionally, iterating over nested elements requires nested each
or map
:
[[1, 2], [3, 4], [5, 6]].map{|a| a.map{|e| e + 1}} #=> [[2, 3], [4, 5], [6, 7]]
[[[1, 2], [3, 4]], [[5, 6]]].map{|a| a.map{|a| a.map{|e| e + 1}}} #=> [[[2, 3], [4, 5]], [[6, 7]]]
With the proposed optional argument, this would be done by:
[[1, 2], [3, 4], [5, 6]].map(1){|e| e + 1} #=> [[2, 3], [4, 5], [6, 7]]
[[[1, 2], [3, 4]], [[5, 6]]].map(2){|e| e + 1} #=> [[[2, 3], [4, 5]], [[6, 7]]]
Absence of the parameter should be understood as the parameter being defaulted to 0
.
[1, 2, 3, 4, 5, 6].map{|e| e + 1} #=> [2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6].map(0){|e| e + 1} #=> [2, 3, 4, 5, 6, 7]
Actions
Like0
Like0Like0Like0