Class: Nendo::Cell

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nendo/ruby/types.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.



84
85
86
87
# File 'lib/nendo/ruby/types.rb', line 84

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

Instance Attribute Details

#carObject

Returns the value of attribute car.



88
89
90
# File 'lib/nendo/ruby/types.rb', line 88

def car
  @car
end

#cdrObject

Returns the value of attribute cdr.



88
89
90
# File 'lib/nendo/ruby/types.rb', line 88

def cdr
  @cdr
end

Instance Method Details

#eachObject

Supporting iterator



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nendo/ruby/types.rb', line 90

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

#firstObject



149
150
151
# File 'lib/nendo/ruby/types.rb', line 149

def first
  self.car
end

#getLastAtomObject



133
134
135
136
137
138
139
# File 'lib/nendo/ruby/types.rb', line 133

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

#isDottedObject



114
115
116
# File 'lib/nendo/ruby/types.rb', line 114

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

#isNullObject



118
119
120
# File 'lib/nendo/ruby/types.rb', line 118

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

#lastAtomObject



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

def lastAtom
  lastOne = self.lastCell
  lastOne.isDotted
end

#lastCellObject



122
123
124
125
126
# File 'lib/nendo/ruby/types.rb', line 122

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

#lengthObject



111
# File 'lib/nendo/ruby/types.rb', line 111

def length() self.to_arr.length  end

#secondObject Also known as: cdar



153
154
155
# File 'lib/nendo/ruby/types.rb', line 153

def second
  self.cdr.car
end

#sizeObject

alias of length



112
# File 'lib/nendo/ruby/types.rb', line 112

def size()   self.length         end

#thirdObject Also known as: cddar



157
158
159
# File 'lib/nendo/ruby/types.rb', line 157

def third
  self.cdr.cdr.car
end

#to_arrObject



141
142
143
144
145
146
147
# File 'lib/nendo/ruby/types.rb', line 141

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