class SPARQL::Algebra::Operator::Avg
The SPARQL
`avg` set function.
@example
(prefix ((: <http://www.example.org/>)) (project (?avg) (extend ((?avg ??.0)) (group () ((??.0 (avg ?o))) (bgp (triple ?s :dec ?o))))))
Constants
- NAME
Public Class Methods
new(*operands, **options)
click to toggle source
Calls superclass method
SPARQL::Algebra::Operator::new
# File lib/sparql/algebra/operator/avg.rb, line 19 def initialize(*operands, **options) raise ArgumentError, "avg operator accepts at most one argument with an optional :distinct" if (operands - %i{distinct}).length != 1 super end
Public Instance Methods
apply(enum, **options)
click to toggle source
The Avg
set function calculates the average value for an expression over a group. It is defined in terms of Sum
and Count
.
@param [Enumerable<Array<RDF::Term>>] enum
enum of evaluated operand
@return [RDF::Literal::Numeric] The numeric average of the terms
# File lib/sparql/algebra/operator/avg.rb, line 32 def apply(enum, **options) # FIXME: we don't actually do anything with distinct operands.shift if distinct = (operands.first == :distinct) if enum.empty? RDF::Literal(0) elsif enum.flatten.all? {|n| n.is_a?(RDF::Literal::Numeric)} enum.flatten.reduce(:+) / RDF::Literal::Decimal.new(enum.length) else raise TypeError, "Averaging non-numeric types: #{enum.flatten}" end end