diff --git a/src/datascript/built_ins.cljc b/src/datascript/built_ins.cljc index 257b1e9e..6880747a 100644 --- a/src/datascript/built_ins.cljc +++ b/src/datascript/built_ins.cljc @@ -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, diff --git a/test/datascript/test/query_fns.cljc b/test/datascript/test/query_fns.cljc index c6179e11..091d5d8f 100644 --- a/test/datascript/test/query_fns.cljc +++ b/test/datascript/test/query_fns.cljc @@ -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]