Skip to content

Commit

Permalink
Restore multiarity to query comparison fns #420 #421
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jan 19, 2022
1 parent efb56b6 commit cd129b7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
45 changes: 41 additions & 4 deletions src/datascript/built_ins.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,49 @@
(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=,
'< (fn [a b] (neg? (db/value-compare a b))),
'> (fn [a b] (pos? (db/value-compare a b))),
'<= (fn [a b] (not (pos? (db/value-compare a b)))),
'>= (fn [a b] (not (neg? (db/value-compare a b)))),
'< 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,
Expand Down
3 changes: 1 addition & 2 deletions test/datascript/test/query_fns.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@
(is (= (d/q '[:find ?e1 ?e2
:where [?e1 :age ?a1]
[?e2 :age ?a2]
[(< ?a1 18)]
[(< 18 ?a2)]] db)
[(< ?a1 18 ?a2)]] db)
#{[1 2] [1 3]}))
(is (= (d/q '[:find ?a1
:where [_ :age ?a1]
Expand Down

0 comments on commit cd129b7

Please sign in to comment.