class Keisan::Functions::EnumerableFunction

Public Class Methods

new(name) click to toggle source

Filters lists/hashes: (list, variable, boolean_expression) (hash, key, value, boolean_expression)

Calls superclass method Keisan::Function::new
# File lib/keisan/functions/enumerable_function.rb, line 7
def initialize(name)
  super(name, -3)
end

Public Instance Methods

evaluate(ast_function, context = nil) click to toggle source
# File lib/keisan/functions/enumerable_function.rb, line 19
def evaluate(ast_function, context = nil)
  validate_arguments!(ast_function.children.count)
  context ||= Context.new

  operand, arguments, expression = operand_arguments_expression_for(ast_function, context)

  # Extract underlying operand for cells
  real_operand = operand.is_a?(AST::Cell) ? operand.node : operand

  case real_operand
  when AST::List
    evaluate_list(real_operand, arguments, expression, context).evaluate(context)
  when AST::Hash
    evaluate_hash(real_operand, arguments, expression, context).evaluate(context)
  else
    raise Exceptions::InvalidFunctionError.new("Unhandled first argument to #{name}: #{real_operand}")
  end
end
simplify(ast_function, context = nil) click to toggle source
# File lib/keisan/functions/enumerable_function.rb, line 38
def simplify(ast_function, context = nil)
  evaluate(ast_function, context)
end
unbound_variables(children, context) click to toggle source
Calls superclass method Keisan::Function#unbound_variables
# File lib/keisan/functions/enumerable_function.rb, line 15
def unbound_variables(children, context)
  super - Set.new(shadowing_variable_names(children).map(&:name))
end
value(ast_function, context = nil) click to toggle source
# File lib/keisan/functions/enumerable_function.rb, line 11
def value(ast_function, context = nil)
  evaluate(ast_function, context)
end

Protected Instance Methods

shadowing_variable_names(children) click to toggle source
# File lib/keisan/functions/enumerable_function.rb, line 44
def shadowing_variable_names(children)
  raise Exceptions::NotImplementedError.new
end
verify_arguments!(arguments) click to toggle source
# File lib/keisan/functions/enumerable_function.rb, line 48
def verify_arguments!(arguments)
  unless arguments.all? {|argument| argument.is_a?(AST::Variable)}
    raise Exceptions::InvalidFunctionError.new("Middle arguments to #{name} must be variables")
  end
end

Private Instance Methods

operand_arguments_expression_for(ast_function, context) click to toggle source
# File lib/keisan/functions/enumerable_function.rb, line 56
def operand_arguments_expression_for(ast_function, context)
  operand = ast_function.children[0].simplify(context)
  arguments = ast_function.children[1...-1]
  expression = ast_function.children[-1]

  verify_arguments!(arguments)

  [operand, arguments, expression]
end