-
Notifications
You must be signed in to change notification settings - Fork 98
/
list.rb
984 lines (900 loc) · 27.9 KB
/
list.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
# coding: utf-8
module RDF
##
# An RDF list.
#
# @example Constructing a new list
# RDF::List[1, 2, 3]
#
# @since 0.2.3
class RDF::List
include RDF::Enumerable
include RDF::Value
include Comparable
##
# 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.
#
# @example
# RDF::List[]
# RDF::List[*(1..10)]
# RDF::List[1, 2, 3]
# RDF::List["foo", "bar"]
# RDF::List["a", 1, "b", 2, "c", 3]
#
# @param [Array<RDF::Term>] values
# @return [RDF::List]
def self.[](*values)
self.new(subject: nil, graph: nil, values: values)
end
##
# Initializes a newly-constructed list.
#
# Instantiates a new list based at `subject`, which **should** be an RDF::Node. List may be initialized using passed `values`.
#
# If a `values` initializer is set with an empty list, `subject`
# will be used as the first element in the list. Otherwise,
# if the list is not empty, `subject` identifies the first element
# of the list to which `values` are prepended yielding a new `subject`.
# Otherwise, if there are no initial `values`, and `subject` does
# not identify an existing list in `graph`, the list remains
# identified by `subject`, but will be invalid.
#
# @example add constructed list to existing graph
# l = RDF::List(values: (1, 2, 3))
# g = RDF::Graph.new << l
# g.count # => l.count
#
# @example use a transaction for block initialization
# l = RDF::List(graph: graph, wrap_transaction: true) do |list|
# list << RDF::Literal(1)
# # list.graph.rollback will rollback all list changes within this block.
# end
# list.count #=> 1
#
# @param [RDF::Resource] subject (RDF.nil)
# Subject should be an {RDF::Node}, not a {RDF::URI}. A list with an IRI head will not validate, but is commonly used to detect if a list is valid.
# @param [RDF::Graph] graph (RDF::Graph.new)
# @param [Array<RDF::Term>] values
# Any values which are not terms are coerced to `RDF::Literal`.
# @param [Boolean] wrap_transaction (false)
# Wraps the callback in a transaction, and replaces the graph with that transaction for the duraction of the callback. This has the effect of allowing any list changes to be made atomically, or rolled back.
# @yield [list]
# @yieldparam [RDF::List] list
def initialize(subject: nil, graph: nil, values: nil, wrap_transaction: false, &block)
@subject = subject || RDF.nil
@graph = graph || RDF::Graph.new
is_empty = @graph.query({subject: subject, predicate: RDF.first}).empty?
if subject && is_empty
# An empty list with explicit subject and value initializers
@subject = RDF.nil
first, *values = Array(values)
if first || values.length > 0
# Intantiate the list from values, and insert the first value using subject.
values.reverse_each {|value| self.unshift(value)}
@graph.insert RDF::Statement(subject, RDF.first, first || RDF.nil)
@graph.insert RDF::Statement(subject, RDF.rest, @subject)
end
@subject = subject
else
# Otherwise, prepend any values, which resets @subject
Array(values).reverse_each {|value| self.unshift(value)}
end
if block_given?
if wrap_transaction
old_graph = @graph
begin
Transaction.begin(@graph, graph_name: @graph.graph_name, mutable: @graph.mutable?) do |trans|
@graph = trans
case block.arity
when 1 then block.call(self)
else instance_eval(&block)
end
trans.execute if trans.mutated?
end
ensure
@graph = old_graph
end
else
case block.arity
when 1 then block.call(self)
else instance_eval(&block)
end
end
end
end
UNSET = Object.new.freeze # @private
# The canonical empty list.
NIL = RDF::List.new(subject: RDF.nil).freeze
##
# Is this a {RDF::List}?
#
# @return [Boolean]
def list?
true
end
##
# Validate the list ensuring that
# * each node is referenced exactly once (except for the head, which may have no reference)
# * rdf:rest values are all BNodes are nil
# * each subject has exactly one value for `rdf:first` and
# `rdf:rest`.
# * The value of `rdf:rest` must be either a BNode or `rdf:nil`.
# * only the list head may have any other properties
# @return [Boolean]
def valid?
li = subject
list_nodes = []
while li != RDF.nil do
return false if list_nodes.include?(li)
list_nodes << li
rest = nil
firsts = rests = 0
@graph.query({subject: li}) do |st|
return false unless st.subject.node?
case st.predicate
when RDF.first
firsts += 1
when RDF.rest
rest = st.object
return false unless rest.node? || rest == RDF.nil
rests += 1
when RDF.type
else
# It may have no other properties
return false unless li == subject
end
end
return false unless firsts == 1 && rests == 1
li = rest
end
# All elements other than the head must be referenced exactly once
return list_nodes.all? do |li|
refs = @graph.query({object: li}).count
case refs
when 0 then li == subject
when 1 then true
else false
end
end
end
# @!attribute [r] subject
# @return [RDF::Resource] the subject term of this list.
attr_reader :subject
# @!attribute [r] graph
# @return [RDF::Graph] the underlying graph storing the statements that constitute this list
attr_reader :graph
##
# @see RDF::Value#==
def ==(other)
return false if other.is_a?(RDF::Value) && !other.list?
super
end
##
# Returns the set intersection of this list and `other`.
#
# The resulting list contains the elements common to both lists, with no
# duplicates.
#
# @example
# 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[]
#
# @param [RDF::List] other
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-26
def &(other)
self.class.new(values: (to_a & other.to_a))
end
##
# Returns the set union of this list and `other`.
#
# The resulting list contains the elements from both lists, with no
# duplicates.
#
# @example
# 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]
#
# @param [RDF::List] other
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-7C
def |(other)
self.class.new(values: (to_a | other.to_a))
end
##
# Returns the concatenation of this list and `other`.
#
# @example
# RDF::List[1, 2] + RDF::List[3, 4] #=> RDF::List[1, 2, 3, 4]
#
# @param [RDF::List] other
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-2B
def +(other)
self.class.new(values: (to_a + other.to_a))
end
##
# Returns the difference between this list and `other`, removing any
# elements that appear in both lists.
#
# @example
# RDF::List[1, 2, 2, 3] - RDF::List[2] #=> RDF::List[1, 3]
#
# @param [RDF::List] other
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-2D
def -(other)
self.class.new(values: (to_a - other.to_a))
end
##
# Returns either a repeated list or a string concatenation of the
# elements in this list.
#
# @overload *(times)
# Returns a new list built of `times` repetitions of this list.
#
# @example
# RDF::List[1, 2, 3] * 2 #=> RDF::List[1, 2, 3, 1, 2, 3]
#
# @param [Integer] times
# @return [RDF::List]
#
# @overload *(sep)
# Returns the string concatenation of the elements in this list
# separated by `sep`. Equivalent to `self.join(sep)`.
#
# @example
# RDF::List[1, 2, 3] * "," #=> "1,2,3"
#
# @param [String, #to_s] sep
# @return [RDF::List]
#
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-2A
def *(int_or_str)
case int_or_str
when Integer then self.class.new(values: (to_a * int_or_str))
else join(int_or_str.to_s)
end
end
##
# Element Assignment — Sets the element at `index`, or replaces a subarray from the `start` index for `length` elements, or replaces a subarray specified by the `range` of indices.
#
# If indices are greater than the current capacity of the array, the array grows automatically. Elements are inserted into the array at `start` if length is zero.
#
# Negative indices will count backward from the end of the array. For `start` and `range` cases the starting index is just before an element.
#
# An `IndexError` is raised if a negative index points past the beginning of the array.
#
# (see #unshift).
#
# @example
# a = RDF::List.new
# a[4] = "4"; #=> [rdf:nil, rdf:nil, rdf:nil, rdf:nil, "4"]
# a[0, 3] = [ 'a', 'b', 'c' ] #=> ["a", "b", "c", rdf:nil, "4"]
# a[1..2] = [ 1, 2 ] #=> ["a", 1, 2, rdf:nil, "4"]
# a[0, 2] = "?" #=> ["?", 2, rdf:nil, "4"]
# a[0..2] = "A" #=> ["A", "4"]
# a[-1] = "Z" #=> ["A", "Z"]
# a[1..-1] = nil #=> ["A", rdf:nil]
# a[1..-1] = [] #=> ["A"]
# a[0, 0] = [ 1, 2 ] #=> [1, 2, "A"]
# a[3, 0] = "B" #=> [1, 2, "A", "B"]
#
# @overload []=(index, term)
# Replaces the element at `index` with `term`.
# @param [Integer] index
# @param [RDF::Term] term
# A non-RDF::Term is coerced to a Literal.
# @return [RDF::Term]
# @raise [IndexError]
#
# @overload []=(start, length, value)
# Replaces a subarray from the `start` index for `length` elements with `value`. Value is a {RDF::Term}, Array of {RDF::Term}, or {RDF::List}.
# @param [Integer] start
# @param [Integer] length
# @param [RDF::Term, Array<RDF::Term>, RDF::List] value
# A non-RDF::Term is coerced to a Literal.
# @return [RDF::Term, RDF::List]
# @raise [IndexError]
#
# @overload []=(range, value)
# Replaces a subarray from the `start` index for `length` elements with `value`. Value is a {RDF::Term}, Array of {RDF::Term}, or {RDF::List}.
# @param [Range] range
# @param [RDF::Term, Array<RDF::Term>, RDF::List] value
# A non-RDF::Term is coerced to a Literal.
# @return [RDF::Term, RDF::List]
# @raise [IndexError]
# @since 1.1.15
def []=(*args)
start, length = 0, 0
ary = self.to_a
value = case args.last
when Array then args.last
when RDF::List then args.last.to_a
else [args.last]
end
ret = case args.length
when 3
start, length = args[0], args[1]
ary[start, length] = value
when 2
case args.first
when Integer
raise ArgumentError, "Index form of []= takes a single term" if args.last.is_a?(Array)
ary[args.first] = args.last.is_a?(RDF::List) ? args.last.subject : args.last
when Range
ary[args.first] = value
else
raise ArgumentError, "Index form of must use an integer or range"
end
else
raise ArgumentError, "List []= takes one or two index values"
end
# Clear the list and create a new list using the existing subject
subject = @subject unless ary.empty? || @subject == RDF.nil
self.clear
new_list = RDF::List.new(subject: subject, graph: @graph, values: ary)
@subject = new_list.subject
ret # Returns inserted values
end
##
# Appends an element to the head of this list. Existing references are not updated, as the list subject changes as a side-effect.
#
# @example
# RDF::List[].unshift(1).unshift(2).unshift(3) #=> RDF::List[3, 2, 1]
#
# @param [RDF::Term, Array<RDF::Term>, RDF::List] value
# A non-RDF::Term is coerced to a Literal
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-unshift
#
def unshift(value)
value = normalize_value(value)
new_subject, old_subject = RDF::Node.new, subject
graph.insert([new_subject, RDF.first, value.is_a?(RDF::List) ? value.subject : value])
graph.insert([new_subject, RDF.rest, old_subject])
@subject = new_subject
return self
end
##
# Removes and returns the element at the head of this list.
#
# @example
# RDF::List[1,2,3].shift #=> 1
#
# @return [RDF::Term]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-shift
def shift
return nil if empty?
value = first
old_subject, new_subject = subject, rest_subject
graph.delete([old_subject, RDF.type, RDF.List])
graph.delete([old_subject, RDF.first, value])
graph.delete([old_subject, RDF.rest, new_subject])
@subject = new_subject
return value
end
##
# Empties this list
#
# @example
# RDF::List[1, 2, 2, 3].clear #=> RDF::List[]
#
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-clear
def clear
until empty?
shift
end
return self
end
##
# Appends an element to the tail of this list.
#
# @example
# RDF::List[] << 1 << 2 << 3 #=> RDF::List[1, 2, 3]
#
# @param [RDF::Term] value
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-3C-3C
def <<(value)
value = normalize_value(value)
if empty?
@subject = new_subject = RDF::Node.new
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.first, value.is_a?(RDF::List) ? value.subject : value])
graph.insert([new_subject, RDF.rest, RDF.nil])
self
end
##
# Compares this list to `other` using eql? on each component.
#
# @example
# RDF::List[1, 2, 3].eql? RDF::List[1, 2, 3] #=> true
# RDF::List[1, 2, 3].eql? [1, 2, 3] #=> true
#
# @param [RDF::List] other
# @return [Integer]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-3C-3D-3E
def eql?(other)
to_a.eql? Array(other)
end
##
# Compares this list to `other` for sorting purposes.
#
# @example
# RDF::List[1] <=> RDF::List[1] #=> 0
# RDF::List[1] <=> RDF::List[2] #=> -1
# RDF::List[2] <=> RDF::List[1] #=> 1
#
# @param [RDF::List] other
# @return [Integer]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-3C-3D-3E
def <=>(other)
to_a <=> Array(other)
end
##
# Returns `true` if this list is empty.
#
# @example
# RDF::List[].empty? #=> true
# RDF::List[1, 2, 3].empty? #=> false
#
# @return [Boolean]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-empty-3F
def empty?
graph.query({subject: subject, predicate: RDF.first}).empty?
end
##
# Returns the length of this list.
#
# @example
# RDF::List[].length #=> 0
# RDF::List[1, 2, 3].length #=> 3
#
# @return [Integer]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-length
def length
each.count
end
alias_method :size, :length
##
# Returns the index of the first element equal to `value`, or `nil` if
# no match was found.
#
# @example
# RDF::List['a', 'b', 'c'].index('a') #=> 0
# RDF::List['a', 'b', 'c'].index('d') #=> nil
#
# @param [RDF::Term] value
# @return [Integer]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-index
def index(value)
each.with_index do |v, i|
return i if v == value
end
return nil
end
##
# Returns a slice of a list.
#
# @example
# RDF::List[1, 2, 3].slice(0) #=> RDF::Literal(1),
# RDF::List[1, 2, 3].slice(0, 2) #=> RDF::List[1, 2],
# RDF::List[1, 2, 3].slice(0..2) #=> RDF::List[1, 2, 3]
#
# @return [RDF::Term]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-slice
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
alias :[] :slice
##
# @private
def slice_with_start_and_length(start, length)
self.class.new(values: to_a.slice(start, length))
end
##
# @private
def slice_with_range(range)
self.class.new(values: to_a.slice(range))
end
protected :slice_with_start_and_length
protected :slice_with_range
##
# Returns element at `index` with default.
#
# @example
# RDF::List[1, 2, 3].fetch(0) #=> RDF::Literal(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
#
# @return [RDF::Term, nil]
# @see http://ruby-doc.org/core-1.9/classes/Array.html#M000420
def fetch(index, default = UNSET)
val = at(index)
return val unless val.nil?
case
when block_given? then yield index
when !default.eql?(UNSET) then default
else raise IndexError, "index #{index} not in the list #{self.inspect}"
end
end
##
# Returns the element at `index`.
#
# @example
# RDF::List[1, 2, 3].at(0) #=> 1
# RDF::List[1, 2, 3].at(4) #=> nil
#
# @return [RDF::Term, nil]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-at
def at(index)
each.with_index { |v, i| return v if i == index }
return nil
end
alias_method :nth, :at
##
# Returns the first element in this list.
#
# @example
# RDF::List[*(1..10)].first #=> RDF::Literal(1)
#
# @return [RDF::Term]
def first
graph.first_object(subject: first_subject, predicate: RDF.first)
end
##
# Returns the second element in this list.
#
# @example
# RDF::List[*(1..10)].second #=> RDF::Literal(2)
#
# @return [RDF::Term]
def second
at(1)
end
##
# Returns the third element in this list.
#
# @example
# RDF::List[*(1..10)].third #=> RDF::Literal(4)
#
# @return [RDF::Term]
def third
at(2)
end
##
# Returns the fourth element in this list.
#
# @example
# RDF::List[*(1..10)].fourth #=> RDF::Literal(4)
#
# @return [RDF::Term]
def fourth
at(3)
end
##
# Returns the fifth element in this list.
#
# @example
# RDF::List[*(1..10)].fifth #=> RDF::Literal(5)
#
# @return [RDF::Term]
def fifth
at(4)
end
##
# Returns the sixth element in this list.
#
# @example
# RDF::List[*(1..10)].sixth #=> RDF::Literal(6)
#
# @return [RDF::Term]
def sixth
at(5)
end
##
# Returns the seventh element in this list.
#
# @example
# RDF::List[*(1..10)].seventh #=> RDF::Literal(7)
#
# @return [RDF::Term]
def seventh
at(6)
end
##
# Returns the eighth element in this list.
#
# @example
# RDF::List[*(1..10)].eighth #=> RDF::Literal(8)
#
# @return [RDF::Term]
def eighth
at(7)
end
##
# Returns the ninth element in this list.
#
# @example
# RDF::List[*(1..10)].ninth #=> RDF::Literal(9)
#
# @return [RDF::Term]
def ninth
at(8)
end
##
# Returns the tenth element in this list.
#
# @example
# RDF::List[*(1..10)].tenth #=> RDF::Literal(10)
#
# @return [RDF::Term]
def tenth
at(9)
end
##
# Returns the last element in this list.
#
# @example
# RDF::List[*(1..10)].last #=> RDF::Literal(10)
#
# @return [RDF::Term]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-last
def last
graph.first_object(subject: last_subject, predicate: RDF.first)
end
##
# Returns a list containing all but the first element of this list.
#
# @example
# RDF::List[1, 2, 3].rest #=> RDF::List[2, 3]
#
# @return [RDF::List]
def rest
(subject = rest_subject).eql?(RDF.nil) ? nil : self.class.new(subject: subject, graph: graph)
end
##
# Returns a list containing the last element of this list.
#
# @example
# RDF::List[1, 2, 3].tail #=> RDF::List[3]
#
# @return [RDF::List]
def tail
(subject = last_subject).eql?(RDF.nil) ? nil : self.class.new(subject: subject, graph: graph)
end
##
# Returns the first subject term constituting this list.
#
# This is equivalent to `subject`.
#
# @example
# RDF::List[1, 2, 3].first_subject #=> RDF::Node(...)
#
# @return [RDF::Resource]
def first_subject
subject
end
##
# @example
# RDF::List[1, 2, 3].rest_subject #=> RDF::Node(...)
#
# @return [RDF::Resource]
def rest_subject
graph.first_object(subject: subject, predicate: RDF.rest)
end
##
# Returns the last subject term constituting this list.
#
# @example
# RDF::List[1, 2, 3].last_subject #=> RDF::Node(...)
#
# @return [RDF::Resource]
def last_subject
each_subject.to_a.last # TODO: optimize this
end
##
# Yields each subject term constituting this list.
#
# @example
# RDF::List[1, 2, 3].each_subject do |subject|
# puts subject.inspect
# end
#
# @return [Enumerator]
# @see RDF::Enumerable#each
def each_subject
return enum_subject unless block_given?
subject = self.subject
yield subject
loop do
rest = graph.first_object(subject: subject, predicate: RDF.rest)
break if rest.nil? || rest.eql?(RDF.nil)
yield subject = rest
end
end
##
# Yields each element in this list.
#
# @example
# RDF::List[1, 2, 3].each do |value|
# puts value.inspect
# end
#
# @return [Enumerator]
# @see http://ruby-doc.org/core-1.9/classes/Enumerable.html
def each
return to_enum unless block_given?
each_subject do |subject|
if value = graph.first_object(subject: subject, predicate: RDF.first)
yield value # FIXME
end
end
end
##
# Yields each statement constituting this list.
#
# @example
# RDF::List[1, 2, 3].each_statement do |statement|
# puts statement.inspect
# end
#
# @return [Enumerator]
# @see RDF::Enumerable#each_statement
def each_statement(&block)
return enum_statement unless block_given?
each_subject do |subject|
graph.query({subject: subject}, &block)
end
end
alias_method :to_rdf, :each_statement
##
# Returns a string created by converting each element of this list into
# a string, separated by `sep`.
#
# @example
# RDF::List[1, 2, 3].join #=> "123"
# RDF::List[1, 2, 3].join(", ") #=> "1, 2, 3"
#
# @param [String] sep
# @return [String]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-join
def join(sep = $,)
map(&:to_s).join(sep)
end
##
# Returns the elements in this list in reversed order.
#
# @example
# RDF::List[1, 2, 3].reverse #=> RDF::List[3, 2, 1]
#
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-reverse
def reverse
self.class.new(values: to_a.reverse)
end
##
# Returns the elements in this list in sorted order.
#
# @example
# RDF::List[2, 3, 1].sort #=> RDF::List[1, 2, 3]
#
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-sort
def sort(&block)
self.class.new(values: super)
end
##
# Returns the elements in this list in sorted order.
#
# @example
# RDF::List[2, 3, 1].sort_by(&:to_i) #=> RDF::List[1, 2, 3]
#
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-sort_by
def sort_by(&block)
self.class.new(values: super)
end
##
# Returns a new list with the duplicates in this list removed.
#
# @example
# RDF::List[1, 2, 2, 3].uniq #=> RDF::List[1, 2, 3]
#
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-uniq
def uniq
self.class.new(values: to_a.uniq)
end
##
# Returns the elements in this list as an array.
#
# @example
# RDF::List[].to_a #=> []
# RDF::List[1, 2, 3].to_a #=> [RDF::Literal(1), RDF::Literal(2), RDF::Literal(3)]
#
# @return [Array]
def to_a
each.to_a
end
##
# Returns the elements in this list as a set.
#
# @example
# RDF::List[1, 2, 3].to_set #=> Set[RDF::Literal(1), RDF::Literal(2), RDF::Literal(3)]
#
# @return [Set]
def to_set
require 'set' unless defined?(::Set)
each.to_set
end
##
# Returns the subject of the list.
#
# @example
# RDF::List[].to_term #=> "RDF[:nil]"
# RDF::List[1, 2, 3].to_term #=> "RDF::Node"
#
# @return [RDF::Resource]
def to_term
subject
end
##
# Returns a string representation of this list.
#
# @example
# RDF::List[].to_s #=> "RDF::List[]"
# RDF::List[1, 2, 3].to_s #=> "RDF::List[1, 2, 3]"
#
# @return [String]
def to_s
'RDF::List[' + join(', ') + ']'
end
##
# Returns a developer-friendly representation of this list.
#
# @example
# RDF::List[].inspect #=> "#<RDF::List(_:g2163790380)>"
#
# @return [String]
def inspect
if self.equal?(NIL)
'RDF::List::NIL'
else
sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, join(', '))
end
end
private
##
# Normalizes `Array` to `RDF::List` and `nil` to `RDF.nil`.
#
# @param value [Object]
# @return [RDF::Value, Object] normalized value
def normalize_value(value)
case value
when nil then RDF.nil
when RDF::Value then value
when Array then self.class.new(subject: nil, graph: graph, values: value)
else value
end
end
end
end