Class: Array

Inherits:
Object show all
Defined in:
lib/porolog/core_ext.rb

Overview

In Porolog, Arrays are the equivalent of lists.

Instance Method Summary collapse

Instance Method Details

#/(other) ⇒ Array

Returns an Array with the Object being the head and the other Object being the tail.

Parameters:

  • other (Object)

    the Object which is to be the tail

Returns:

  • (Array)

    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

#cleanArray

Removes Porolog processing objects.

Returns:

  • (Array)

    the values of its elements with variables replaced by nil and Tails replaced by UNKNOWN_TAIL.



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

#goalPorolog::Goal

Returns the goal that is most likely to be the goal for this array.

Returns:

  • (Porolog::Goal)

    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

Parameters:

  • head_size (Integer) (defaults to: 1)

    specifies the size of the head

Returns:

  • (Object)

    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.

Returns:

  • (Boolean)

    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

Parameters:

  • head_size (Integer) (defaults to: 1)

    specifies the size of the head

Returns:

  • (Object)

    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

#typeSymbol

Returns the type of the object (for an Array, should be :array)

Returns:

  • (Symbol)

    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.

Returns:

  • (Array)

    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

#variablesArray

Returns embedded variables.

Returns:

  • (Array)

    embedded variables.



87
88
89
# File 'lib/porolog/core_ext.rb', line 87

def variables
  map(&:variables).flatten
end