Class: SXP::List

Inherits:
Pair show all
Includes:
Enumerable
Defined in:
lib/sxp/list.rb

Overview

A singly-linked list.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Pair

#dotted?, #proper?, #to_a

Constructor Details

- (List) initialize(elements = []) {|list| ... }

A new instance of List

Parameters:

  • (Array) elements (defaults to: [])

Yields:

  • (list)

Yield Parameters:



20
21
22
23
24
# File 'lib/sxp/list.rb', line 20

def initialize(elements = [], &block)
  @pair = nil
  unshift(*elements) unless elements.empty?
  block.call(self) if block_given?
end

Class Method Details

+ (Object) [](*elements)

Parameters:

Returns:



12
13
14
# File 'lib/sxp/list.rb', line 12

def self.[](*elements)
  self.new(elements)
end

Instance Method Details

- (Object) &(other)

Parameters:

Returns:



53
54
55
# File 'lib/sxp/list.rb', line 53

def &(other)
  self.class.new(self.to_a & other.to_a)
end

- (Object) *(times)

Parameters:

Returns:



67
68
69
70
# File 'lib/sxp/list.rb', line 67

def *(times)
  result = (self.to_a * times)
  result.is_a?(Array) ? self.class.new(result) : result
end

- (Object) +(other)

Parameters:

Returns:



75
76
77
# File 'lib/sxp/list.rb', line 75

def +(other)
  self.class.new(self.to_a + other.to_a)
end

- (Object) -(other)

Parameters:

Returns:



82
83
84
# File 'lib/sxp/list.rb', line 82

def -(other)
  self.class.new(self.to_a - other.to_a)
end

- (Object) <<(object)

Parameters:

Returns:



89
90
91
92
# File 'lib/sxp/list.rb', line 89

def <<(object)
  push(object)
  self
end

- (Object) <=>(other)

Parameters:

Returns:



97
98
99
# File 'lib/sxp/list.rb', line 97

def <=>(other)
  to_a <=> other.to_a
end

- (Object) ==(other)

Parameters:

Returns:



104
105
106
107
108
109
110
111
112
113
# File 'lib/sxp/list.rb', line 104

def ==(other)
  case other
    when List
      self.length == other.length && to_a == other.to_a
    when other.respond_to?(:to_list)
      other.to_list == self
    else
      false
  end
end

- (Object) [](*args)

Parameters:

Returns:



118
119
120
121
# File 'lib/sxp/list.rb', line 118

def [](*args)
  result = to_a[*args]
  result.is_a?(Array) ? self.class.new(result) : result # FIXME
end

- (Object) []=(*args)

Parameters:

Returns:

Raises:

  • (NotImplementedError)


126
127
128
# File 'lib/sxp/list.rb', line 126

def []=(*args)
  raise NotImplementedError # TODO
end

- (Object) assoc(object)

Parameters:

Returns:

Raises:

  • (NotImplementedError)


133
134
135
# File 'lib/sxp/list.rb', line 133

def assoc(object)
  raise NotImplementedError # TODO
end

- (Object) at(index)

Parameters:

Returns:



140
141
142
# File 'lib/sxp/list.rb', line 140

def at(index)
  to_a.at(index)
end

- (Object) clear

Returns:



146
147
148
149
# File 'lib/sxp/list.rb', line 146

def clear
  @pair = nil
  self
end

- (Object) collect!(&block)

Returns:

Raises:

  • (NotImplementedError)


153
154
155
# File 'lib/sxp/list.rb', line 153

def collect!(&block)
  raise NotImplementedError # TODO
end

- (Object) compact

Returns:



159
160
161
# File 'lib/sxp/list.rb', line 159

def compact
  self.class.new(to_a.compact)
end

- (Object) compact!

Returns:

Raises:

  • (NotImplementedError)


165
166
167
# File 'lib/sxp/list.rb', line 165

def compact!
  raise NotImplementedError # TODO
end

- (Object) concat(other)

Parameters:

Returns:

Raises:

  • (NotImplementedError)


172
173
174
# File 'lib/sxp/list.rb', line 172

def concat(other)
  raise NotImplementedError # TODO
end

- (Object) delete(object, &block)

Parameters:

Returns:

Raises:

  • (NotImplementedError)


179
180
181
# File 'lib/sxp/list.rb', line 179

def delete(object, &block)
  raise NotImplementedError # TODO
end

- (Object) delete_at(index)

Parameters:

Returns:

Raises:

  • (NotImplementedError)


186
187
188
# File 'lib/sxp/list.rb', line 186

def delete_at(index)
  raise NotImplementedError # TODO
end

- (Object) delete_if(&block)

Returns:

Raises:

  • (NotImplementedError)


192
193
194
# File 'lib/sxp/list.rb', line 192

def delete_if(&block)
  raise NotImplementedError # TODO
end

- (Enumerator) each(&block)

Returns:

  • (Enumerator)


198
199
200
201
202
203
204
205
# File 'lib/sxp/list.rb', line 198

def each(&block)
  pair = @pair
  while pair != nil
    block.call(pair.head)
    pair = pair.tail
  end
  self
end

- (Enumerator) each_index(&block)

Returns:

  • (Enumerator)


209
210
211
212
213
214
215
# File 'lib/sxp/list.rb', line 209

def each_index(&block)
  index = 0
  each do
    block.call(index)
    index += 1
  end
end

- (Boolean) empty?

Returns:

  • (Boolean)


219
220
221
# File 'lib/sxp/list.rb', line 219

def empty?
  @pair.nil?
end

- (Boolean) eql?(other)

Parameters:

Returns:

  • (Boolean)


226
227
228
229
230
231
232
# File 'lib/sxp/list.rb', line 226

def eql?(other)
  case other
    when self then true
    when List
      self.length == other.length && to_a.eql?(other.to_a)
  end
end

- (Object) fetch(*args, &block)

Parameters:

Returns:



237
238
239
# File 'lib/sxp/list.rb', line 237

def fetch(*args, &block)
  to_a.fetch(*args, &block)
end

- (Object) fill(*args, &block)

Parameters:

Returns:

Raises:

  • (NotImplementedError)


244
245
246
# File 'lib/sxp/list.rb', line 244

def fill(*args, &block)
  raise NotImplementedError # TODO
end

- (Object) first(count = nil)

Parameters:

  • (Integer) count (defaults to: nil)

Returns:



251
252
253
254
255
256
257
258
259
260
# File 'lib/sxp/list.rb', line 251

def first(count = nil)
  case
    when count.nil?
      @pair.head unless empty?
    when count == 1
      empty? ? [] : [first]
    when count > 1
      empty? ? [] : to_a.first(count)
  end
end

- (Object) flatten

Returns:

Raises:

  • (NotImplementedError)


264
265
266
# File 'lib/sxp/list.rb', line 264

def flatten
  raise NotImplementedError # TODO
end

- (Object) flatten!

Returns:

Raises:

  • (NotImplementedError)


270
271
272
# File 'lib/sxp/list.rb', line 270

def flatten!
  raise NotImplementedError # TODO
end

- (Object) head

Returns:



34
35
36
# File 'lib/sxp/list.rb', line 34

def head
  first
end

- (Object) include?(object)

Parameters:

Returns:



277
278
279
# File 'lib/sxp/list.rb', line 277

def include?(object)
  to_a.include?(object)
end

- (Object) index(object)

Parameters:

Returns:



284
285
286
# File 'lib/sxp/list.rb', line 284

def index(object)
  to_a.index(object)
end

- (Object) insert(index, *objects)

Parameters:

Returns:

Raises:

  • (NotImplementedError)


292
293
294
# File 'lib/sxp/list.rb', line 292

def insert(index, *objects)
  raise NotImplementedError # TODO
end

- (String) inspect

Returns:



28
29
30
# File 'lib/sxp/list.rb', line 28

def inspect
  "(" << map { |value| value.inspect }.join(' ') << ")"
end

- (Object) join(separator = $,)

Parameters:

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

Returns:



299
300
301
# File 'lib/sxp/list.rb', line 299

def join(separator = $,)
  to_a.join(separator)
end

- (Object) last(count = nil)

Parameters:

  • (Integer) count (defaults to: nil)

Returns:



306
307
308
309
310
311
312
313
# File 'lib/sxp/list.rb', line 306

def last(count = nil)
  case
    when count.nil?
      to_a.last
    else
      to_a.last(count)
  end
end

- (Integer) length

Returns:



317
318
319
# File 'lib/sxp/list.rb', line 317

def length
  @length ||= to_a.length
end

- (Object) map!(&block)

Returns:



323
324
325
# File 'lib/sxp/list.rb', line 323

def map!(&block)
  collect!(&block)
end

- (Integer) nitems

Returns:



329
330
331
# File 'lib/sxp/list.rb', line 329

def nitems
  to_a.nitems
end

- (Object) pack(template)

Parameters:

Returns:



336
337
338
# File 'lib/sxp/list.rb', line 336

def pack(template)
  to_a.pack(template)
end

- (Object) pop

Returns:

Raises:

  • (NotImplementedError)


342
343
344
# File 'lib/sxp/list.rb', line 342

def pop
  raise NotImplementedError # TODO
end

- (Object) push(*objects)

Parameters:

Returns:

Raises:

  • (NotImplementedError)


349
350
351
# File 'lib/sxp/list.rb', line 349

def push(*objects)
  raise NotImplementedError # TODO
end

- (Object) rassoc(key)

Parameters:

Returns:

Raises:

  • (NotImplementedError)


356
357
358
# File 'lib/sxp/list.rb', line 356

def rassoc(key)
  raise NotImplementedError # TODO
end

- (Object) reject!(&block)

Returns:

Raises:

  • (NotImplementedError)


362
363
364
# File 'lib/sxp/list.rb', line 362

def reject!(&block)
  raise NotImplementedError # TODO
end

- (Object) replace(other_list)

Parameters:

Returns:



369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/sxp/list.rb', line 369

def replace(other_list)
  case other_list
    when Pair
      @pair = other_list
    when List
      @pair = other_list.to_pair
    when Array
      @pair = nil
      unshift(*other_list)
    else
      # TODO
  end
  self
end

- (Object) rest

Returns:



46
47
48
# File 'lib/sxp/list.rb', line 46

def rest
  empty? ? false : @pair.tail
end

- (Object) reverse

Returns:



386
387
388
# File 'lib/sxp/list.rb', line 386

def reverse
  self.class.new(to_a.reverse)
end

- (Object) reverse!

Returns:

Raises:

  • (NotImplementedError)


392
393
394
# File 'lib/sxp/list.rb', line 392

def reverse!
  raise NotImplementedError # TODO
end

- (Object) reverse_each(&block)

Returns:



398
399
400
401
# File 'lib/sxp/list.rb', line 398

def reverse_each(&block)
  to_a.reverse_each(&block)
  self
end

- (Object) rindex(object)

Parameters:

Returns:



406
407
408
# File 'lib/sxp/list.rb', line 406

def rindex(object)
  to_a.rindex(object)
end

- (Object) shift

Returns:

Raises:

  • (NotImplementedError)


412
413
414
# File 'lib/sxp/list.rb', line 412

def shift
  raise NotImplementedError # TODO
end

- (Integer) size

Returns:



418
419
420
# File 'lib/sxp/list.rb', line 418

def size
  length
end

- (Object) slice(*args)

Parameters:

Returns:



425
426
427
# File 'lib/sxp/list.rb', line 425

def slice(*args)
  self[*args]
end

- (Object) slice!(*args)

Parameters:

Returns:

Raises:

  • (NotImplementedError)


432
433
434
# File 'lib/sxp/list.rb', line 432

def slice!(*args)
  raise NotImplementedError # TODO
end

- (Object) sort(&block)

Returns:



438
439
440
441
# File 'lib/sxp/list.rb', line 438

def sort(&block)
  (array = to_a).sort!(&block)
  self.class.new(array)
end

- (Object) sort!

Returns:

Raises:

  • (NotImplementedError)


445
446
447
# File 'lib/sxp/list.rb', line 445

def sort!
  raise NotImplementedError # TODO
end

- (Object) tail

Returns:



40
41
42
# File 'lib/sxp/list.rb', line 40

def tail
  rest
end

- (List) to_list

Returns:



451
452
453
# File 'lib/sxp/list.rb', line 451

def to_list
  self
end

- (Pair) to_pair

Returns:



457
458
459
# File 'lib/sxp/list.rb', line 457

def to_pair
  @pair
end

- (String) to_s

Returns:



463
464
465
# File 'lib/sxp/list.rb', line 463

def to_s
  join
end

- (Object) transpose

Returns:



469
470
471
# File 'lib/sxp/list.rb', line 469

def transpose
  self.class.new(to_a.transpose)
end

- (Object) uniq

Returns:



475
476
477
# File 'lib/sxp/list.rb', line 475

def uniq
  self.class.new(to_a.uniq)
end

- (Object) uniq!

Returns:

Raises:

  • (NotImplementedError)


481
482
483
# File 'lib/sxp/list.rb', line 481

def uniq!
  raise NotImplementedError # TODO
end

- (Object) unshift(*objects)

Parameters:

Returns:



488
489
490
491
492
493
# File 'lib/sxp/list.rb', line 488

def unshift(*objects)
  objects.reverse_each do |object|
    @pair = Pair.new(object, @pair)
  end
  self
end

- (Object) values_at(*selector)

Parameters:

Returns:



498
499
500
# File 'lib/sxp/list.rb', line 498

def values_at(*selector)
  self.class.new(to_a.values_at(*selector))
end

- (Object) |(other)

Parameters:

Returns:



60
61
62
# File 'lib/sxp/list.rb', line 60

def |(other)
  self.class.new(self.to_a | other.to_a)
end