【发布时间】:2016-11-25 13:23:59
【问题描述】:
想出一种在 Ruby 中反转多维(偶数维)数组的功能性方法让我很吃惊。 输入:[[1, 2, 3], [4, 5, 6], [7, 8, 9]] 输出:[[7, 4, 1], [8, 5, 2], [9, 6, 3]]
此迭代解决方案有效。
def reverse(arr)
size = arr.length
output = Array.new(size) { Array.new(size,0) }
arr.reverse.each_with_index do |a, i|
a.each_with_index do |a, j|
output[j][i] = a
end
end
output
end
任何人都知道如何使用更多的函数式编程风格而不参考显式索引?
【问题讨论】:
标签: arrays ruby multidimensional-array collections functional-programming