lib/nydp/pair.rb in nydp-0.4.3 vs lib/nydp/pair.rb in nydp-0.4.5
- old
+ new
@@ -14,21 +14,32 @@
def cadr ; cdr.car ; end
def cdar ; car.cdr ; end
def cddr ; cdr.cdr ; end
def car= thing ; @car = thing ; @_hash = nil ; end
def cdr= thing ; @cdr = thing ; @_hash = nil ; end
- def hash ; @_hash ||= (car.hash + cdr.hash) ; end
+ # def hash ; @_hash ||= (car.hash + cdr.hash) ; end
+ def hash ; (car.hash + cdr.hash) ; end # can't cache hash of symbol, breaks when unmarshalling
def eql? other ; self == other ; end
def copy ; cons(car, cdr.copy) ; end
def + other ; copy.append other ; end
def size ; 1 + (cdr.is_a?(Nydp::Pair) ? cdr.size : 0) ; end
def inspect ; "(#{inspect_rest})" ; end
def & other ; self.class.from_list((Set.new(self) & other).to_a) ; end
def | other ; self.class.from_list((Set.new(self) | other).to_a) ; end
def - other ; self.class.from_list((Set.new(self) - other).to_a) ; end
def proper? ; Nydp::NIL.is?(cdr) || (cdr.is_a?(Nydp::Pair) && cdr.proper?) ; end
+ def index_of x
+ if x == car
+ 0
+ elsif pair?(cdr)
+ 1 + cdr.index_of(x)
+ else
+ nil
+ end
+ end
+
# returns Array of elements after calling #n2r on each element
def to_ruby list=[]
list << n2r(car)
cdr.is_a?(Nydp::Pair) ? cdr.to_ruby(list) : list
end
@@ -49,10 +60,10 @@
def self.from_list list, last=Nydp::NIL, n=0
if n >= list.size
last
else
- new list[n], from_list(list, last, n+1)
+ Nydp::Pair.new list[n], from_list(list, last, n+1)
end
end
def == other
(NIL != other) && (other.respond_to? :car) && (self.car == other.car) && (self.cdr == other.cdr)