Sha256: 56cf428f15fddf6d9a69d2d83ba046a0f78927e25d19b111873efd671ff163a7

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

module RDF::N3::Algebra::Math
  ##
  # The subject is a pair of integers. The object is calculated by dividing the first number of the pair by the second and taking the remainder.
  #
  # @see https://www.w3.org/TR/xpath-functions/#func-numeric-mod
  class Remainder < RDF::N3::Algebra::ListOperator
    NAME = :mathRemainder
    URI = RDF::N3::Math.remainder

    ##
    # The math:remainder operator takes a pair of strings or numbers and calculates their remainder.
    #
    # @param [RDF::N3::List] list
    # @return [RDF::Term]
    # @see RDF::N3::ListOperator#evaluate
    def resolve(list)
      list.to_a.map(&:as_number).reduce(&:%)
    end

    ##
    # The list argument must be a pair of literals.
    #
    # @param [RDF::N3::List] list
    # @return [Boolean]
    # @see RDF::N3::ListOperator#validate
    def validate(list)
      if super && list.all? {|li| li.is_a?(RDF::Literal) && li.as_number.is_a?(RDF::Literal::Integer)} && list.length == 2
        true
      else
        log_error(NAME) {"list is not a pair of integers: #{list.to_sxp}"}
        false
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rdf-n3-3.3.0 lib/rdf/n3/algebra/math/remainder.rb
rdf-n3-3.2.1 lib/rdf/n3/algebra/math/remainder.rb
rdf-n3-3.2.0 lib/rdf/n3/algebra/math/remainder.rb
rdf-n3-3.1.2 lib/rdf/n3/algebra/math/remainder.rb