class SPARQL::Algebra::Operator::Sample

The SPARQL `sample` set function.

@example

(prefix ((: <http://www.example.org/>))
  (filter (|| (|| (= ?sample 1.0) (= ?sample 2.2)) (= ?sample 3.5))
    (project (?sample)
      (extend ((?sample ??.0))
        (group () ((??.0 (sample ?o)))
          (bgp (triple ?s :dec ?o)))))))

@see www.w3.org/TR/sparql11-query/#defn_aggSample

Constants

NAME

Public Class Methods

new(*operands, **options) click to toggle source
Calls superclass method SPARQL::Algebra::Operator::new
# File lib/sparql/algebra/operator/sample.rb, line 20
def initialize(*operands, **options)
  raise ArgumentError,
    "sample 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

Sample is a set function which returns an arbitrary value from the multiset passed to it.

@param [Enumerable<Array<RDF::Term>>] enum

enum of evaluated operand

@return [RDF::Term] An arbitrary term @raise [TypeError] If enum is empty

# File lib/sparql/algebra/operator/sample.rb, line 34
def apply(enum, **options)
  enum.detect(lambda {raise TypeError, "Sampling an empty multiset"}) {|e| e.first}.first
end