123456789_123456789_123456789_123456789_123456789_

Class: Rake::LinkedList

Relationships & Source Files
Namespace Children
Classes:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Enumerable
Inherits: Object
Defined in: lib/rake/linked_list.rb

Overview

Polylithic linked list structure used to implement several data structures in ::Rake.

Constant Summary

Class Method Summary

Instance Attribute Summary

  • #empty? ⇒ Boolean readonly

    Is the list empty? .make guards against a list being empty making any instantiated LinkedList object not empty by default You should consider overriding this method if you implement your own .make method.

  • #head readonly
  • #tail readonly

Instance Method Summary

  • #==(other)

    Lists are structurally equivalent.

  • #conj(item)

    Polymorphically add a new element to the head of a list.

  • #each

    For each item in the list.

  • #inspect

    Same as #to_s, but with inspected items.

  • #to_s

    Convert to string: LL(item, item…).

Constructor Details

.new(head, tail = EMPTY) ⇒ LinkedList (protected)

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 83

def initialize(head, tail=EMPTY)
  @head = head
  @tail = tail
end

Class Method Details

.cons(head, tail)

Cons a new head onto the tail list.

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 72

def self.cons(head, tail)
  new(head, tail)
end

.empty

The standard empty list class for the given LinkedList class.

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 77

def self.empty
  self::EMPTY
end

.make(*args)

Make a list out of the given arguments. This method is polymorphic

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 58

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

Instance Attribute Details

#empty?Boolean (readonly)

Is the list empty? .make guards against a list being empty making any instantiated LinkedList object not empty by default You should consider overriding this method if you implement your own .make method

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 19

def empty?
  false
end

#head (readonly)

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 7

attr_reader :head, :tail

#tail (readonly)

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 7

attr_reader :head, :tail

Instance Method Details

#==(other)

Lists are structurally equivalent.

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 24

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

#conj(item)

Polymorphically add a new element to the head of a list. The type of head node will be the same list type as the tail.

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 11

def conj(item)
  self.class.cons(item, self)
end

#each

For each item in the list.

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 47

def each
  current = self
  while !current.empty?
    yield(current.head)
    current = current.tail
  end
  self
end

#inspect

Same as #to_s, but with inspected items.

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 41

def inspect
  items = map(&:inspect).join(", ")
  "LL(#{items})"
end

#to_s

Convert to string: LL(item, item…)

[ GitHub ]

  
# File 'lib/rake/linked_list.rb', line 35

def to_s
  items = map(&:to_s).join(", ")
  "LL(#{items})"
end