Closed artob closed 8 years ago
This will just find the first such node, rather than all matching nodes. It could be considered analogous to Enumerable#first_object
, which is a special case returning the first of all such objects. A pattern such as:
ast.each_descendant {|node| node.is_a?(klass)}
The first-case could be implemented as
ast.each_descendant.detect {|node| node.is_a?(klass)}
Then #each_descendant
simply recursively enumerates each operand. #find_node
could be sugar for this. WDYT?
The ast.each_descendant {|node| node.is_a?(klass)}
seems to me more neat. And yes then a bit of sugar to make it intuitive.
OK, let's see if we can manage with SPARQL::Algebra::Operator#each_descendant
for now. We can add sugar to the gem if it turns out to be needed.
So, this is implemented as SPARQL::Algebra::Operator#descendants
, which does what I described for #each_descendant
; I renamed it #each_descendant
and aliased as #each
and #each_descendant
. I'm not able to make Operator
and Enumerable due to dependencies in the Parser (Array(sxp) does the wrong thing).
Also, it does not descend into RDF::Query::Patterns, so you need to do something like the following:
ast.each_descendant.select {|op| op.is_a?(RDF::Query)}.map(&:patterns).flatten
Sounds reasonable, let see how far we can go with them.
On Sat, Sep 19, 2015 at 7:49 AM, Gregg Kellogg notifications@github.com wrote:
So, this is implemented as SPARQL::Algebra::Operator#descendants, which does what I described for #each_descendant; I renamed it #each_descendant and aliased as #each and #each_descendant. I'm not able to make Operator and Enumerable due to dependencies in the Parser (Array(sxp) does the wrong thing).
Also, it does not descend into RDF::Query::Patterns, so you need to do something like the following:
ast.each_descendant.select {|op| op.is_a?(RDF::Query)}.map(&:patterns).flatten
— Reply to this email directly or view it on GitHub https://github.com/ruby-rdf/sparql/issues/23#issuecomment-141587938.
Ra
Released in 1.1.9.
Quite often, one needs to process or transform the result of
SPARQL.parse()
in some way, which typically involves locating a child node of a particular kind. Here follows a simple proposal for methods to aid in this:I've opened this as a ticket, instead of directly committing it, in order to check with @gkellogg regarding naming and/or duplication of functionality with whatever facilities may already exist to do this:
CC: @helios