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 Operation::Unary

#initialize

Methods inherited from Function

extract_value, rename_attributes

Methods included from Visitable

#accept

Class Method Details

.call(sum, value) ⇒ Numeric

Return the sum for a sequence of numbers

Examples:

sum = Sum.call(sum, value)

Parameters:

Returns:



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:



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

def default
  if    type <= Types::Float   then super.to_f
  elsif type <= Types::Decimal then super.to_d
  else
    super
  end
end

#typeClass<Types::Numeric>

Return the type returned from #call

Examples:

type = aggregate.type  # => Axiom::Types::Numeric

Returns:

  • (Class<Types::Numeric>)


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

def type
  Attribute.infer_type(operand)
end