lib/ImmutableQueue.rb in maroon-0.6.5 vs lib/ImmutableQueue.rb in maroon-0.7.0
- old
+ new
@@ -1,50 +1,44 @@
class ImmutableQueue
-
-def push(element)
+ def push(element)
b = (back or ImmutableStack.empty)
-ImmutableQueue.new(front, b.push(element))
+ ImmutableQueue.new(front, b.push(element))
+ end
- end
-
-def pop()
+ def pop()
f, b = front, back
-if (f == ImmutableStack.empty) then
- until (b == ImmutableStack.empty) do
- (e, b = b.pop
- f = f.push(e))
+ if (f == ImmutableStack.empty) then
+ until (b == ImmutableStack.empty) do
+ (e, b = b.pop
+ f = f.push(e))
+ end
+ end
+ head, f = f.pop
+ if (f == b) then
+ [head, ImmutableQueue.empty]
+ else
+ [head, ImmutableQueue.new(f, b)]
+ end
end
-end
-head, f = f.pop
-if (f == b) then
- [head, ImmutableQueue.empty]
-else
- [head, ImmutableQueue.new(f, b)]
-end
- end
-
-def self.empty()
+ def self.empty()
@@empty ||= ImmutableQueue.new(ImmutableStack.empty, ImmutableStack.empty)
- end
-
-def push_array(arr)
+ end
+
+ def push_array(arr)
q = self
-arr.each { |i| q = q.push(i) } if arr
-q
+ arr.each { |i| q = q.push(i) } if arr
+ q
+ end
- end
-
- private
-
-def initialize(front,back)
+ private
+ def initialize(front, back)
@front = (front or ImmutableStack.empty)
-@back = (back or ImmutableStack.empty)
-self.freeze
+ @back = (back or ImmutableStack.empty)
+ self.freeze
+ end
- end
-attr_reader :front
- attr_reader :back
-
-
-
- end
+ attr_reader :front
+ attr_reader :back
+
+
+end
\ No newline at end of file