class Array

In Porolog, Arrays are the equivalent of lists.

Public Instance Methods

/(other) click to toggle source

@param other [Object] the Object which is to be the tail @return [Array] an Array with the Object being the head and the other Object being the tail.

# 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() click to toggle source

Removes Porolog processing objects. @return [Array] the values of its elements with variables replaced by nil and Tails replaced by UNKNOWN_TAIL.

# 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() click to toggle source

@return [Porolog::Goal] the goal that is most likely to be the goal for this array.

# 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) click to toggle source

@param head_size [Integer] specifies the size of the head @return [Object] the head of the Array

# 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?() click to toggle source

@return [Boolean] whether the Object is an Array with a head and a tail.

# 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) click to toggle source

@param head_size [Integer] specifies the size of the head @return [Object] the tail of the Array

# 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() click to toggle source

@return [Symbol] the type of the object (for an Array, should be :array)

# File lib/porolog/core_ext.rb, line 140
def type
  :array
end
value(visited = []) click to toggle source

@return [Array] the values of its elements.

# 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() click to toggle source

@return [Array] embedded variables.

# File lib/porolog/core_ext.rb, line 87
def variables
  map(&:variables).flatten
end