class Nydp::Pair

Constants

NIL

Attributes

car[R]
cdr[R]

Public Class Methods

from_list(list, last=nil, n=list.size-1) click to toggle source
# File lib/nydp/pair.rb, line 151
def self.from_list list, last=nil, n=list.size-1
  while (n >= 0)
    last = cons(list[n], last)
    n -= 1
  end

  last
end
new(car, cdr) click to toggle source
# File lib/nydp/pair.rb, line 8
def initialize car, cdr
  @car, @cdr = car, cdr
end
parse_list(list) click to toggle source

this breaks everything even though it seems like the logical thing to do… def to_ary

to_a

end

# File lib/nydp/pair.rb, line 143
def self.parse_list list
  if list.slice(-2) == :"."
    from_list(list[0...-2], list.slice(-1))
  else
    from_list list
  end
end

Public Instance Methods

&(other ;) click to toggle source
# File lib/nydp/pair.rb, line 24
def &    other ; self.class.from_list((Set.new(self) & Array(other)).to_a)    ; end
+(other ;) click to toggle source
# File lib/nydp/pair.rb, line 20
def +    other ; copy_append other                          ; end
-(other ;) click to toggle source
# File lib/nydp/pair.rb, line 26
def -    other ; self.class.from_list((Set.new(self) - Array(other)).to_a)    ; end
==(other) click to toggle source
# File lib/nydp/pair.rb, line 160
def == other
  (NIL != other) && (other.respond_to? :car) && (self.car == other.car) && (self.cdr == other.cdr)
end
[](i ;) click to toggle source
# File lib/nydp/pair.rb, line 23
def []       i ; nth i                                      ; end
caar() click to toggle source
# File lib/nydp/pair.rb, line 13
def caar       ; car.car                                    ; end
cadr() click to toggle source
# File lib/nydp/pair.rb, line 14
def cadr       ; cdr.car                                    ; end
car=(thing ;) click to toggle source
# File lib/nydp/pair.rb, line 17
def car= thing ; @car = thing                               ; end
cdar() click to toggle source
# File lib/nydp/pair.rb, line 15
def cdar       ; car.cdr                                    ; end
cddr() click to toggle source
# File lib/nydp/pair.rb, line 16
def cddr       ; cdr.cdr                                    ; end
cdr=(thing ;) click to toggle source
# File lib/nydp/pair.rb, line 18
def cdr= thing ; @cdr = thing                               ; end
compile_to_ruby(indent, srcs, opts=nil) click to toggle source
# File lib/nydp/pair.rb, line 85
def compile_to_ruby indent, srcs, opts=nil
  a,x = [], self
  while x.is_a?(Nydp::Pair)
    a << x.car.compile_to_ruby("", srcs)
    x = x.cdr
  end

  if !x || (x.is_a?(Nydp::Literal) && !x.expression)
    "#{indent}list(" + a.join(", ") + ")"
  else
    "#{indent}Nydp::Pair.from_list([" + a.join(", ") + "], #{x.compile_to_ruby "", srcs})"
  end
end
copy() click to toggle source
# File lib/nydp/pair.rb, line 45
def copy
  a    = last = cons
  x    = self
  while (x.is_a? Nydp::Pair)
    last = last.cdr = cons(x.car)
    x    = x.cdr
  end

  last.cdr = x
  a.cdr
end
copy_append(lastcdr) click to toggle source
# File lib/nydp/pair.rb, line 57
def copy_append lastcdr
  a    = last = cons
  x    = self
  while (x.is_a? Nydp::Pair)
    last = last.cdr = cons(x.car)
    x    = x.cdr
  end

  last.cdr = lastcdr
  a.cdr
end
each() { |car| ... } click to toggle source
# File lib/nydp/pair.rb, line 164
def each &block
  xs = self
  while xs
    yield xs.car
    xs = xs.cdr
  end
end
eql?(other ;) click to toggle source
# File lib/nydp/pair.rb, line 19
def eql? other ; self == other                              ; end
hash() click to toggle source

can’t cache hash of symbol, breaks when unmarshalling

# File lib/nydp/pair.rb, line 34
def hash
  a    = 0
  x    = self
  while (x.is_a? Nydp::Pair)
    a = a + x.car.hash
    x = x.cdr
  end

  a + x.hash
end
index_of(x) click to toggle source
# File lib/nydp/pair.rb, line 107
def index_of x
  if x == car
    0
  elsif pair?(cdr)
    1 + cdr.index_of(x)
  else
    nil
  end
end
inspect() click to toggle source
# File lib/nydp/pair.rb, line 22
def inspect    ; "(#{inspect_rest})"                        ; end
inspect_rest() click to toggle source
# File lib/nydp/pair.rb, line 238
def inspect_rest
  res = [car._nydp_inspect]
  it = cdr
  while it
    if it.is_a?(self.class)
      res << it.car._nydp_inspect
      it = it.cdr
    else
      res << "."
      res << it._nydp_inspect
      it = nil
    end
  end
  res.compact.join " "
end
join(str) click to toggle source
# File lib/nydp/pair.rb, line 81
def join str
  to_a.join(str)
end
map() { |car)| ... } click to toggle source
# File lib/nydp/pair.rb, line 69
def map
  a    = last = cons
  x    = self
  while (x.is_a? Nydp::Pair)
    last = last.cdr = cons(yield x.car)
    x    = x.cdr
  end

  last.cdr = yield(x) if x
  a.cdr
end
nth(n) click to toggle source
# File lib/nydp/pair.rb, line 99
def nth n
  xs = self
  while n > 0
    xs, n = xs.cdr, n-1
  end
  xs.car
end
nydp_type() click to toggle source
# File lib/nydp/pair.rb, line 12
def nydp_type  ; :pair                                      ; end
proper?() click to toggle source
# File lib/nydp/pair.rb, line 27
def proper?    ; (!cdr) || (cdr.is_a?(Nydp::Pair) && cdr.proper?)             ; end
size() click to toggle source
# File lib/nydp/pair.rb, line 21
def size       ; 1 + (cdr.is_a?(Nydp::Pair) ? cdr.size : 0) ; end
to_a(list=[]) click to toggle source

returns Array of elements as they are

# File lib/nydp/pair.rb, line 129
def to_a list=[]
  x = self
  while x.is_a?(Nydp::Pair)
    list << x.car
    x = x.cdr
  end
  list
end
to_ruby(list=[], pair=self) click to toggle source

returns Array of elements after calling n2r on each element

# File lib/nydp/pair.rb, line 118
def to_ruby list=[], pair=self
  list << n2r(pair.car)
  while(pair.cdr.is_a?(Nydp::Pair))
    pair = pair.cdr
    list << n2r(pair.car)
  end

  list
end
to_s() click to toggle source
# File lib/nydp/pair.rb, line 172
def to_s
  if (car == :quote)
    if !cdr.cdr
      "'#{cdr.car.to_s}"
    else
      "'#{cdr.to_s}"
    end
  elsif (car == :"brace-list")
    if !cdr
      "{}"
    else
      "{ #{cdr.to_s_rest} }"
    end
  elsif (car == :"percent-syntax")
    cdr.to_a.compact.join("%")
  elsif (car == :"colon-syntax")
    cdr.to_a.compact.join(":")
  elsif (car == :"dot-syntax")
    cdr.to_a.compact.join(".")
  elsif (car == :"prefix-list")
    "#{cdr.car.to_s}#{cdr.cdr.car.to_s}"
  elsif (car == :quasiquote)
    if !cdr.cdr
      "`#{cdr.car.to_s}"
    else
      "`#{cdr.to_s}"
    end
  elsif (car == :unquote)
    if !cdr.cdr
      ",#{cdr.car.to_s}"
    else
      ",#{cdr.to_s}"
    end
  elsif (car == :"unquote-splicing")
    if !cdr.cdr
      ",@#{cdr.car.to_s}"
    else
      ",@#{cdr.to_s}"
    end
  else
    "(#{to_s_rest})"
  end
end
to_s_car() click to toggle source
# File lib/nydp/pair.rb, line 216
def to_s_car
  if (!car)
    "nil"
  elsif car.is_a?(String)
    car.inspect
  elsif car.is_a?(Symbol)
    car._nydp_inspect
  else
    car.to_s
  end
end
to_s_rest() click to toggle source
# File lib/nydp/pair.rb, line 228
def to_s_rest
  cdr_s = if cdr.is_a?(self.class)
            cdr.to_s_rest
          elsif cdr
            ". #{cdr.to_s}"
          end

  [to_s_car, cdr_s].compact.join " "
end
|(other ;) click to toggle source
# File lib/nydp/pair.rb, line 25
def |    other ; self.class.from_list((Set.new(self) | Array(other)).to_a)    ; end