lib/hamster/stack.rb in hamster-0.2.13 vs lib/hamster/stack.rb in hamster-0.3.0

- old
+ new

@@ -1,7 +1,8 @@ require 'forwardable' +require 'hamster/immutable' require 'hamster/list' module Hamster def self.stack(*items) @@ -10,12 +11,14 @@ class Stack extend Forwardable - def initialize(list) - @list = list + include Immutable + + def initialize + @list = EmptyList end def empty? @list.empty? end @@ -28,20 +31,20 @@ def top @list.head end def push(item) - self.class.new(@list.cons(item)) + transform { @list = @list.cons(item) } end def_delegator :self, :push, :<< def pop list = @list.tail if list.empty? EmptyStack else - self.class.new(list) + transform { @list = list } end end def clear EmptyStack @@ -52,15 +55,10 @@ return false unless other.class.equal?(self.class) @list.eql?(other.instance_eval{@list}) end def_delegator :self, :eql?, :== - def dup - self - end - def_delegator :self, :dup, :clone - def to_a @list.to_a end def_delegator :self, :to_a, :entries @@ -76,8 +74,8 @@ @list.inspect end end - EmptyStack = Stack.new(EmptyList) + EmptyStack = Stack.new end