-
Notifications
You must be signed in to change notification settings - Fork 6
/
operators.cljc
152 lines (121 loc) · 4.22 KB
/
operators.cljc
1
2
3
4
5
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
(ns axel-f.excel.operators
(:require [axel-f.excel.coerce :as coerce]))
(defn add*
"Returns the sum of given nums."
[^{:doc "Number or string that can be coerced to number."} x
& [^{:doc "Number or string that can be coerced to number."} y]]
(let [x (coerce/excel-number x)]
(if (some? y)
(+ x (coerce/excel-number y))
x)))
(def add #'add*)
(defn sub*
"If no ys are supplied, returns the negation of x, else subtracts the y from x and returns the result."
[^{:doc "Number or string that can be coerced to number."} x
& [^{:doc "Number or string that can be coerced to number."} y]]
(if (some? y)
(- (coerce/excel-number x)
(coerce/excel-number y))
(* -1 (coerce/excel-number x))))
(def sub #'sub*)
(defn mult*
"Returns the product of nums."
[^{:doc "Number or string that can be coerced to number."} x
^{:doc "Number or string that can be coerced to number."} y]
(* (coerce/excel-number x)
(coerce/excel-number y)))
(def mult #'mult*)
(defn div*
"Returns numerator divided by the denominator"
[^{:doc "Number or string that can be coerced to number."} x
^{:doc "Number or string that can be coerced to number."} y]
(/ (coerce/excel-number x)
(coerce/excel-number y)))
(def div #'div*)
(defn less*
"Returns non-nil if nums are in monotonically increasing order, otherwise false."
[^{:doc "Number or string that can be coerced to number."} x
^{:doc "Number or string that can be coerced to number."} y]
(< (coerce/excel-number x)
(coerce/excel-number y)))
(def less #'less*)
(defn more*
"Returns non-nil if nums are in monotonically decreasing order, otherwise false."
[^{:doc "Number or string that can be coerced to number."} x
^{:doc "Number or string that can be coerced to number."} y]
(> (coerce/excel-number x)
(coerce/excel-number y)))
(def more #'more*)
(defn less-or-eq*
"Returns non-nil if nums are in monotonically non-decreasing order, otherwise false."
[^{:doc "Number or string that can be coerced to number."} x
^{:doc "Number or string that can be coerced to number."} y]
(<= (coerce/excel-number x)
(coerce/excel-number y)))
(def less-or-eq #'less-or-eq*)
(defn more-or-eq*
"Returns non-nil if nums are in monotonically non-increasing order, otherwise false."
[^{:doc "Number or string that can be coerced to number."} x
^{:doc "Number or string that can be coerced to number."} y]
(>= (coerce/excel-number x)
(coerce/excel-number y)))
(def more-or-eq #'more-or-eq*)
(defn not-eq*
"Returns true if object are not equal, otherwise false."
[^{:doc "Object to check."} x
^{:doc "Object to check."} y]
(not= x y))
(def not-eq #'not-eq*)
(defn eq*
"Returns true if objects are equal, otherwise false."
[^{:doc "Object to check."} x
^{:doc "Object to check."} y]
(= x y))
(def eq #'eq*)
(defn concatenate*
"Concatenate two strings."
[^{:doc "String or number that can be coerced to string."} x
^{:doc "String or number that can be coerced to string."} y]
(str (coerce/excel-str x)
(coerce/excel-str y)))
(def concatenate #'concatenate*)
(defn pow*
"Returns the value of the first argument raised to the power of the second argument."
[^{:doc "Number or string that can be coerced to number."} x
^{:doc "Number or string that can be coerced to number."} y]
(Math/pow (coerce/excel-number x)
(coerce/excel-number y)))
(def pow #'pow*)
(defn negate*
"Returns true if x is logical false, false otherwise."
[^{:doc "Any object."} x]
(not x))
(def negate #'negate*)
(defn percent*
"Calculate product of given number and 0.01"
[^{:doc "Number or string that can be coerced to number."} x]
(* 0.01 (coerce/excel-number x)))
(def percent #'percent*)
(defn range**
"Returns a collection of integers starting from x and end at y (exclusive)"
[^{:doc "Number or string that can be coerced to number."} x
^{:doc "Number or string that can be coerced to number."} y]
(range (coerce/excel-number x)
(coerce/excel-number y)))
(def range* #'range**)
(def env
{":" range*
"+" add
"-" sub
"*" mult
"/" div
"<" less
">" more
"<=" less-or-eq
">=" more-or-eq
"<>" not-eq
"=" eq
"&" concatenate
"^" pow
"!" negate
"%" percent})