Ruby inner flatten (array of arrays)

北慕城南 提交于 2019-12-04 04:08:04

问题


I have an array like the following

[
  [[0, :a], [2, :b]],
  [3, :c],
  [4, :d],
  [[5, :e], [6, :f], [7, :g]]
]

That is, an Array of elements that are either (1) 2-element Arrays, or (2) an Array of 2-element Arrays.

I am trying to find an elegant way to "flatten" this array such that elements that are (2) get expanded out into root-level elements. In this example:

[[0, :a], [2, :b], [3, :c], [4, :d], [5, :e], [6, :f], [7, :g]]

This is almost like using Array#flatten(depth), except depth needs to work from the inside out, rather than the outside in.

The actual arrays can get very large, so I do not want to push (<<) elements onto a new Array in a loop for performance reasons. For some reason I cannot think of how to use any combination of map, flatten, flat_map, or other more efficient Enumerable methods to accomplish this without writing a C++-style preallocate-and-populate loop. Can anyone think of a more Rubyist way to do this?


回答1:


One way to do is:

array.flatten.each_slice(2).to_a



回答2:


Posting another solution here for posterity:

array.flat_map{|el| el.first.is_a?(Array) ? el : [el]}

This solution also handles if the innermost Arrays are of variable length -- that is, the total Array is an Array of (1) n-element Arrays or (2) Arrays of n-element Arrays, where n is not necessarily the same for any given element. For example

[
  [1,2,3],
  [[4,5], [6,7,8,9]]
]


来源:https://stackoverflow.com/questions/37283623/ruby-inner-flatten-array-of-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!