Class: RazyK::Node
- Inherits:
-
Object
- Object
- RazyK::Node
- Defined in:
- lib/razyk/node.rb
Overview
Combinator Expression is implemented as DAG (Directed Acyclic Graph) There is two type of node
Combinator - leaf node which has no child node.
express elemental combinator.
Pair - non leaf node which always has 2 child node.
express term of two combinators or terms
ex) In pair expression like Lisp/Scheme ““ski” => ( ( S . K ) . I ) “‘s`ki” => ( S . ( K . I ) )
S, K, I are Combinator
(S . K) is Pair
Direct Known Subclasses
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Class Method Summary collapse
-
.connect(a, b) ⇒ Object
create connectivity from a to b (a -> b) TODO: circularity check.
-
.disconnect(a, b) ⇒ Object
destroy connectivity from a to b (a -x-> b).
- .list(*args, terminator: nil, memory: {}) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(label, from = [], to = []) ⇒ Node
constructor
A new instance of Node.
- #integer ⇒ Object
- #integer? ⇒ Boolean
-
#replace(new_node) ⇒ Object
replace parent nodes’ reference of self to new_node.
-
#replace_child(a, b) ⇒ Object
replace child node from a to b.
Constructor Details
#initialize(label, from = [], to = []) ⇒ Node
Returns a new instance of Node.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/razyk/node.rb', line 19 def initialize(label, from=[], to=[]) @label = label @from = [] @to = [] from.each do |f| self.class.connect(f, self) end to.each do |t| self.class.connect(self, t) end end |
Instance Attribute Details
#from ⇒ Object (readonly)
Returns the value of attribute from.
30 31 32 |
# File 'lib/razyk/node.rb', line 30 def from @from end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
30 31 32 |
# File 'lib/razyk/node.rb', line 30 def label @label end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
30 31 32 |
# File 'lib/razyk/node.rb', line 30 def to @to end |
Class Method Details
.connect(a, b) ⇒ Object
create connectivity from a to b (a -> b) TODO: circularity check
34 35 36 37 |
# File 'lib/razyk/node.rb', line 34 def self.connect(a, b) a.to.push(b) b.from.push(a) end |
.disconnect(a, b) ⇒ Object
destroy connectivity from a to b (a -x-> b)
40 41 42 43 |
# File 'lib/razyk/node.rb', line 40 def self.disconnect(a, b) a.to.delete(b) b.from.delete(a) end |
.list(*args, terminator: nil, memory: {}) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/razyk/node.rb', line 78 def self.list(*args, terminator: nil, memory: {}) cons = lambda{|x, y| Pair.cons(x, y, memory) } term = (terminator || cons[:K, 256]) args.reverse.each do |i| term = cons[cons[:S, cons[cons[:S, :I], cons[:K, i]]], cons[:K, term]] end term end |
Instance Method Details
#as_json ⇒ Object
74 75 76 |
# File 'lib/razyk/node.rb', line 74 def as_json {name: @label.to_s} end |
#integer ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/razyk/node.rb', line 62 def integer unless integer? raise "#{self} is not a integer" end case @label when Integer @label else Integer(@label.to_s) end end |
#integer? ⇒ Boolean
58 59 60 |
# File 'lib/razyk/node.rb', line 58 def integer? @label.is_a?(Integer) or (/\A\d+\z/ =~ @label.to_s) end |
#replace(new_node) ⇒ Object
replace parent nodes’ reference of self to new_node
46 47 48 49 50 |
# File 'lib/razyk/node.rb', line 46 def replace(new_node) @from.dup.each do |f| f.replace_child(self, new_node) end end |
#replace_child(a, b) ⇒ Object
replace child node from a to b
53 54 55 56 |
# File 'lib/razyk/node.rb', line 53 def replace_child(a, b) self.class.disconnect(self, a) self.class.connect(self, b) end |