Class: Axiom::Aggregate::Sum

Inherits:
Axiom::Aggregate show all
Defined in:
lib/axiom/aggregate/sum.rb

Overview

The sum of a sequence of numbers

Defined Under Namespace

Modules: Methods

Constant Summary collapse

DEFAULT =
0

Instance Attribute Summary

Attributes included from Operation::Unary

#operand

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Axiom::Aggregate

#call, default, finalize, #finalize

Methods included from Visitable

#accept

Methods included from Operation::Unary

#initialize

Class Method Details

.call(sum, value) ⇒ Numeric

Return the sum for a sequence of numbers

Examples:

sum = Sum.call(sum, value)

Parameters:

  • sum (Numeric)
  • value (Object)

Returns:

  • (Numeric)


23
24
25
26
# File 'lib/axiom/aggregate/sum.rb', line 23

def self.call(sum, value)
  return sum if value.nil?
  sum + value
end

Instance Method Details

#defaultNumeric

TODO:

refactor once functions know their return types

Return the default sum

Examples:

when the operand is a float

default = sum.default  # => 0.0

when the operand is a decimal

default = sum.default  # => BigDecimal('0.0')

when the operand is an integer

default = sum.default  # => 0

Returns:

  • (Numeric)


44
45
46
47
48
49
50
# File 'lib/axiom/aggregate/sum.rb', line 44

def default
  if    type.equal?(Attribute::Float)   then super.to_f
  elsif type.equal?(Attribute::Decimal) then BigDecimal(super.to_s)
  else
    super
  end
end

#typeClass<Attribute::Numeric>

Return the type returned from #call

Examples:

type = Axiom::Aggregate::Sum.type

Returns:



60
61
62
# File 'lib/axiom/aggregate/sum.rb', line 60

def type
  Attribute.infer_type(operand)
end