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.



12
13
14
15
# File 'lib/ImmutableStack.rb', line 12

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

Class Method Details

.emptyObject



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

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

Instance Method Details

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

Yields:

  • (head)


5
6
7
8
9
10
11
# File 'lib/ImmutableStack.rb', line 5

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

#popObject



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

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

#push(element) ⇒ Object



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

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