Skip to content

Commit

Permalink
Merge pull request #674 from samply/refactoring
Browse files Browse the repository at this point in the history
Various Refactorings
  • Loading branch information
alexanderkiel committed Apr 7, 2022
2 parents b53a412 + ae46bfe commit 41714e4
Show file tree
Hide file tree
Showing 39 changed files with 513 additions and 215 deletions.
8 changes: 8 additions & 0 deletions modules/byte-buffer/src/blaze/byte_buffer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@


(defn allocate
"Allocates a new byte buffer.
The new buffer's position will be zero, its limit will be `capacity`, its
mark will be undefined, each of its elements will be initialized to zero, and
its byte order will be BIG_ENDIAN.
It will have a backing array, and its array offset will be zero."
{:inline (fn [capacity] `(ByteBuffer/allocate ~capacity))}
[capacity]
(ByteBuffer/allocate capacity))
Expand Down Expand Up @@ -111,6 +118,7 @@


(defn limit
"Returns the limit of `byte-buffer`."
{:inline
(fn [byte-buffer]
`(.limit ~(vary-meta byte-buffer assoc :tag `ByteBuffer)))}
Expand Down
11 changes: 10 additions & 1 deletion modules/byte-buffer/test/blaze/byte_buffer_test.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
(ns blaze.byte-buffer-test
(:require
[blaze.byte-buffer :as bb]
[blaze.test-util :refer [satisfies-prop]]
[clojure.spec.test.alpha :as st]
[clojure.test :as test :refer [deftest is testing]]))
[clojure.test :as test :refer [deftest is testing]]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop]))


(st/instrument)
Expand All @@ -17,6 +20,12 @@
(test/use-fixtures :each fixture)


(deftest limit-test
(satisfies-prop 100
(prop/for-all [capacity gen/nat]
(= capacity (bb/limit (bb/allocate capacity))))))


(deftest size-up-to-null-test
(testing "empty buffer"
(let [buf (bb/allocate 0)]
Expand Down
2 changes: 1 addition & 1 deletion modules/byte-string/src/blaze/byte_string.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
(instance? ByteString x))


(def ^:const empty
(def empty
ByteString/EMPTY)


Expand Down
3 changes: 2 additions & 1 deletion modules/coll/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
{cloverage/cloverage
{:mvn/version "1.2.3"}}

:main-opts ["-m" "cloverage.coverage" "--codecov" "-p" "src" "-s" "test"]}}}
:main-opts ["-m" "cloverage.coverage" "--codecov" "-p" "src" "-s" "test"
"-e" ".+spec"]}}}
33 changes: 29 additions & 4 deletions modules/coll/src/blaze/coll/core.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns blaze.coll.core
(:refer-clojure :exclude [count eduction empty? first])
(:refer-clojure :exclude [count eduction empty? first nth])
(:import
[clojure.lang Counted IReduceInit Seqable Sequential]))
[clojure.lang Counted Indexed IReduceInit Seqable Sequential]))


(set! *warn-on-reflection* true)
Expand All @@ -11,7 +11,7 @@
(defn first
"Like `clojure.core/first` but for reducible collections."
[coll]
(reduce (fn [_ x] (reduced x)) nil coll))
(reduce #(reduced %2) nil coll))


(defn empty?
Expand All @@ -20,7 +20,7 @@
(nil? (first coll)))


(defn inc-rf [sum _] (inc ^long sum))
(defn- inc-rf [sum _] (inc ^long sum))


(defn eduction
Expand All @@ -37,3 +37,28 @@
Counted
(count [coll]
(.reduce coll inc-rf 0))))


(defn count
"Like `clojure.core/count` but works only for non-nil collections
implementing `clojure.lang.Countet` like vectors."
{:inline
(fn [coll]
`(.count ~(with-meta coll {:tag `Counted})))}
[coll]
(.count ^Counted coll))


(defn nth
"Like `clojure.core/nth` but works only for non-nil collections implementing
`clojure.lang.Indexed` like vectors."
{:inline
(fn
([coll i]
`(.nth ~(with-meta coll {:tag `Indexed}) (int ~i)))
([coll i not-found]
`(.nth ~(with-meta coll {:tag `Indexed}) (int ~i) ~not-found)))}
([coll i]
(.nth ^Indexed coll i))
([coll i not-found]
(.nth ^Indexed coll i not-found)))
15 changes: 15 additions & 0 deletions modules/coll/src/blaze/coll/core_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@
(s/fdef coll/empty?
:args (s/cat :coll (s/nilable #(instance? IReduceInit %)))
:ret boolean?)


(s/fdef coll/eduction
:args (s/cat :xform ifn? :coll any?)
:ret #(instance? IReduceInit %))


(s/fdef coll/count
:args (s/cat :coll #(instance? IReduceInit %))
:ret nat-int?)


(s/fdef coll/nth
:args (s/cat :coll #(instance? IReduceInit %) :i nat-int? :not-found (s/? any?))
:ret nat-int?)
35 changes: 32 additions & 3 deletions modules/coll/test/blaze/coll/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require
[blaze.coll.core :as coll]
[clojure.spec.test.alpha :as st]
[clojure.test :as test :refer [deftest is testing]]))
[clojure.test :as test :refer [are deftest is testing]]))


(st/instrument)
Expand Down Expand Up @@ -44,7 +44,7 @@

(deftest eduction-test
(testing "eductions are sequential"
(is (sequential? (coll/eduction (map identity) [1]))))
(is (sequential? (coll/eduction identity [1]))))

(testing "eductions can be reduced"
(is (= [2 3] (reduce conj [] (coll/eduction (map inc) [1 2])))))
Expand All @@ -53,4 +53,33 @@
(is (= (list 2 3) (seq (coll/eduction (map inc) [1 2])))))

(testing "eductions are counted"
(is (= 2 (count (coll/eduction (map inc) [1 2]))))))
(is (= 2 (count (coll/eduction identity [1 2]))))))


(deftest count-test
(are [coll n] (and (= n (coll/count coll))
(= n (apply coll/count [coll])))
[] 0
[::x] 1
[::x ::y] 2))


(deftest nth-test
(are [coll n x] (and (= x (coll/nth coll n))
(= x (apply coll/nth coll [n])))
[::x] 0 ::x
[::x ::y] 0 ::x
[::x ::y] 1 ::y)

(testing "throws IndexOutOfBoundsException"
(is (thrown? IndexOutOfBoundsException (coll/nth [] 0))))

(testing "not-found"
(are [coll n x] (and (= x (coll/nth coll n ::not-found))
(= x (apply coll/nth coll n [::not-found])))
[] 0 ::not-found
[::x] 0 ::x
[::x] 1 ::not-found
[::x ::y] 0 ::x
[::x ::y] 1 ::y
[::x ::y] 2 ::not-found)))
11 changes: 6 additions & 5 deletions modules/cql/src/blaze/elm/compiler/aggregate_operators.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Section numbers are according to
https://cql.hl7.org/04-logicalspecification.html."
(:require
[blaze.coll.core :as coll]
[blaze.elm.aggregates :as aggregates]
[blaze.elm.compiler.macros :refer [defaggop]]
[blaze.elm.compiler.queries :as queries]
Expand Down Expand Up @@ -49,12 +50,12 @@
(defaggop median [source]
(let [sorted (vec (sort-by identity queries/asc-comparator (remove nil? source)))]
(when (seq sorted)
(if (zero? (rem (count sorted) 2))
(let [upper-idx (quot (count sorted) 2)]
(p/divide (p/add (nth sorted (dec upper-idx))
(nth sorted upper-idx))
(if (zero? (rem (coll/count sorted) 2))
(let [upper-idx (quot (coll/count sorted) 2)]
(p/divide (p/add (coll/nth sorted (dec upper-idx))
(coll/nth sorted upper-idx))
2))
(nth sorted (quot (count sorted) 2))))))
(coll/nth sorted (quot (coll/count sorted) 2))))))


;; 21.9. Min
Expand Down
37 changes: 18 additions & 19 deletions modules/cql/src/blaze/elm/list.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
PersistentVector
(equal [x y]
(when y
(if (= (count x) (count y))
(if (empty? x)
(if (= (.count x) (count y))
(if (zero? (.count x))
true
(loop [[t & ts] (map p/equal x y)]
(if (and (true? t) ts)
Expand All @@ -28,16 +28,16 @@

IReduceInit
(equal [x y]
(p/equal (vec x) y)))
(p/equal (PersistentVector/create x) y)))


;; 12.2. Equivalent
(extend-protocol p/Equivalent
PersistentVector
(equivalent [x y]
(if y
(if (= (count x) (count y))
(if (empty? x)
(if (= (.count x) (count y))
(if (zero? (.count x))
true
(loop [[t & ts] (map p/equivalent x y)]
(if (and (true? t) ts)
Expand All @@ -48,19 +48,19 @@

IReduceInit
(equivalent [x y]
(p/equivalent (vec x) y)))
(p/equivalent (PersistentVector/create x) y)))


;; 17.6. Indexer
(extend-protocol p/Indexer
PersistentVector
(indexer [list index]
(when (and index (<= 0 index) (< index (count list)))
(nth list index)))
(when index
(.nth list index nil)))

IReduceInit
(indexer [list index]
(p/indexer (vec list) index)))
(p/indexer (PersistentVector/create list) index)))


;; 19.5. Contains
Expand Down Expand Up @@ -110,7 +110,7 @@
PersistentVector
(intersect [x y]
(when y
(if (<= (count x) (count y))
(if (<= (.count x) (count y))
(.reduce
x
(fn [result x]
Expand All @@ -122,29 +122,29 @@

IReduceInit
(intersect [x y]
(p/intersect (vec x) y)))
(p/intersect (PersistentVector/create x) y)))


;; 19.24. ProperContains
(extend-protocol p/ProperContains
PersistentVector
(proper-contains [list x _]
(and (p/contains list x _) (> (count list) 1)))
(and (p/contains list x _) (> (.count list) 1)))

IReduceInit
(proper-contains [list x precision]
(p/proper-contains (vec list) x precision)))
(p/proper-contains (PersistentVector/create list) x precision)))


;; 19.26. ProperIncludes
(extend-protocol p/ProperIncludes
PersistentVector
(proper-includes [x y _]
(and (p/includes x y _) (> (count x) (count y))))
(and (p/includes x y _) (> (.count x) (count y))))

IReduceInit
(proper-includes [x y precision]
(p/proper-includes (vec x) y precision)))
(p/proper-includes (PersistentVector/create x) y precision)))


;; 19.31. Union
Expand Down Expand Up @@ -175,10 +175,9 @@
(extend-protocol p/SingletonFrom
PersistentVector
(singleton-from [list]
(let [[fst snd] list]
(if (nil? snd)
fst
(throw-anom (more-than-one-element-anom list)))))
(if (<= (.count list) 1)
(.nth list 0 nil)
(throw-anom (more-than-one-element-anom list))))

IReduceInit
(singleton-from [list]
Expand Down
2 changes: 1 addition & 1 deletion modules/cql/test/blaze/elm/literal.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns blaze.elm.literal
(:refer-clojure
:exclude [and boolean count distinct first flatten list long max min not or
:exclude [abs and boolean count distinct first flatten list long max min not or
time])
(:require
[blaze.elm.spec]
Expand Down
3 changes: 2 additions & 1 deletion modules/db-protocols/src/blaze/db/impl/protocols.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
(-compile-value [search-param modifier value] "Can return an anomaly.")
(-resource-handles
[search-param context tid modifier compiled-value]
[search-param context tid modifier compiled-value start-id])
[search-param context tid modifier compiled-value start-id]
"Returns a reducible collection.")
(-compartment-keys [search-param context compartment tid compiled-value])
(-matches? [search-param context resource-handle modifier compiled-values])
(-compartment-ids [_ resolver resource])
Expand Down
4 changes: 2 additions & 2 deletions modules/db/src/blaze/db/impl/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(set! *unchecked-math* :warn-on-boxed)


(defn inc-rf [sum _] (inc ^long sum))
(defn- inc-rf [sum _] (inc ^long sum))


(defmacro with-open-coll
Expand All @@ -26,7 +26,7 @@
IReduceInit
(reduce [_ rf# init#]
(with-open ~bindings
(.reduce ~(vary-meta coll assoc :tag `IReduceInit) rf# init#)))
(reduce rf# init# ~coll)))
Seqable
(seq [this#]
(.seq ^Seqable (persistent! (.reduce this# conj! (transient [])))))
Expand Down
Loading

0 comments on commit 41714e4

Please sign in to comment.