class Rake::LinkedList
structures in Rake.
Polylithic linked list structure used to implement several data
def self.cons(head, tail)
def self.cons(head, tail) new(head, tail) end
def self.empty
def self.empty self::EMPTY end
def self.make(*args)
Make a list out of the given arguments. This method is
def self.make(*args) # return an EmptyLinkedList if there are no arguments return empty if !args || args.empty? # build a LinkedList by starting at the tail and iterating # through each argument # inject takes an EmptyLinkedList to start args.reverse.inject(empty) do |list, item| list = cons(item, list) list # return the newly created list for each item in the block end end
def ==(other)
def ==(other) current = self while !current.empty? && !other.empty? return false if current.head != other.head current = current.tail other = other.tail end current.empty? && other.empty? end
def conj(item)
Polymorphically add a new element to the head of a list. The
def conj(item) self.class.cons(item, self) end
def each
def each current = self while !current.empty? yield(current.head) current = current.tail end self end
def empty?
object not empty by default
.make guards against a list being empty making any instantiated LinkedList
Is the list empty?
def empty? false end
def initialize(head, tail=EMPTY)
def initialize(head, tail=EMPTY) @head = head @tail = tail end
def inspect
def inspect items = map(&:inspect).join(", ") "LL(#{items})" end
def to_s
def to_s items = map(&:to_s).join(", ") "LL(#{items})" end