class Hamster::Cons

The basic building block for constructing lists

A Cons, also known as a “cons cell”, has a “head” and a “tail”, where the head is an element in the list, and the tail is a reference to the rest of the list. This way a singly linked list can be constructed, with each `Cons` holding a single element and a pointer to the next `Cons`.

The last `Cons` instance in the chain has the {EmptyList} as its tail.

@private

Attributes

head[R]
tail[R]

Public Class Methods

new(head, tail = EmptyList) click to toggle source
# File lib/hamster/list.rb, line 1280
def initialize(head, tail = EmptyList)
  @head = head
  @tail = tail
  @size = tail.cached_size? ? tail.size + 1 : nil
end

Public Instance Methods

cached_size?() click to toggle source
# File lib/hamster/list.rb, line 1295
def cached_size?
  @size != nil
end
empty?() click to toggle source
# File lib/hamster/list.rb, line 1286
def empty?
  false
end
length()
Alias for: size
size() click to toggle source
Calls superclass method Hamster::List#size
# File lib/hamster/list.rb, line 1290
def size
  @size ||= super
end
Also aliased as: length