Module: SchemeNumbersHelper

Included in:
SchemeNumbers
Defined in:
lib/lisp/interpreter/numbers.rb

Overview

Helper functions for SchemeNumbers

Instance Method Summary collapse

Instance Method Details

#compare_value_arithmetic(other, oper) ⇒ Object



35
36
37
38
39
40
# File 'lib/lisp/interpreter/numbers.rb', line 35

def compare_value_arithmetic(other, oper)
  raise 'Incorrect number of arguments' if other.size < 2
  other = convert_to_num other
  result = other.each_cons(2).all? { |x, y| x.public_send oper, y }
  result ? '#t' : '#f'
end

#convert_to_num(other) ⇒ Object



42
43
44
45
46
47
# File 'lib/lisp/interpreter/numbers.rb', line 42

def convert_to_num(other)
  other.each do |t|
    raise 'Invalid data type' unless check_for_number t
  end
  other.map(&:to_num)
end

#divide_number(a, b) ⇒ Object



55
56
57
58
# File 'lib/lisp/interpreter/numbers.rb', line 55

def divide_number(a, b)
  return a / b if (a / b).to_i.to_f == a / b.to_f
  a / b.to_f
end

#divide_special_convert(other) ⇒ Object



49
50
51
52
53
# File 'lib/lisp/interpreter/numbers.rb', line 49

def divide_special_convert(other)
  other = convert_to_num other
  return [0] if other.size == 1 && other[0] == 0.0
  other
end

#find_idx_numerators(other) ⇒ Object



9
10
11
# File 'lib/lisp/interpreter/numbers.rb', line 9

def find_idx_numerators(other)
  other[0] == '(' ? (find_bracket_idx other, 0) + 1 : 1
end

#get_num_denom(other) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/lisp/interpreter/numbers.rb', line 26

def get_num_denom(other)
  num, other = find_next_value other
  raise 'Invalid data type' unless check_for_number num
  return [num, 1] if other.empty?
  denom, other = find_next_value other
  raise 'Incorrect number of arguments' unless other.empty?
  [num, denom]
end

#get_one_arg_function(other) ⇒ Object



3
4
5
6
7
# File 'lib/lisp/interpreter/numbers.rb', line 3

def get_one_arg_function(other)
  raise 'Incorrect number of arguments' if other.size != 1
  raise 'Invalid data type' unless check_for_number other[0]
  other[0].to_num
end

#num_denom_helper(other) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lisp/interpreter/numbers.rb', line 13

def num_denom_helper(other)
  raise 'Incorrect number of arguments' if other.empty?
  if other.size == 1
    other = other[0].split('/')
  else
    _, temp = find_next_value other
    raise 'Incorrect number of arguments' unless temp[0] == '/' || temp.empty?
    i = find_idx_numerators other
    other.delete_at(i)
  end
  other
end