Class: RDF::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Resource
Defined in:
lib/rdf/model/list.rb

Overview

An RDF list.

Examples:

Constructing a new list

RDF::List[1, 2, 3]

Since:

Constant Summary

NIL =

The canonical empty list.

Since:

  • 0.2.3

RDF::List.new(RDF.nil).freeze

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Resource

new, #resource?

Methods included from Term

#constant?, #variable?

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

#alias_method

Methods included from Countable

#count

Constructor Details

- (List) initialize(subject = nil, graph = nil, values = nil) {|list| ... }

Initializes a newly-constructed list.

Parameters:

Yields:

  • (list)

Yield Parameters:

Since:

  • 0.2.3



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.

Returns:

Since:

  • 0.2.3



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.

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[]
RDF::List[*(1..10)]
RDF::List[1, 2, 3]
RDF::List["foo", "bar"]
RDF::List["a", 1, "b", 2, "c", 3]

Parameters:

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2] & RDF::List[1, 2]       #=> RDF::List[1, 2]
RDF::List[1, 2] & RDF::List[2, 3]       #=> RDF::List[2]
RDF::List[1, 2] & RDF::List[3, 4]       #=> RDF::List[]

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



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.

Overloads:

  • - (RDF::List) *(times)

    Returns a new list built of times repetitions of this list.

    Examples:

    RDF::List[1, 2, 3] * 2                #=> RDF::List[1, 2, 3, 1, 2, 3]
    

    Parameters:

    • (Integer) times

    Returns:

  • - (RDF::List) *(sep)

    Returns the string concatenation of the elements in this list separated by sep. Equivalent to self.join(sep).

    Examples:

    RDF::List[1, 2, 3] * ","              #=> "1,2,3"
    

    Parameters:

    • (String, #to_s) sep

    Returns:

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2] + RDF::List[3, 4]       #=> RDF::List[1, 2, 3, 4]

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 2, 3] - RDF::List[2]    #=> RDF::List[1, 3]

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[] << 1 << 2 << 3              #=> RDF::List[1, 2, 3]

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1] <=> RDF::List[1]           #=> 0
RDF::List[1] <=> RDF::List[2]           #=> -1
RDF::List[2] <=> RDF::List[1]           #=> 1

Parameters:

Returns:

  • (Integer)

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3][0]                   #=> 1

Parameters:

  • (Integer) index

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].at(0)                #=> 1

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].each do |value|
  puts value.inspect
end

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].each_statement do |statement|
  puts statement.inspect
end

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].each_subject do |subject|
  puts subject.inspect
end

Returns:

See Also:

  • Enumerable#each

Since:

  • 0.2.3



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.

Examples:

RDF::List[*(1..10)].eighth              #=> 8

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[].empty?                      #=> true
RDF::List[1, 2, 3].empty?               #=> false

Returns:

  • (Boolean)

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].fetch(0)             #=> 1
RDF::List[1, 2, 3].fetch(4)             #=> IndexError
RDF::List[1, 2, 3].fetch(4, nil)        #=> nil
RDF::List[1, 2, 3].fetch(4) { |n| n*n } #=> 16

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[*(1..10)].fifth               #=> 5

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[*(1..10)].first               #=> 1

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].first_subject        #=> RDF::Node(...)

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[*(1..10)].fourth              #=> 4

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List['a', 'b', 'c'].index('a')     #=> 0
RDF::List['a', 'b', 'c'].index('d')     #=> nil

Parameters:

Returns:

  • (Integer)

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[].inspect                     #=> "#<RDF::List(_:g2163790380)>"

Returns:

  • (String)

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].join                 #=> "123"
RDF::List[1, 2, 3].join(", ")           #=> "1, 2, 3"

Parameters:

  • (String) sep (defaults to: $,)

Returns:

  • (String)

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].last                 #=> 3

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].last_subject         #=> RDF::Node(...)

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[].length                      #=> 0
RDF::List[1, 2, 3].length               #=> 3

Returns:

  • (Integer)

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[*(1..10)].ninth               #=> 9

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].rest                 #=> RDF::List[2, 3]

Returns:

Since:

  • 0.2.3



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

Examples:

RDF::List[1, 2, 3].rest_subject         #=> RDF::Node(...)

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].reverse              #=> RDF::List[3, 2, 1]

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[*(1..10)].second              #=> 2

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[*(1..10)].seventh             #=> 7

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[*(1..10)].sixth               #=> 6

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].at(0)                #=> 1

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[2, 3, 1].sort                 #=> RDF::List[1, 2, 3]

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[2, 3, 1].sort_by(&:to_i)      #=> RDF::List[1, 2, 3]

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].tail                 #=> RDF::List[3]

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[*(1..10)].tenth               #=> 10

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[*(1..10)].third               #=> 3

Returns:

Since:

  • 0.2.3



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.

Examples:

RDF::List[].to_a                        #=> []
RDF::List[1, 2, 3].to_a                 #=> [1, 2, 3]

Returns:

  • (Array)

Since:

  • 0.2.3



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.

Examples:

RDF::List[].to_s                        #=> "RDF::List[]"
RDF::List[1, 2, 3].to_s                 #=> "RDF::List[1, 2, 3]"

Returns:

  • (String)

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 3].to_set               #=> Set[1, 2, 3]

Returns:

  • (Set)

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2, 2, 3].uniq              #=> RDF::List[1, 2, 3]

Returns:

See Also:

Since:

  • 0.2.3



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.

Examples:

RDF::List[1, 2] | RDF::List[1, 2]       #=> RDF::List[1, 2]
RDF::List[1, 2] | RDF::List[2, 3]       #=> RDF::List[1, 2, 3]
RDF::List[1, 2] | RDF::List[3, 4]       #=> RDF::List[1, 2, 3, 4]

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



104
105
106
# File 'lib/rdf/model/list.rb', line 104

def |(other)
  RDF::List[*(to_a | other.to_a)]
end