Module: Trxl::AvgSumFunction2

Defined in:
lib/trxl/trxl_grammar.rb

Instance Method Summary collapse

Instance Method Details

#eval(env = Environment.new) ⇒ Object



7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
# File 'lib/trxl/trxl_grammar.rb', line 7496

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)
          if (size = val.compact.size) > 0
            next_sum + val.inject(0) { |s, v| s + (v || 0) } / size
          else
            next_sum
          end
        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



7528
7529
7530
# File 'lib/trxl/trxl_grammar.rb', line 7528

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