class SXP::List

A singly-linked list.

Public Class Methods

[](*elements) click to toggle source

@param [Array] elements @return [Object]

# File lib/sxp/list.rb, line 12
def self.[](*elements)
  self.new(elements)
end
new(elements = [], &block) click to toggle source

@param [Array] elements @yield [list] @yieldparam [List] list

# 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

Public Instance Methods

&(other) click to toggle source

@param [Object] other @return [Object]

# File lib/sxp/list.rb, line 53
def &(other)
  self.class.new(self.to_a & other.to_a)
end
*(times) click to toggle source

@param [Object] times @return [Object]

# File lib/sxp/list.rb, line 67
def *(times)
  result = (self.to_a * times)
  result.is_a?(Array) ? self.class.new(result) : result
end
+(other) click to toggle source

@param [Object] other @return [Object]

# File lib/sxp/list.rb, line 75
def +(other)
  self.class.new(self.to_a + other.to_a)
end
-(other) click to toggle source

@param [Object] other @return [Object]

# File lib/sxp/list.rb, line 82
def -(other)
  self.class.new(self.to_a - other.to_a)
end
<<(object) click to toggle source

@param [Object] object @return [Object]

# File lib/sxp/list.rb, line 89
def <<(object)
  push(object)
  self
end
<=>(other) click to toggle source

@param [Object] other @return [Object]

# File lib/sxp/list.rb, line 97
def <=>(other)
  to_a <=> other.to_a
end
==(other) click to toggle source

@param [Object] other @return [Object]

# 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
[](*args) click to toggle source

@param [Array] args @return [Object]

# File lib/sxp/list.rb, line 118
def [](*args)
  result = to_a[*args]
  result.is_a?(Array) ? self.class.new(result) : result # FIXME
end
[]=(*args) click to toggle source

@param [Array] args @return [Object]

# File lib/sxp/list.rb, line 126
def []=(*args)
  raise NotImplementedError # TODO
end
assoc(object) click to toggle source

@param [Object] object @return [Object]

# File lib/sxp/list.rb, line 133
def assoc(object)
  raise NotImplementedError # TODO
end
at(index) click to toggle source

@param [Integer] index @return [Object]

# File lib/sxp/list.rb, line 140
def at(index)
  to_a.at(index)
end
clear() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 146
def clear
  @pair = nil
  self
end
collect!(&block) click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 153
def collect!(&block)
  raise NotImplementedError # TODO
end
compact() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 159
def compact
  self.class.new(to_a.compact)
end
compact!() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 165
def compact!
  raise NotImplementedError # TODO
end
concat(other) click to toggle source

@param [Object] other @return [Object]

# File lib/sxp/list.rb, line 172
def concat(other)
  raise NotImplementedError # TODO
end
delete(object, &block) click to toggle source

@param [Object] object @return [Object]

# File lib/sxp/list.rb, line 179
def delete(object, &block)
  raise NotImplementedError # TODO
end
delete_at(index) click to toggle source

@param [Integer] index @return [Object]

# File lib/sxp/list.rb, line 186
def delete_at(index)
  raise NotImplementedError # TODO
end
delete_if(&block) click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 192
def delete_if(&block)
  raise NotImplementedError # TODO
end
each(&block) click to toggle source

@return [Enumerator]

# 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
each_index(&block) click to toggle source

@return [Enumerator]

# File lib/sxp/list.rb, line 209
def each_index(&block)
  index = 0
  each do
    block.call(index)
    index += 1
  end
end
empty?() click to toggle source

@return [Boolean]

# File lib/sxp/list.rb, line 219
def empty?
  @pair.nil?
end
eql?(other) click to toggle source

@param [Object] other @return [Boolean]

# 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
fetch(*args, &block) click to toggle source

@param [Array] args @return [Object]

# File lib/sxp/list.rb, line 237
def fetch(*args, &block)
  to_a.fetch(*args, &block)
end
fill(*args, &block) click to toggle source

@param [Array] args @return [Object]

# File lib/sxp/list.rb, line 244
def fill(*args, &block)
  raise NotImplementedError # TODO
end
first(count = nil) click to toggle source

@param [Integer] count @return [Object]

# 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
flatten() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 264
def flatten
  raise NotImplementedError # TODO
end
flatten!() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 270
def flatten!
  raise NotImplementedError # TODO
end
head() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 34
def head
  first
end
include?(object) click to toggle source

@param [Object] object @return [Object]

# File lib/sxp/list.rb, line 277
def include?(object)
  to_a.include?(object)
end
index(object) click to toggle source

@param [Object] object @return [Object]

# File lib/sxp/list.rb, line 284
def index(object)
  to_a.index(object)
end
insert(index, *objects) click to toggle source

@param [Integer] index @param [Array] objects @return [Object]

# File lib/sxp/list.rb, line 292
def insert(index, *objects)
  raise NotImplementedError # TODO
end
inspect() click to toggle source

@return [String]

# File lib/sxp/list.rb, line 28
def inspect
  "(" << map { |value| value.inspect }.join(' ') << ")"
end
join(separator = $,) click to toggle source

@param [String] separator @return [Object]

# File lib/sxp/list.rb, line 299
def join(separator = $,)
  to_a.join(separator)
end
last(count = nil) click to toggle source

@param [Integer] count @return [Object]

# 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
length() click to toggle source

@return [Integer]

# File lib/sxp/list.rb, line 317
def length
  @length ||= to_a.length
end
map!(&block) click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 323
def map!(&block)
  collect!(&block)
end
nitems() click to toggle source

@return [Integer]

# File lib/sxp/list.rb, line 329
def nitems
  to_a.nitems
end
pack(template) click to toggle source

@param [Object] template @return [Object]

# File lib/sxp/list.rb, line 336
def pack(template)
  to_a.pack(template)
end
pop() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 342
def pop
  raise NotImplementedError # TODO
end
push(*objects) click to toggle source

@param [Array] objects @return [Object]

# File lib/sxp/list.rb, line 349
def push(*objects)
  raise NotImplementedError # TODO
end
rassoc(key) click to toggle source

@param [Object] key @return [Object]

# File lib/sxp/list.rb, line 356
def rassoc(key)
  raise NotImplementedError # TODO
end
reject!(&block) click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 362
def reject!(&block)
  raise NotImplementedError # TODO
end
replace(other_list) click to toggle source

@param [Object] other_list @return [Object]

# 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
rest() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 46
def rest
  empty? ? false : @pair.tail
end
reverse() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 386
def reverse
  self.class.new(to_a.reverse)
end
reverse!() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 392
def reverse!
  raise NotImplementedError # TODO
end
reverse_each(&block) click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 398
def reverse_each(&block)
  to_a.reverse_each(&block)
  self
end
rindex(object) click to toggle source

@param [Object] object @return [Object]

# File lib/sxp/list.rb, line 406
def rindex(object)
  to_a.rindex(object)
end
shift() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 412
def shift
  raise NotImplementedError # TODO
end
size() click to toggle source

@return [Integer]

# File lib/sxp/list.rb, line 418
def size
  length
end
slice(*args) click to toggle source

@param [Array] args @return [Object]

# File lib/sxp/list.rb, line 425
def slice(*args)
  self[*args]
end
slice!(*args) click to toggle source

@param [Array] args @return [Object]

# File lib/sxp/list.rb, line 432
def slice!(*args)
  raise NotImplementedError # TODO
end
sort(&block) click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 438
def sort(&block)
  (array = to_a).sort!(&block)
  self.class.new(array)
end
sort!() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 445
def sort!
  raise NotImplementedError # TODO
end
tail() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 40
def tail
  rest
end
to_list() click to toggle source

@return [List]

# File lib/sxp/list.rb, line 451
def to_list
  self
end
to_pair() click to toggle source

@return [Pair]

# File lib/sxp/list.rb, line 457
def to_pair
  @pair
end
to_s() click to toggle source

@return [String]

# File lib/sxp/list.rb, line 463
def to_s
  join
end
transpose() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 469
def transpose
  self.class.new(to_a.transpose)
end
uniq() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 475
def uniq
  self.class.new(to_a.uniq)
end
uniq!() click to toggle source

@return [Object]

# File lib/sxp/list.rb, line 481
def uniq!
  raise NotImplementedError # TODO
end
unshift(*objects) click to toggle source

@param [Array] objects @return [Object]

# File lib/sxp/list.rb, line 488
def unshift(*objects)
  objects.reverse_each do |object|
    @pair = Pair.new(object, @pair)
  end
  self
end
values_at(*selector) click to toggle source

@param [Array] selector @return [Object]

# File lib/sxp/list.rb, line 498
def values_at(*selector)
  self.class.new(to_a.values_at(*selector))
end
|(other) click to toggle source

@param [Object] other @return [Object]

# File lib/sxp/list.rb, line 60
def |(other)
  self.class.new(self.to_a | other.to_a)
end