Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merges an array of arrays by adding the elements together.
# Takes an array of totals_tuples and combines them into a single tuple.
def self.merge_tuples(tuples)
tuples.inject([]) do |xs, ys|
l = [xs.length, ys.length].max
zs = []
l.times.each do |i|
zs[i] = (xs[i] || 0) + (ys[i] || 0)
end
zs
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment