Class: Crisp::Functions::Arithmetic

Inherits:
Object
  • Object
show all
Defined in:
lib/crisp/functions/arithmetic.rb

Overview

Defining arithemtic crisp functions

Class Method Summary collapse

Class Method Details

.load(current_env) ⇒ Object

load the functions and bind them into the given environment



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/crisp/functions/arithmetic.rb', line 6

def self.load(current_env)

  # +
  # sum up all arguments
  #
  #  (+ 1 2 3)
  #  => 6
  Function.new do
    args_evaled.inject(:+)
  end.bind('+', current_env)

  # -
  # substract all arguments
  #
  #  (- 5 2 1)
  #  => 2
  Function.new do
    args_evaled.inject(:-)
  end.bind('-', current_env)

  # *
  # multiply all arguments
  #
  #  (* 2 3 4)
  #  => 24
  Function.new do
    args_evaled.inject(:*)
  end.bind('*', current_env)

  # /
  # divide all arguments
  #
  #  (/ 20 2 2)
  #  => 5
  Function.new do
    args_evaled.inject(:/)
  end.bind('/', current_env)

  # =
  # compare 2 arguments for equality
  #
  #  (= 1 2)
  #  => false
  Function.new do
    validate_args_count(2, args.size)
    values = args_evaled
    values[0] == values[1]
  end.bind('=', current_env)

  # >
  # compare 2 arguments for being greater than the other
  #
  #  (> 2 1)
  #  => true
  Function.new do
    validate_args_count(2, args.size)
    values = args_evaled
    values[0] > values[1]
  end.bind('>', current_env)

  # <
  # compare 2 arguments for being less than the other
  #
  #  (< 2 1)
  #  => false
  Function.new do
    validate_args_count(2, args.size)
    values = args_evaled
    values[0] < values[1]
  end.bind('<', current_env)

end