Class: Array
Overview
In Porolog, Arrays are the equivalent of lists.
Instance Method Summary collapse
-
#/(other) ⇒ Array
An Array with the Object being the head and the other Object being the tail.
-
#clean ⇒ Array
Removes Porolog processing objects.
-
#goal ⇒ Porolog::Goal
The goal that is most likely to be the goal for this array.
-
#head(head_size = 1) ⇒ Object
The head of the Array.
-
#headtail? ⇒ Boolean
Whether the Object is an Array with a head and a tail.
-
#tail(head_size = 1) ⇒ Object
The tail of the Array.
-
#type ⇒ Symbol
The type of the object (for an Array, should be :array).
-
#value(visited = []) ⇒ Array
The values of its elements.
-
#variables ⇒ Array
Embedded variables.
Instance Method Details
#/(other) ⇒ Array
Returns an Array with the Object being the head and the other Object being the tail.
146 147 148 149 150 151 152 |
# File 'lib/porolog/core_ext.rb', line 146 def /(other) if other.is_a?(Porolog::Variable) || other.is_a?(Symbol) self + [Porolog::Tail.new(other)] else self + [*other] end end |
#clean ⇒ Array
Removes Porolog processing objects.
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/porolog/core_ext.rb', line 125 def clean value.map{|element| if element.is_a?(Array) element.clean elsif element.is_a?(Porolog::Tail) Porolog::UNKNOWN_TAIL elsif element.is_a?(Porolog::Variable) nil else element.value end } end |
#goal ⇒ Porolog::Goal
Returns the goal that is most likely to be the goal for this array.
184 185 186 |
# File 'lib/porolog/core_ext.rb', line 184 def goal map{|element| element.goal if element.respond_to?(:goal) }.flatten.find{|goal| goal.is_a?(Porolog::Goal) } end |
#head(head_size = 1) ⇒ Object
Returns the head of the Array
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/porolog/core_ext.rb', line 156 def head(head_size = 1) if head_size == 1 if first == Porolog::UNKNOWN_TAIL nil else first end else self[0...head_size] end end |
#headtail? ⇒ Boolean
Returns whether the Object is an Array with a head and a tail.
179 180 181 |
# File 'lib/porolog/core_ext.rb', line 179 def headtail? length == 2 && (last.is_a?(Porolog::Tail) || last == Porolog::UNKNOWN_TAIL) end |
#tail(head_size = 1) ⇒ Object
Returns the tail of the Array
170 171 172 173 174 175 176 |
# File 'lib/porolog/core_ext.rb', line 170 def tail(head_size = 1) if last == Porolog::UNKNOWN_TAIL [*self[head_size..-2], Porolog::UNKNOWN_TAIL] else [*self[head_size..-1]] end end |
#type ⇒ Symbol
Returns the type of the object (for an Array, should be :array)
140 141 142 |
# File 'lib/porolog/core_ext.rb', line 140 def type :array end |
#value(visited = []) ⇒ Array
Returns the values of its elements.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/porolog/core_ext.rb', line 92 def value(visited = []) return self if visited.include?(self) visited = visited + [self] flat_map{|element| if element.is_a?(Porolog::Tail) tail = element.value(visited) if tail.is_a?(Array) tail elsif tail.is_a?(Porolog::Variable) || tail.is_a?(Porolog::Value) tail = tail.value(visited) if tail.is_a?(Array) tail elsif tail.is_a?(Porolog::Variable) || tail.is_a?(Porolog::Value) tail = tail.goal.variablise(tail.value(visited)) if tail.is_a?(Array) tail else [element] end else [element] end else [element] end else [element.value(visited)] end } end |
#variables ⇒ Array
Returns embedded variables.
87 88 89 |
# File 'lib/porolog/core_ext.rb', line 87 def variables map(&:variables).flatten end |