Module: SchemeNumbersHelper
- Included in:
- SchemeNumbers
- Defined in:
- lib/lisp/interpreter/core/numbers.rb
Overview
Helper functions for SchemeNumbers
Instance Method Summary collapse
- #compare_value_arithmetic(other, oper) ⇒ Object
- #convert_to_num(other) ⇒ Object
- #divide_number(a, b) ⇒ Object
- #divide_special_convert(other) ⇒ Object
- #find_idx_numerators(other) ⇒ Object
- #get_num_denom(other) ⇒ Object
- #get_one_arg_function(other) ⇒ Object
- #num_denom_helper(other) ⇒ Object
- #num_denom_validator(temp) ⇒ Object
- #rationalize_num(num) ⇒ Object
Instance Method Details
#compare_value_arithmetic(other, oper) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/lisp/interpreter/core/numbers.rb', line 41 def compare_value_arithmetic(other, oper) raise arg_err_build 'at least 2', other.size 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
48 49 50 51 52 53 |
# File 'lib/lisp/interpreter/core/numbers.rb', line 48 def convert_to_num(other) other.each do |t| raise type_err '<number>', t.type unless check_for_num t end other.map(&:to_num) end |
#divide_number(a, b) ⇒ Object
61 62 63 64 |
# File 'lib/lisp/interpreter/core/numbers.rb', line 61 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
55 56 57 58 59 |
# File 'lib/lisp/interpreter/core/numbers.rb', line 55 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/core/numbers.rb', line 9 def find_idx_numerators(other) other[0] == '(' ? (find_bracket_idx other, 0) + 1 : 1 end |
#get_num_denom(other) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/lisp/interpreter/core/numbers.rb', line 32 def get_num_denom(other) x, temp = find_next_value other num_denom_validator temp return [x, 1] if temp.empty? y, temp = find_next_value temp[1..-1] num_denom_validator temp [x, y] end |
#get_one_arg_function(other) ⇒ Object
3 4 5 6 7 |
# File 'lib/lisp/interpreter/core/numbers.rb', line 3 def get_one_arg_function(other) raise arg_err_build 1, other.size if other.size != 1 raise type_err '<number>', other[0].type unless check_for_num other[0] other[0].to_num end |
#num_denom_helper(other) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/lisp/interpreter/core/numbers.rb', line 17 def num_denom_helper(other) if other.size == 1 other = rationalize_num other[0] if check_for_num other[0] other[0].split('/') else other = other.map { |t| t.to_s.split(%r{(\/)}) }.flatten other.delete('') get_num_denom other end end |
#num_denom_validator(temp) ⇒ Object
28 29 30 |
# File 'lib/lisp/interpreter/core/numbers.rb', line 28 def num_denom_validator(temp) raise arg_err_build 1, temp.size + 1 if (temp[0] != '/') && !temp.empty? end |
#rationalize_num(num) ⇒ Object
13 14 15 |
# File 'lib/lisp/interpreter/core/numbers.rb', line 13 def rationalize_num(num) [num.to_num.to_r.to_s] end |