Skip to content

Instantly share code, notes, and snippets.

@danielharan
Created July 16, 2009 01:43
Show Gist options
  • Save danielharan/148102 to your computer and use it in GitHub Desktop.
Save danielharan/148102 to your computer and use it in GitHub Desktop.
Refactoring an example from Hadoop: The Definitive Guide, 1st Edition
#!/usr/bin/env ruby
maxima = Hash.new(0)
STDIN.each_line do |line|
key,val = line.split("\t")
maxima[key] = [maxima[key], val.to_i].max
end
maxima.each do |key,val|
puts [key,val].join("\t")
end
#!/usr/bin/env ruby
last_key, max_val = nil, 0
STDIN.each_line do |line|
key, val = line.split("\t")
if last_key && last_key != key
puts "#{last_key}\t#{max_val}"
last_key, max_val = key, val.to_i
else
last_key, max_val = key, [max_val, val.to_i].max
end
end
puts "#{last_key}\t#{max_val}" if last_key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment