Class: Bmg::Operator::Autosummarize::YsByX

Inherits:
Object
  • Object
show all
Defined in:
lib/bmg/operator/autosummarize.rb

Overview

Summarizes by converting dependents to { x => [ys], … } such that ‘x` is not null and `[ys]` is a distinct list of observed non-null `y`.

Instance Method Summary collapse

Constructor Details

#initialize(y, x, &sorter) ⇒ YsByX

Returns a new instance of YsByX.



278
279
280
281
282
# File 'lib/bmg/operator/autosummarize.rb', line 278

def initialize(y, x, &sorter)
  @y = y
  @x = x
  @sorter = sorter
end

Instance Method Details

#init(v) ⇒ Object



284
285
286
# File 'lib/bmg/operator/autosummarize.rb', line 284

def init(v)
  v.nil? ? [] : [v]
end

#inspectObject Also known as: to_s



304
305
306
# File 'lib/bmg/operator/autosummarize.rb', line 304

def inspect
  ":#{@y}s_by_#{@x}"
end

#sum(v1, v2) ⇒ Object



288
289
290
# File 'lib/bmg/operator/autosummarize.rb', line 288

def sum(v1, v2)
  v2.nil? ? v1 : (v1 << v2)
end

#term(v) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
# File 'lib/bmg/operator/autosummarize.rb', line 292

def term(v)
  h = {}
  v = v.reject{|tuple| tuple[@x].nil? }
  v = v.sort(&@sorter) if @sorter
  v.each do |tuple|
    h[tuple[@x]] ||= []
    h[tuple[@x]] << tuple[@y]
    h[tuple[@x]].uniq!
  end
  h
end