Skip to content

Instantly share code, notes, and snippets.

@aherve
Created December 6, 2016 14:19
Show Gist options
  • Select an option

  • Save aherve/85672b05d7bace21349ea890afcd488d to your computer and use it in GitHub Desktop.

Select an option

Save aherve/85672b05d7bace21349ea890afcd488d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Reducer
attr_accessor :key, :game_type, :user_id
def initialize(key,value)
@key = key
#split the primary key to get user_id and game type:
@game_type,@user_id = key.split("|")
#to perform an on-the-fly average, we only need two variables:
@count = 1
@average = value.to_f
end
#on the fly averaging. We do NOT store the entire array !
def accumulate(value)
@average = ( @count.to_f/(@count + 1) * @average ) + (value.to_f / (@count + 1) )
@count += 1
end
# follow the expectations
def output!
puts [
@user_id,
@game_type,
@average.round(1),
].join("\t")
end
end
ARGF.each do |line|
next if line.chomp.empty?
# split the data
new_key,value = line.chomp.split("\t").map(&:strip)
#initialize if required
@red ||= Reducer.new(new_key,value)
# if the key is the same, then continue accumulating
if new_key == @red.key
@red.accumulate(value)
# if the key is new, then first output current results, then instanciate a new reducer
else
@red.output!
@red = Reducer.new(new_key,value)
end
end
@red.output!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment