Class: Keisan::Functions::EnumerableFunction

Inherits:
Keisan::Function show all
Defined in:
lib/keisan/functions/enumerable_function.rb

Direct Known Subclasses

Filter, Map, Reduce

Instance Attribute Summary

Attributes inherited from Keisan::Function

#arity, #name

Instance Method Summary collapse

Methods inherited from Keisan::Function

#differentiate

Constructor Details

#initialize(name) ⇒ EnumerableFunction

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



7
8
9
# File 'lib/keisan/functions/enumerable_function.rb', line 7

def initialize(name)
  super(name, -3)
end

Instance Method Details

#evaluate(ast_function, context = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



38
39
40
# File 'lib/keisan/functions/enumerable_function.rb', line 38

def simplify(ast_function, context = nil)
  evaluate(ast_function, context)
end

#unbound_variables(children, context) ⇒ Object



15
16
17
# 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) ⇒ Object



11
12
13
# File 'lib/keisan/functions/enumerable_function.rb', line 11

def value(ast_function, context = nil)
  evaluate(ast_function, context)
end