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

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

Returns a new instance of List.

Parameters:

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

Yields:

  • (list)

Yield Parameters:



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

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

Class Method Details

.[](*elements) ⇒ Object

Parameters:

Returns:



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

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

Instance Method Details

#&(other) ⇒ Object

Parameters:

Returns:



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

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

#*(times) ⇒ Object

Parameters:

Returns:



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

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

#+(other) ⇒ Object

Parameters:

Returns:



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

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

#-(other) ⇒ Object

Parameters:

Returns:



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

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

#<<(object) ⇒ Object

Parameters:

Returns:



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

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

#<=>(other) ⇒ Object

Parameters:

Returns:



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

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

#==(other) ⇒ Object

Parameters:

Returns:



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

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

#[](*args) ⇒ Object

Parameters:

Returns:



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

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

#[]=(*args) ⇒ Object

Parameters:

Returns:

Raises:

  • (NotImplementedError)


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

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

#assoc(object) ⇒ Object

Parameters:

Returns:

Raises:

  • (NotImplementedError)


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

def assoc(object)
  raise NotImplementedError # TODO
end

#at(index) ⇒ Object

Parameters:

Returns:



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

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

#clearObject

Returns:



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

def clear
  @pair = nil
  self
end

#collect!(&block) ⇒ Object

Returns:

Raises:

  • (NotImplementedError)


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

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

#compactObject

Returns:



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

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

#compact!Object

Returns:

Raises:

  • (NotImplementedError)


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

def compact!
  raise NotImplementedError # TODO
end

#concat(other) ⇒ Object

Parameters:

Returns:

Raises:

  • (NotImplementedError)


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

def concat(other)
  raise NotImplementedError # TODO
end

#delete(object, &block) ⇒ Object

Parameters:

Returns:

Raises:

  • (NotImplementedError)


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

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

#delete_at(index) ⇒ Object

Parameters:

Returns:

Raises:

  • (NotImplementedError)


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

def delete_at(index)
  raise NotImplementedError # TODO
end

#delete_if(&block) ⇒ Object

Returns:

Raises:

  • (NotImplementedError)


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

def delete_if(&block)
  raise NotImplementedError # TODO
end

#each(&block) ⇒ Enumerator

Returns:

  • (Enumerator)


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

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

#each_index(&block) ⇒ Enumerator

Returns:

  • (Enumerator)


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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @pair.nil?
end

#eql?(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


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

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

#fetch(*args, &block) ⇒ Object

Parameters:

Returns:



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

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

#fill(*args, &block) ⇒ Object

Parameters:

Returns:

Raises:

  • (NotImplementedError)


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

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

#first(count = nil) ⇒ Object

Parameters:

  • count (Integer) (defaults to: nil)

Returns:



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

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

#flattenObject

Returns:

Raises:

  • (NotImplementedError)


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

def flatten
  raise NotImplementedError # TODO
end

#flatten!Object

Returns:

Raises:

  • (NotImplementedError)


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

def flatten!
  raise NotImplementedError # TODO
end

#headObject

Returns:



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

def head
  first
end

#include?(object) ⇒ Object

Parameters:

Returns:



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

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

#index(object) ⇒ Object

Parameters:

Returns:



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

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

#insert(index, *objects) ⇒ Object

Parameters:

Returns:

Raises:

  • (NotImplementedError)


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

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

#inspectString

Returns:



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

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

#join(separator = $,) ⇒ Object

Parameters:

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

Returns:



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

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

#last(count = nil) ⇒ Object

Parameters:

  • count (Integer) (defaults to: nil)

Returns:



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

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

#lengthInteger

Returns:



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

def length
  @length ||= to_a.length
end

#map!(&block) ⇒ Object

Returns:



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

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

#nitemsInteger

Returns:



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

def nitems
  to_a.nitems
end

#pack(template) ⇒ Object

Parameters:

Returns:



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

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

#popObject

Returns:

Raises:

  • (NotImplementedError)


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

def pop
  raise NotImplementedError # TODO
end

#push(*objects) ⇒ Object

Parameters:

Returns:

Raises:

  • (NotImplementedError)


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

def push(*objects)
  raise NotImplementedError # TODO
end

#rassoc(key) ⇒ Object

Parameters:

Returns:

Raises:

  • (NotImplementedError)


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

def rassoc(key)
  raise NotImplementedError # TODO
end

#reject!(&block) ⇒ Object

Returns:

Raises:

  • (NotImplementedError)


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

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

#replace(other_list) ⇒ Object

Parameters:

Returns:



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

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

#restObject

Returns:



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

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

#reverseObject

Returns:



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

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

#reverse!Object

Returns:

Raises:

  • (NotImplementedError)


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

def reverse!
  raise NotImplementedError # TODO
end

#reverse_each(&block) ⇒ Object

Returns:



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

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

#rindex(object) ⇒ Object

Parameters:

Returns:



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

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

#shiftObject

Returns:

Raises:

  • (NotImplementedError)


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

def shift
  raise NotImplementedError # TODO
end

#sizeInteger

Returns:



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

def size
  length
end

#slice(*args) ⇒ Object

Parameters:

Returns:



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

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

#slice!(*args) ⇒ Object

Parameters:

Returns:

Raises:

  • (NotImplementedError)


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

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

#sort(&block) ⇒ Object

Returns:



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

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

#sort!Object

Returns:

Raises:

  • (NotImplementedError)


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

def sort!
  raise NotImplementedError # TODO
end

#tailObject

Returns:



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

def tail
  rest
end

#to_listList

Returns:



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

def to_list
  self
end

#to_pairPair

Returns:



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

def to_pair
  @pair
end

#to_sString

Returns:



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

def to_s
  join
end

#transposeObject

Returns:



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

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

#uniqObject

Returns:



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

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

#uniq!Object

Returns:

Raises:

  • (NotImplementedError)


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

def uniq!
  raise NotImplementedError # TODO
end

#unshift(*objects) ⇒ Object

Parameters:

Returns:



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

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

#values_at(*selector) ⇒ Object

Parameters:

Returns:



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

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

#|(other) ⇒ Object

Parameters:

Returns:



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

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