lib/rupture/seq.rb in rupture-0.1.0 vs lib/rupture/seq.rb in rupture-0.2.0
- old
+ new
@@ -1,49 +1,44 @@
module Rupture
- class Seq < Enumerable::Enumerator
+ class Seq
+ include Enumerable
include Sequence
- def initialize
- super(self)
+ def inspect
+ "(#{to_a.collect(&:inspect).join(',')})"
end
- def each
- s = self
- while s = s.seq
- yield s.first
- s = s.rest
- end
- end
-
def [](index)
nth(index)
end
def to_ary
to_a
end
- def ==(other)
+ def eql?(other)
+ return false unless other.respond_to?(:seq)
+
s = self.seq
o = other.seq
while s && o
return false if s.first != o.first
s = s.next
o = o.next
end
+
s.nil? and o.nil?
end
+ alias == eql?
- def self.empty
- @empty ||= EmptySeq.new
+ class EmptySeq < Seq
+ def seq
+ nil
+ end
end
- end
- class EmptySeq < Seq
- def seq
- nil
- end
+ Empty = EmptySeq.new
end
end
class NilClass
include Rupture::Sequence
@@ -55,8 +50,8 @@
def first
nil
end
def rest
- Rupture::Seq.empty
+ Rupture::Seq::Empty
end
end