class Ruspea::Runtime::List

Attributes

count[R]
head[R]
tail[R]

Public Class Methods

create(*items) click to toggle source
# File lib/ruspea/runtime/list.rb, line 5
def self.create(*items)
  return Nill.instance if items.length == 0

  new items[0], create(*items[1..items.length]), count: items.length
end
new(head, tail = Nill.instance, count: 0) click to toggle source
# File lib/ruspea/runtime/list.rb, line 11
def initialize(head, tail = Nill.instance, count: 0)
  @head = head
  @tail = tail
  @count = count
end

Public Instance Methods

==(other) click to toggle source
# File lib/ruspea/runtime/list.rb, line 57
def ==(other)
  return false if self.class != other.class
  head == other.head && tail == other.tail
end
car() click to toggle source
# File lib/ruspea/runtime/list.rb, line 21
def car
  head
end
cdr() click to toggle source
# File lib/ruspea/runtime/list.rb, line 25
def cdr
  tail
end
cons(el) click to toggle source
# File lib/ruspea/runtime/list.rb, line 17
def cons(el)
  self.class.new el, self, count: @count + 1
end
empty?() click to toggle source
# File lib/ruspea/runtime/list.rb, line 37
def empty?
  false
end
eq?(other) click to toggle source
# File lib/ruspea/runtime/list.rb, line 49
def eq?(other)
  self == other
end
eql?(other) click to toggle source
# File lib/ruspea/runtime/list.rb, line 53
def eql?(other)
  self == other
end
inspect() click to toggle source
# File lib/ruspea/runtime/list.rb, line 62
def inspect
  @printer ||= Ruspea::Printer.new
  @printer.call self
end
take(amount, from = self) click to toggle source
# File lib/ruspea/runtime/list.rb, line 29
def take(amount, from = self)
  return Nill.instance if amount == 0 || from.empty?

  from
    .take(amount - 1, from.tail)
    .cons(from.head)
end
to_a(list = self, array = []) click to toggle source
# File lib/ruspea/runtime/list.rb, line 41
def to_a(list = self, array = [])
  return array if list.empty?

  to_a(
    list.tail,
    array + [list.head])
end