I'd like to optimize following code for conciseness.
x1.each { |x|
x2.each { |y|
....
xN.each { |z|
yield {}.merge(x).merge(y)...... merge(z)
}
}
}
Assume x1, x2, ..., xN
are Enumerator objects.
- The above is not concise
- It works with x1, x2 as
Array
s, but not asEnumerator
s- Because enumerator iterators should be reset for inner loops
I tried this but without success:
[x1, x2, ..., xN].reduce(:product).map { |x| x.reduce :merge }
Any recommendations?
UPDATE
currently solved with:
[x1, x2, ..., xN].map(:to_a).reduce(:product).map { |x|
yield x.flatten.reduce(:merge)
}
x1.product(x2,.,xn).each { |hash,elem| elem.reduce({},:merge) }
may work... – Wale[x1, x2, ..., xN].reduce(:product).map { |x| x.reduce :merge }
– Justinajustineproduct
is not defined onEnumerator
instances. – Garfieldgarfinkel