-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
built_ins.cljc
187 lines (164 loc) · 5.16 KB
/
built_ins.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(ns datascript.built-ins
(:require
[clojure.string :as str]
[datascript.db :as db #?(:cljs :refer-macros :clj :refer) [raise]]
[datascript.impl.entity :as de]))
(defn- -differ? [& xs]
(let [l (count xs)]
(not= (take (/ l 2) xs) (drop (/ l 2) xs))))
(defn- -get-else
[db e a else-val]
(when (nil? else-val)
(raise "get-else: nil default value is not supported" {:error :query/where}))
(if-some [datom (first (db/-search db [(db/entid db e) a]))]
(:v datom)
else-val))
(defn- -get-some
[db e & as]
(reduce
(fn [_ a]
(when-some [datom (first (db/-search db [(db/entid db e) a]))]
(reduced [(:a datom) (:v datom)])))
nil
as))
(defn- -missing?
[db e a]
(nil? (get (de/entity db e) a)))
(defn- and-fn [& args]
(reduce (fn [a b]
(if b b (reduced b))) true args))
(defn- or-fn [& args]
(reduce (fn [a b]
(if b (reduced b) b)) nil args))
(defn- less
([x] true)
([x y] (neg? (db/value-compare x y)))
([x y & more]
(if (less x y)
(if (next more)
(recur y (first more) (next more))
(less y (first more)))
false)))
(defn- greater
([x] true)
([x y] (pos? (db/value-compare x y)))
([x y & more]
(if (greater x y)
(if (next more)
(recur y (first more) (next more))
(greater y (first more)))
false)))
(defn- less-equal
([x] true)
([x y] (not (pos? (db/value-compare x y))))
([x y & more]
(if (less-equal x y)
(if (next more)
(recur y (first more) (next more))
(less-equal y (first more)))
false)))
(defn- greater-equal
([x] true)
([x y] (not (neg? (db/value-compare x y))))
([x y & more]
(if (greater-equal x y)
(if (next more)
(recur y (first more) (next more))
(greater-equal y (first more)))
false)))
(def query-fns {
'= =, '== ==, 'not= not=, '!= not=,
'< less, '> greater, '<= less-equal, '>= greater-equal,
'+ +, '- -, '* *, '/ /,
'quot quot, 'rem rem, 'mod mod, 'inc inc, 'dec dec, 'max max, 'min min,
'zero? zero?, 'pos? pos?, 'neg? neg?, 'even? even?, 'odd? odd?, 'compare compare,
'rand rand, 'rand-int rand-int,
'true? true?, 'false? false?, 'nil? nil?, 'some? some?, 'not not, 'and and-fn, 'or or-fn,
'complement complement, 'identical? identical?,
'identity identity, 'keyword keyword, 'meta meta, 'name name, 'namespace namespace, 'type type,
'vector vector, 'list list, 'set set, 'hash-map hash-map, 'array-map array-map,
'count count, 'range range, 'not-empty not-empty, 'empty? empty?, 'contains? contains?,
'str str, 'subs, subs, 'get get,
'pr-str pr-str, 'print-str print-str, 'println-str println-str, 'prn-str prn-str,
're-find re-find, 're-matches re-matches, 're-seq re-seq, 're-pattern re-pattern,
'-differ? -differ?, 'get-else -get-else, 'get-some -get-some, 'missing? -missing?, 'ground identity,
'clojure.string/blank? str/blank?, 'clojure.string/includes? str/includes?,
'clojure.string/starts-with? str/starts-with?, 'clojure.string/ends-with? str/ends-with?
'tuple vector, 'untuple identity
})
;; Aggregates
(defn- aggregate-sum [coll]
(reduce + 0 coll))
(defn- aggregate-avg [coll]
(/ (aggregate-sum coll) (count coll)))
(defn- aggregate-median [coll]
(let [terms (sort coll)
size (count coll)
med (bit-shift-right size 1)]
(cond-> (nth terms med)
(even? size)
(-> (+ (nth terms (dec med)))
(/ 2)))))
(defn- aggregate-variance [coll]
(let [mean (aggregate-avg coll)
sum (aggregate-sum
(for [x coll
:let [delta (- x mean)]]
(* delta delta)))]
(/ sum (count coll))))
(defn- aggregate-stddev [coll]
(#?(:cljs js/Math.sqrt :clj Math/sqrt) (aggregate-variance coll)))
(defn- aggregate-min
([coll]
(reduce
(fn [acc x]
(if (neg? (compare x acc))
x acc))
(first coll) (next coll)))
([n coll]
(vec
(reduce (fn [acc x]
(cond
(< (count acc) n)
(sort compare (conj acc x))
(neg? (compare x (last acc)))
(sort compare (conj (butlast acc) x))
:else acc))
[] coll))))
(defn- aggregate-max
([coll]
(reduce
(fn [acc x]
(if (pos? (compare x acc))
x acc))
(first coll) (next coll)))
([n coll]
(vec
(reduce (fn [acc x]
(cond
(< (count acc) n)
(sort compare (conj acc x))
(pos? (compare x (first acc)))
(sort compare (conj (next acc) x))
:else acc))
[] coll))))
(defn- aggregate-rand
([coll] (rand-nth coll))
([n coll] (vec (repeatedly n #(rand-nth coll)))))
(defn- aggregate-sample [n coll]
(vec (take n (shuffle coll))))
(defn- aggregate-count-distinct [coll]
(count (distinct coll)))
(def aggregates
{'sum aggregate-sum
'avg aggregate-avg
'median aggregate-median
'variance aggregate-variance
'stddev aggregate-stddev
'distinct set
'min aggregate-min
'max aggregate-max
'rand aggregate-rand
'sample aggregate-sample
'count count
'count-distinct aggregate-count-distinct})