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.
Array
s, but not as Enumerator
s
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)
}
I'll start with point #2:
Enumerators
I've tested ([{a: 1}, {a: 2}, {a: 3}].each
) your code worked - apparently Enumerator#each
either rewinds at the end, or it uses its own pointer.Enumerator
objects (especially the inner ones) that calling to_a
on each at first will not increase your time complexity at all (it will stay O(n1*n2*...*nk)
With point #1, if calling to_a
is out of the question, you can consider recursion:
def deep_merge(enum = nil, *enums)
if enum.nil?
yield({})
else
enum.each do |x|
deep_merge(*enums) do |h|
yield h.merge(x)
end
end
end
end
now you can call deep_merge(x1, x2, ... xN)
and get the needed outcome...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With