Class: RDF::N3::Algebra::Math::Sum

Inherits:
ListOperator
  • Object
show all
Defined in:
lib/rdf/n3/algebra/math/sum.rb

Overview

schema: ($a_1 .. $a_n) math:sum $a_s

summary: performs addition of numbers

definition: true if and only if the arithmetic sum of $a_1, .. $a_n equals $a_s. Requires either:

  1. all $a_1, .., $a_n to be bound; or

  2. all but one $a_i (subject list) to be bound, and $a_s to be bound.

literal domains:

  • $a_1 .. $a_n : xs:decimal (or its derived types), xs:float, or xs:double (see note on type promotion, and casting from string)

  • $a_s: xs:decimal (or its derived types), xs:float, or xs:double (see note on type promotion, and casting from string)

Examples:

{ ("3" "5") math:sum ?x } => { ?x :valueOf "3 + 5" } .
{ (3 5) math:sum ?x } => { ?x :valueOf "3 + 5 = 8" } .

See Also:

Constant Summary collapse

NAME =
:mathSum
URI =
RDF::N3::Math[:sum]

Instance Attribute Summary

Attributes included from Enumerable

#existentials, #universals

Instance Method Summary collapse

Methods inherited from ListOperator

#as_literal, #execute, #input_operand, #validate

Methods included from Builtin

#each, #evaluate, #hash, #input_operand, #rank, #to_uri

Instance Method Details

#resolve(list) ⇒ RDF::Term

Evaluates to the sum of the list elements

Parameters:

Returns:

See Also:

  • ListOperator#evaluate


36
37
38
# File 'lib/rdf/n3/algebra/math/sum.rb', line 36

def resolve(list)
  list.to_a.map(&:as_number).reduce(&:+) || RDF::Literal(0)  # Empty list sums to 0
end