Class: Nendo::Cell

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nendo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(car = Nil.new, cdr = Nil.new) ⇒ Cell

Returns a new instance of Cell.



90
91
92
93
# File 'lib/nendo.rb', line 90

def initialize( car = Nil.new, cdr = Nil.new )
  @car = car
  @cdr = cdr
end

Instance Attribute Details

#carObject

Returns the value of attribute car.



94
95
96
# File 'lib/nendo.rb', line 94

def car
  @car
end

#cdrObject

Returns the value of attribute cdr.



94
95
96
# File 'lib/nendo.rb', line 94

def cdr
  @cdr
end

Instance Method Details

#eachObject

Supporting iterator



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/nendo.rb', line 96

def each                    # Supporting iterator
  h = {}
  if not isNull
    it = self
    while Nil != it.class
      h[ it.hash ] = true
      # printf( "%s : %s\n", it.car, it.hash )
      yield it
      if it.cdr.is_a? Cell
        it = it.cdr
        if h.has_key?( it.hash )
          # found circular-list.
          it = Nil.new
        end
      else
        it = Nil.new
      end
    end
  end
end

#getLastAtomObject



139
140
141
142
143
144
145
# File 'lib/nendo.rb', line 139

def getLastAtom
  if self.lastAtom
    self.lastCell.cdr
  else
    Nendo::Nil.new
  end
end

#isDottedObject



120
121
122
# File 'lib/nendo.rb', line 120

def isDotted
  ((Cell != @cdr.class) and (Nil != @cdr.class))
end

#isNullObject



124
125
126
# File 'lib/nendo.rb', line 124

def isNull
  ((Nil  == @car.class) and (Nil == @cdr.class))
end

#lastAtomObject



134
135
136
137
# File 'lib/nendo.rb', line 134

def lastAtom
  lastOne = self.lastCell
  lastOne.isDotted
end

#lastCellObject



128
129
130
131
132
# File 'lib/nendo.rb', line 128

def lastCell
  lastOne = self
  self.each { |x| lastOne = x }
  lastOne
end

#lengthObject



117
# File 'lib/nendo.rb', line 117

def length() self.to_arr.length  end

#sizeObject

alias of length



118
# File 'lib/nendo.rb', line 118

def size()   self.length         end

#to_arrObject



147
148
149
150
151
152
153
# File 'lib/nendo.rb', line 147

def to_arr
  if isNull
    []
  else
    self.map {|x| x.car}
  end
end