Module: Trxl::AvgSumFunction2

Defined in:
lib/trxl/trxl_grammar.rb

Instance Method Summary collapse

Instance Method Details

#eval(env = Environment.new) ⇒ Object



6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
# File 'lib/trxl/trxl_grammar.rb', line 6775

def eval(env = Environment.new)
  strict = true
  nr_of_vals = 0
  values = expressions
  strict_flag = values[0].eval(env)
  if strict_flag.is_a?(TrueClass) || strict_flag.is_a?(FalseClass)
    values.shift
    strict = strict_flag
  end
  values.inject(0) do |sum, e|
    next_val = e.eval(env)
    sum + if next_val.is_a?(Array)
      nr_of_vals = 0
      res = next_val.inject(0) do |next_sum, val|
        if val.is_a?(Array)
          next_sum + val.inject(0) { |s, v| s + (v || 0) } / val.compact.size
        else
          nr_of_vals += 1 if val && (strict || (!strict && val != 0))
          next_sum + (val || 0)
        end
      end 
      nr_of_vals != 0 ? res / nr_of_vals : res
    else
      next_val || 0
    end
  end
end

#expressionsObject



6803
6804
6805
# File 'lib/trxl/trxl_grammar.rb', line 6803

def expressions
  [ expression ] + more_expressions.elements.map { |e| e.expression }
end