Class: RazyK::Pair

Inherits:
Node
  • Object
show all
Defined in:
lib/razyk/node.rb

Overview

Pair has only two child node (car and cdr).

It represent term of combinators or terms.

Instance Attribute Summary collapse

Attributes inherited from Node

#from, #label, #to

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

connect, disconnect, #integer, #integer?, list, #replace

Constructor Details

#initialize(car, cdr) ⇒ Pair

Returns a new instance of Pair.



120
121
122
123
124
125
126
# File 'lib/razyk/node.rb', line 120

def initialize(car, cdr)
  car = Combinator.new(car) unless car.is_a?(Node)
  cdr = Combinator.new(cdr) unless cdr.is_a?(Node)
  super(:Pair, [], [car, cdr])
  @car = car
  @cdr = cdr
end

Instance Attribute Details

#carObject

Returns the value of attribute car.



127
128
129
# File 'lib/razyk/node.rb', line 127

def car
  @car
end

#cdrObject

Returns the value of attribute cdr.



127
128
129
# File 'lib/razyk/node.rb', line 127

def cdr
  @cdr
end

Class Method Details

.cons(car, cdr, mem) ⇒ Object



115
116
117
118
# File 'lib/razyk/node.rb', line 115

def self.cons(car, cdr, mem)
  n = "(#{car} #{cdr})"
  mem[n] ||= self.new(car, cdr)
end

Instance Method Details

#as_jsonObject



161
162
163
# File 'lib/razyk/node.rb', line 161

def as_json
  {name: "", children: [@car.as_json, @cdr.as_json]}
end

#cut_carObject



137
138
139
140
# File 'lib/razyk/node.rb', line 137

def cut_car
  self.class.disconnect(self, car)
  car
end

#cut_cdrObject



142
143
144
145
# File 'lib/razyk/node.rb', line 142

def cut_cdr
  self.class.disconnect(self, cdr)
  cdr
end

#inspectObject



157
158
159
# File 'lib/razyk/node.rb', line 157

def inspect
  to_s
end

#replace_child(a, b) ⇒ Object



147
148
149
150
151
152
# File 'lib/razyk/node.rb', line 147

def replace_child(a, b)
  super
  @car = b if @car == a
  @cdr = b if @cdr == a
  @to = [@car, @cdr]
end

#to_sObject



154
155
156
# File 'lib/razyk/node.rb', line 154

def to_s
  "(#{@car} #{@cdr})"
end