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.



230
231
232
233
234
# File 'lib/bmg/operator/autosummarize.rb', line 230

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

Instance Method Details

#init(v) ⇒ Object



236
237
238
# File 'lib/bmg/operator/autosummarize.rb', line 236

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

#inspectObject Also known as: to_s



256
257
258
# File 'lib/bmg/operator/autosummarize.rb', line 256

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

#sum(v1, v2) ⇒ Object



240
241
242
# File 'lib/bmg/operator/autosummarize.rb', line 240

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

#term(v) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/bmg/operator/autosummarize.rb', line 244

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