Created
March 11, 2013 21:40
-
-
Save markwatson/5138030 to your computer and use it in GitHub Desktop.
Merges an array of arrays by adding the elements together.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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