Class: Keisan::Functions::Range

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

Instance Attribute Summary

Attributes inherited from Keisan::Function

#arity, #name

Instance Method Summary collapse

Methods inherited from Keisan::Function

#differentiate, #unbound_variables

Constructor Details

#initializeRange

Returns a new instance of Range.



4
5
6
# File 'lib/keisan/functions/range.rb', line 4

def initialize
  super("range", ::Range.new(1,3))
end

Instance Method Details

#call(context, *args) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/keisan/functions/range.rb', line 8

def call(context, *args)
  start, finish, shift = start_finish_shift_from_args(*args)

  if shift == 1
    start_finish_range(start, finish)
  else
    start_finish_shift_range(start, finish, shift)
  end
end

#evaluate(ast_function, context = nil) ⇒ Object



23
24
25
26
27
# File 'lib/keisan/functions/range.rb', line 23

def evaluate(ast_function, context = nil)
  validate_arguments!(ast_function.children.count)
  context ||= Keisan::Context.new
  simplify(ast_function, context)
end

#simplify(ast_function, context = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/keisan/functions/range.rb', line 29

def simplify(ast_function, context = nil)
  validate_arguments!(ast_function.children.count)
  context ||= Context.new

  simplified_children = ast_function.children.map {|child| child.simplify(context)}

  if simplified_children.all? {|child| child.is_a?(Keisan::AST::Number)}
    Keisan::AST::List.new(call(context, *simplified_children.map(&:value)))
  else
    Keisan::AST::Function.new(simplified_children, "range")
  end
end

#value(ast_function, context = nil) ⇒ Object



18
19
20
21
# File 'lib/keisan/functions/range.rb', line 18

def value(ast_function, context = nil)
  validate_arguments!(ast_function.children.count)
  evaluate(ast_function, context)
end