Created
February 10, 2012 22:09
-
-
Save mediocretes/1793402 to your computer and use it in GitHub Desktop.
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
class Hash | |
def self.nmerge(*hashes, &block) | |
keys = hashes.map(&:keys).flatten.uniq | |
rhashes = hashes.reverse | |
keys.inject({ }) do |output, key| | |
if block | |
output[key] = block.call(key, hashes.map{ |x| x[key]}) | |
else | |
rhashes.each do |hash| | |
if hash[key] | |
output[key] = hash[key] | |
break | |
end | |
end | |
end | |
output | |
end | |
end | |
end |
janxious
commented
Feb 11, 2012
And the fully functioning version:
class Hash
def self.nmerge(*hashes, &block)
keys = hashes.map(&:keys).flatten.uniq
rhashes = hashes.reverse
keys.inject({ }) do |output, key|
if block
output[key] = block.call(key, hashes.map{ |x| x[key]})
else
rhashes.each do |hash|
if hash[key]
output[key] = hash[key]
break
end
end
end
output
end
end
def self.nmerge4(*hashes, &block)
if block
unique_keys = hashes.map(&:keys).flatten.uniq
unique_keys.inject({}) do |output, key|
output[key] = block.call(key, hashes.map{|x| x[key]})
output
end
else
Hash[hashes.map(&:to_a).flatten(1)]
end
end
end
one = {:one => 1, :two => 1}
two = {:two => 2, :three => 2}
three = {:three => 3, :four => 3}
four = {:four => 4, :five => 4}
five = {:five => 5, :six => 5}
six = {:six => 6, :seven => 6}
require 'benchmark'
Benchmark.bm do |b|
b.report("non-block nmerge") do
400_000.times { Hash.nmerge(one,two,three,four,five,six) }
end
b.report("non-block nmerge4") do
400_000.times { Hash.nmerge4(one,two,three,four,five,six) }
end
b.report("block nmerge") do
100_000.times {
Hash.nmerge(one, two, three, four, five, six) do |key, values|
values.inject(0){ |sum, item| sum + (item || 0) }
end
}
end
b.report("block nmerge4") do
100_000.times {
Hash.nmerge4(one, two, three, four, five, six) do |key, values|
values.inject(0){ |sum, item| sum + (item || 0) }
end
}
end
end
# user system total real
# non-block nmerge 7.780000 0.010000 7.790000 ( 7.869783)
# non-block nmerge4 3.620000 0.010000 3.630000 ( 3.681451)
# block nmerge 3.520000 0.000000 3.520000 ( 3.551300)
# block nmerge4 3.430000 0.000000 3.430000 ( 3.442584)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment