为了简洁起见,我想优化以下代码。
x1.each { |x|
x2.each { |y|
....
xN.each { |z|
yield {}.merge(x).merge(y)...... merge(z)
}
}
}
假设x1, x2, ..., xN
是Enumerator对象。
- 以上不简洁
- 它适用于 x1, x2 作为
Array
s,但不适用于Enumerator
s- 因为应该为内部循环重置枚举器迭代器
我试过这个但没有成功:
[x1, x2, ..., xN].reduce(:product).map { |x| x.reduce :merge }
有什么建议吗?
更新
目前解决了:
[x1, x2, ..., xN].map(:to_a).reduce(:product).map { |x|
yield x.flatten.reduce(:merge)
}