Class: RDF::N3::Algebra::Math::Difference

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

Overview

The subject is a pair of numbers. The object is calculated by subtracting the second number of the pair from the first.

Examples:

{ ("8" "3") math:difference ?x} => { ?x :valueOf "8 - 3" } .
{ ("8") math:difference ?x } => { ?x :valueOf "8 - (error?)" } .
{ (8 3) math:difference ?x} => { ?x :valueOf "8 - 3" } .

See Also:

Constant Summary collapse

NAME =
:mathDifference
URI =
RDF::N3::Math.difference

Instance Attribute Summary

Attributes included from Enumerable

#existentials, #universals

Instance Method Summary collapse

Methods inherited from ListOperator

#as_literal, #execute, #input_operand

Methods included from Builtin

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

Instance Method Details

#resolve(list) ⇒ RDF::Term

The math:difference operator takes a pair of strings or numbers and calculates their difference.

Parameters:

Returns:

See Also:

  • ListOperator#evaluate


21
22
23
# File 'lib/rdf/n3/algebra/math/difference.rb', line 21

def resolve(list)
  list.to_a.map(&:as_number).reduce(&:-)
end

#validate(list) ⇒ Boolean

The list argument must be a pair of literals.

Parameters:

Returns:

  • (Boolean)

See Also:

  • ListOperator#validate


31
32
33
34
35
36
37
38
# File 'lib/rdf/n3/algebra/math/difference.rb', line 31

def validate(list)
  if super && list.all?(&:literal?) && list.length == 2
    true
  else
    log_error(NAME) {"list is not a pair of literals: #{list.to_sxp}"}
    false
  end
end