Class: ImmutableStack

Inherits:
Object
  • Object
show all
Defined in:
lib/ImmutableStack.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h, t) ⇒ ImmutableStack

Returns a new instance of ImmutableStack.



23
24
25
26
27
# File 'lib/ImmutableStack.rb', line 23

def initialize(h, t)
  @head = h
  @tail = t
  self.freeze
end

Class Method Details

.emptyObject



10
11
12
# File 'lib/ImmutableStack.rb', line 10

def self.empty()
  @@empty ||= self.new(nil, nil)
end

Instance Method Details

#each {|head| ... } ⇒ Object

Yields:

  • (head)


14
15
16
17
18
19
20
21
# File 'lib/ImmutableStack.rb', line 14

def each()
  yield(head)
  t = tail
  while t.!=(ImmutableStack.empty) do
    h, t = t.pop
    yield(h)
  end
end

#popObject



2
3
4
# File 'lib/ImmutableStack.rb', line 2

def pop()
  [@head, @tail]
end

#push(element) ⇒ Object



6
7
8
# File 'lib/ImmutableStack.rb', line 6

def push(element)
  ImmutableStack.new(element, self)
end