Class: Rake::LinkedList

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb

Overview

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

Direct Known Subclasses

InvocationChain, EmptyLinkedList, Scope

Defined Under Namespace

Classes: EmptyLinkedList

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#as_json, #compact_blank, #exclude?, #excluding, #in_order_of, #including, #index_by, #index_with, #many?, #maximum, #minimum, #pick, #pluck, #sole, #sum

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



8
9
10
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb', line 8

def head
  @head
end

#tailObject (readonly)

Returns the value of attribute tail.



8
9
10
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb', line 8

def tail
  @tail
end

Class Method Details

.cons(head, tail) ⇒ Object

Cons a new head onto the tail list.



73
74
75
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb', line 73

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

.emptyObject

The standard empty list class for the given LinkedList class.



78
79
80
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb', line 78

def self.empty
  self::EMPTY
end

.make(*args) ⇒ Object

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



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb', line 59

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 Method Details

#==(other) ⇒ Object

Lists are structurally equivalent.



25
26
27
28
29
30
31
32
33
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb', line 25

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) ⇒ Object

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.



12
13
14
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb', line 12

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

#eachObject

For each item in the list.



48
49
50
51
52
53
54
55
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb', line 48

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

#empty?Boolean

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

Returns:

  • (Boolean)


20
21
22
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb', line 20

def empty?
  false
end

#inspectObject

Same as to_s, but with inspected items.



42
43
44
45
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb', line 42

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

#to_sObject

Convert to string: LL(item, item…)



36
37
38
39
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/linked_list.rb', line 36

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