Class: Yadriggy::SourceCode::Cons

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/yadriggy/source_code.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head, tail = nil) ⇒ Cons

Returns a new instance of Cons.



112
113
114
115
# File 'lib/yadriggy/source_code.rb', line 112

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

Instance Attribute Details

#headObject

Returns the value of attribute head.



110
111
112
# File 'lib/yadriggy/source_code.rb', line 110

def head
  @head
end

#tailObject

Returns the value of attribute tail.



110
111
112
# File 'lib/yadriggy/source_code.rb', line 110

def tail
  @tail
end

Class Method Details

.append!(lst1, lst2) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/yadriggy/source_code.rb', line 125

def self.append!(lst1, lst2)
  if lst1 == nil
    lst2
  elsif lst2 == nil
    lst1
  else
    p = lst1
    while p.tail != nil
      p = p.tail
    end
    p.tail = lst2
    lst1
  end
end

.list(*elements) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/yadriggy/source_code.rb', line 117

def self.list(*elements)
  list = nil
  elements.reverse_each do |e|
    list = Cons.new(e, list)
  end
  list
end

Instance Method Details

#eachObject



150
151
152
153
154
155
156
# File 'lib/yadriggy/source_code.rb', line 150

def each()
  list = self
  while list != nil
    yield list.head
    list = list.tail
  end
end

#fold(acc) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/yadriggy/source_code.rb', line 158

def fold(acc)
  list = self
  while list != nil
    acc = yield acc, list.head
    list = list.tail
  end
  acc
end

#sizeObject



140
141
142
143
144
145
146
147
148
# File 'lib/yadriggy/source_code.rb', line 140

def size()
  size = 0
  list = self
  while list != nil
    list = list.tail
    size += 1
  end
  size
end