Skip to content

Instantly share code, notes, and snippets.

@markwatson
Created March 11, 2013 21:40
Show Gist options
  • Save markwatson/5138030 to your computer and use it in GitHub Desktop.
Save markwatson/5138030 to your computer and use it in GitHub Desktop.
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