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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/keisan/functions/enumerable_function.rb', line 15

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)

  case operand
  when AST::List
    evaluate_list(operand, arguments, expression, context)
  when AST::Hash
    evaluate_hash(operand, arguments, expression, context)
  else
    raise Exceptions::InvalidFunctionError.new("Unhandled first argument to #{name}: #{operand}")
  end
end

#simplify(ast_function, context = nil) ⇒ Object



31
32
33
# File 'lib/keisan/functions/enumerable_function.rb', line 31

def simplify(ast_function, context = nil)
  evaluate(ast_function, context)
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