Class: RDF::List
- Inherits:
-
Object
- Object
- RDF::List
- Includes:
- Enumerable, Resource
- Defined in:
- lib/rdf/model/list.rb
Overview
An RDF list.
Constant Summary
- NIL =
The canonical empty list.
RDF::List.new(RDF.nil).freeze
Instance Attribute Summary (collapse)
-
- (RDF::Graph) graph
readonly
Returns the underlying graph storing the statements that constitute this list.
-
- (RDF::Resource) subject
readonly
Returns the subject term of this list.
Class Method Summary (collapse)
-
+ (RDF::List) [](*values)
Constructs a new list from the given
values.
Instance Method Summary (collapse)
-
- (RDF::List) &(other)
Returns the set intersection of this list and
other. -
- (RDF::List) *(int_or_str)
Returns either a repeated list or a string concatenation of the elements in this list.
-
- (RDF::List) +(other)
Returns the concatenation of this list and
other. -
- (RDF::List) -(other)
Returns the difference between this list and
other, removing any elements that appear in both lists. -
- (RDF::List) <<(value)
Appends an element to the tail of this list.
-
- (Integer) <=>(other)
Compares this list to
otherfor sorting purposes. -
- (RDF::Term) [](index)
Returns the element at
index. -
- (RDF::Term) at(index)
(also: #nth)
Returns the element at
index. -
- (Enumerator) each(&block)
Yields each element in this list.
-
- (Enumerator) each_statement(&block)
Yields each statement constituting this list.
-
- (Enumerator) each_subject(&block)
Yields each subject term constituting this list.
-
- (RDF::Term) eighth
Returns the eighth element in this list.
-
- (Boolean) empty?
Returns
trueif this list is empty. -
- (RDF::Term) fetch(index, default = UNSET, &block)
Returns the element at
index. -
- (RDF::Term) fifth
Returns the fifth element in this list.
-
- (RDF::Term) first
Returns the first element in this list.
-
- (RDF::Resource) first_subject
Returns the first subject term constituting this list.
-
- (RDF::Term) fourth
Returns the fourth element in this list.
-
- (Integer) index(value)
Returns the index of the first element equal to
value, ornilif no match was found. -
- (List) initialize(subject = nil, graph = nil, values = nil) {|list| ... }
constructor
Initializes a newly-constructed list.
-
- (String) inspect
Returns a developer-friendly representation of this list.
-
- (String) join(sep = $,)
Returns a string created by converting each element of this list into a string, separated by
sep. -
- (RDF::Term) last
Returns the last element in this list.
-
- (RDF::Resource) last_subject
Returns the last subject term constituting this list.
-
- (Integer) length
(also: #size)
Returns the length of this list.
-
- (RDF::Term) ninth
Returns the ninth element in this list.
-
- (RDF::List) rest
Returns a list containing all but the first element of this list.
- - (RDF::Resource) rest_subject
-
- (RDF::List) reverse
Returns the elements in this list in reversed order.
-
- (RDF::Term) second
Returns the second element in this list.
-
- (RDF::Term) seventh
Returns the seventh element in this list.
-
- (RDF::Term) sixth
Returns the sixth element in this list.
-
- (RDF::Term) slice(*args)
Returns the element at
index. -
- (RDF::List) sort(&block)
Returns the elements in this list in sorted order.
-
- (RDF::List) sort_by(&block)
Returns the elements in this list in sorted order.
-
- (RDF::List) tail
Returns a list containing the last element of this list.
-
- (RDF::Term) tenth
Returns the tenth element in this list.
-
- (RDF::Term) third
Returns the third element in this list.
-
- (Array) to_a
Returns the elements in this list as an array.
-
- (String) to_s
Returns a string representation of this list.
-
- (Set) to_set
Returns the elements in this list as a set.
-
- (RDF::List) uniq
Returns a new list with the duplicates in this list removed.
-
- (RDF::List) |(other)
Returns the set union of this list and
other.
Methods included from Resource
Methods included from Term
Methods included from Value
#graph?, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_rdf, #uri?, #variable?
Methods included from Enumerable
#contexts, #dump, #each_context, #each_graph, #each_object, #each_predicate, #each_quad, #each_triple, #enum_context, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_triple, #has_context?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_triple?, #objects, #predicates, #quads, #statements, #subjects, #to_hash, #triples
Methods included from Util::Aliasing::LateBound
Methods included from Countable
Constructor Details
- (List) initialize(subject = nil, graph = nil, values = nil) {|list| ... }
Initializes a newly-constructed list.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rdf/model/list.rb', line 40 def initialize(subject = nil, graph = nil, values = nil, &block) @subject = subject || RDF::Node.new @graph = graph || RDF::Graph.new values.each { |value| self << value } unless values.nil? || values.empty? if block_given? case block.arity when 1 then block.call(self) else instance_eval(&block) end end end |
Instance Attribute Details
- (RDF::Graph) graph (readonly)
Returns the underlying graph storing the statements that constitute this list.
70 71 72 |
# File 'lib/rdf/model/list.rb', line 70 def graph @graph end |
- (RDF::Resource) subject (readonly)
Returns the subject term of this list.
63 64 65 |
# File 'lib/rdf/model/list.rb', line 63 def subject @subject end |
Class Method Details
+ (RDF::List) [](*values)
Constructs a new list from the given values.
The list will be identified by a new autogenerated blank node, and backed by an initially empty in-memory graph.
28 29 30 |
# File 'lib/rdf/model/list.rb', line 28 def self.[](*values) self.new(nil, nil, values) end |
Instance Method Details
- (RDF::List) &(other)
Returns the set intersection of this list and other.
The resulting list contains the elements common to both lists, with no duplicates.
86 87 88 |
# File 'lib/rdf/model/list.rb', line 86 def &(other) RDF::List[*(to_a & other.to_a)] end |
- (RDF::List) *(times) - (RDF::List) *(sep)
Returns either a repeated list or a string concatenation of the elements in this list.
160 161 162 163 164 165 |
# File 'lib/rdf/model/list.rb', line 160 def *(int_or_str) case int_or_str when Integer then RDF::List[*(to_a * int_or_str)] else join(int_or_str.to_s) end end |
- (RDF::List) +(other)
Returns the concatenation of this list and other.
117 118 119 |
# File 'lib/rdf/model/list.rb', line 117 def +(other) RDF::List[*(to_a + other.to_a)] end |
- (RDF::List) -(other)
Returns the difference between this list and other, removing any
elements that appear in both lists.
131 132 133 |
# File 'lib/rdf/model/list.rb', line 131 def -(other) RDF::List[*(to_a - other.to_a)] end |
- (RDF::List) <<(value)
Appends an element to the tail of this list.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rdf/model/list.rb', line 189 def <<(value) value = case value when nil then RDF.nil when RDF::Value then value when Array then RDF::List.new(nil, graph, value) else value end if empty? new_subject = subject else old_subject, new_subject = last_subject, RDF::Node.new graph.delete([old_subject, RDF.rest, RDF.nil]) graph.insert([old_subject, RDF.rest, new_subject]) end graph.insert([new_subject, RDF.type, RDF.List]) graph.insert([new_subject, RDF.first, value]) graph.insert([new_subject, RDF.rest, RDF.nil]) self end |
- (Integer) <=>(other)
Compares this list to other for sorting purposes.
223 224 225 |
# File 'lib/rdf/model/list.rb', line 223 def <=>(other) to_a <=> other.to_a # TODO: optimize this end |
- (RDF::Term) [](index)
Returns the element at index.
176 177 178 |
# File 'lib/rdf/model/list.rb', line 176 def [](index) at(index) end |
- (RDF::Term) at(index) Also known as: nth
Returns the element at index.
336 337 338 339 340 341 |
# File 'lib/rdf/model/list.rb', line 336 def at(index) each.with_index do |v, i| return v if i == index end return nil end |
- (Enumerator) each(&block)
Yields each element in this list.
555 556 557 558 559 560 561 562 563 |
# File 'lib/rdf/model/list.rb', line 555 def each(&block) return to_enum unless block_given? each_subject do |subject| if value = graph.first_object(:subject => subject, :predicate => RDF.first) block.call(value) # FIXME end end end |
- (Enumerator) each_statement(&block)
Yields each statement constituting this list.
575 576 577 578 579 580 581 |
# File 'lib/rdf/model/list.rb', line 575 def each_statement(&block) return enum_statement unless block_given? each_subject do |subject| graph.query(:subject => subject, &block) end end |
- (Enumerator) each_subject(&block)
Yields each subject term constituting this list.
532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/rdf/model/list.rb', line 532 def each_subject(&block) return enum_subject unless block_given? subject = self.subject block.call(subject) loop do rest = graph.first_object(:subject => subject, :predicate => RDF.rest) break if rest.nil? || rest.eql?(RDF.nil) block.call(subject = rest) end end |
- (RDF::Term) eighth
Returns the eighth element in this list.
429 430 431 |
# File 'lib/rdf/model/list.rb', line 429 def eighth at(7) end |
- (Boolean) empty?
Returns true if this list is empty.
236 237 238 |
# File 'lib/rdf/model/list.rb', line 236 def empty? graph.query(:subject => subject, :predicate => RDF.first).empty? end |
- (RDF::Term) fetch(index, default = UNSET, &block)
Returns the element at index.
316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/rdf/model/list.rb', line 316 def fetch(index, default = UNSET, &block) each.with_index do |v, i| return v if i == index end case when block_given? then block.call(index) when !default.eql?(UNSET) then default else raise IndexError, "index #{index} not in the list #{self.inspect}" end end |
- (RDF::Term) fifth
Returns the fifth element in this list.
396 397 398 |
# File 'lib/rdf/model/list.rb', line 396 def fifth at(4) end |
- (RDF::Term) first
Returns the first element in this list.
352 353 354 |
# File 'lib/rdf/model/list.rb', line 352 def first graph.first_object(:subject => first_subject, :predicate => RDF.first) end |
- (RDF::Resource) first_subject
Returns the first subject term constituting this list.
This is equivalent to #subject.
498 499 500 |
# File 'lib/rdf/model/list.rb', line 498 def first_subject subject end |
- (RDF::Term) fourth
Returns the fourth element in this list.
385 386 387 |
# File 'lib/rdf/model/list.rb', line 385 def fourth at(3) end |
- (Integer) index(value)
Returns the index of the first element equal to value, or nil if
no match was found.
266 267 268 269 270 271 |
# File 'lib/rdf/model/list.rb', line 266 def index(value) each.with_index do |v, i| return i if v == value end return nil end |
- (String) inspect
Returns a developer-friendly representation of this list.
689 690 691 692 693 694 695 696 |
# File 'lib/rdf/model/list.rb', line 689 def inspect if self.equal?(NIL) 'RDF::List::NIL' else #sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, subject.to_s) sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, to_s) # FIXME end end |
- (String) join(sep = $,)
Returns a string created by converting each element of this list into
a string, separated by sep.
594 595 596 |
# File 'lib/rdf/model/list.rb', line 594 def join(sep = $,) map(&:to_s).join(sep) end |
- (RDF::Term) last
Returns the last element in this list.
463 464 465 |
# File 'lib/rdf/model/list.rb', line 463 def last graph.first_object(:subject => last_subject, :predicate => RDF.first) end |
- (RDF::Resource) last_subject
Returns the last subject term constituting this list.
518 519 520 |
# File 'lib/rdf/model/list.rb', line 518 def last_subject each_subject.to_a.last # TODO: optimize this end |
- (Integer) length Also known as: size
Returns the length of this list.
249 250 251 |
# File 'lib/rdf/model/list.rb', line 249 def length each.count end |
- (RDF::Term) ninth
Returns the ninth element in this list.
440 441 442 |
# File 'lib/rdf/model/list.rb', line 440 def ninth at(8) end |
- (RDF::List) rest
Returns a list containing all but the first element of this list.
474 475 476 |
# File 'lib/rdf/model/list.rb', line 474 def rest (subject = rest_subject).eql?(RDF.nil) ? nil : self.class.new(subject, graph) end |
- (RDF::Resource) rest_subject
507 508 509 |
# File 'lib/rdf/model/list.rb', line 507 def rest_subject graph.first_object(:subject => subject, :predicate => RDF.rest) end |
- (RDF::List) reverse
Returns the elements in this list in reversed order.
606 607 608 |
# File 'lib/rdf/model/list.rb', line 606 def reverse RDF::List[*to_a.reverse] end |
- (RDF::Term) second
Returns the second element in this list.
363 364 365 |
# File 'lib/rdf/model/list.rb', line 363 def second at(1) end |
- (RDF::Term) seventh
Returns the seventh element in this list.
418 419 420 |
# File 'lib/rdf/model/list.rb', line 418 def seventh at(6) end |
- (RDF::Term) sixth
Returns the sixth element in this list.
407 408 409 |
# File 'lib/rdf/model/list.rb', line 407 def sixth at(5) end |
- (RDF::Term) slice(*args)
Returns the element at index.
281 282 283 284 285 286 287 288 |
# File 'lib/rdf/model/list.rb', line 281 def slice(*args) case argc = args.size when 2 then slice_with_start_and_length(*args) when 1 then (arg = args.first).is_a?(Range) ? slice_with_range(arg) : at(arg) when 0 then raise ArgumentError, "wrong number of arguments (0 for 1)" else raise ArgumentError, "wrong number of arguments (#{argc} for 2)" end end |
- (RDF::List) sort(&block)
Returns the elements in this list in sorted order.
618 619 620 |
# File 'lib/rdf/model/list.rb', line 618 def sort(&block) RDF::List[*super] end |
- (RDF::List) sort_by(&block)
Returns the elements in this list in sorted order.
630 631 632 |
# File 'lib/rdf/model/list.rb', line 630 def sort_by(&block) RDF::List[*super] end |
- (RDF::List) tail
Returns a list containing the last element of this list.
485 486 487 |
# File 'lib/rdf/model/list.rb', line 485 def tail (subject = last_subject).eql?(RDF.nil) ? nil : self.class.new(subject, graph) end |
- (RDF::Term) tenth
Returns the tenth element in this list.
451 452 453 |
# File 'lib/rdf/model/list.rb', line 451 def tenth at(9) end |
- (RDF::Term) third
Returns the third element in this list.
374 375 376 |
# File 'lib/rdf/model/list.rb', line 374 def third at(2) end |
- (Array) to_a
Returns the elements in this list as an array.
654 655 656 |
# File 'lib/rdf/model/list.rb', line 654 def to_a each.to_a end |
- (String) to_s
Returns a string representation of this list.
678 679 680 |
# File 'lib/rdf/model/list.rb', line 678 def to_s 'RDF::List[' + join(', ') + ']' end |
- (Set) to_set
Returns the elements in this list as a set.
665 666 667 668 |
# File 'lib/rdf/model/list.rb', line 665 def to_set require 'set' unless defined?(::Set) each.to_set end |
- (RDF::List) uniq
Returns a new list with the duplicates in this list removed.
642 643 644 |
# File 'lib/rdf/model/list.rb', line 642 def uniq RDF::List[*to_a.uniq] end |
- (RDF::List) |(other)
Returns the set union of this list and other.
The resulting list contains the elements from both lists, with no duplicates.
104 105 106 |
# File 'lib/rdf/model/list.rb', line 104 def |(other) RDF::List[*(to_a | other.to_a)] end |