merge in latest changes to master

This commit is contained in:
Nathan Marz 2015-06-30 14:31:07 -04:00
commit 8900eac847
5 changed files with 60 additions and 24 deletions

View file

@ -1,3 +1,11 @@
## 0.5.7
* Fix bug in select-one! which wouldn't allow nil result
## 0.5.6
* Add multi-path implementation
* change FIRST/LAST to select nothing on an empty sequence
* Allow sets to be used directly as selectors (acts as filter)
## 0.5.5 ## 0.5.5
* Change filterer to accept a selector (that acts like selected? to determine whether or not to select value) * Change filterer to accept a selector (that acts like selected? to determine whether or not to select value)

View file

@ -1 +1 @@
0.5.5 0.5.7

View file

@ -38,9 +38,9 @@
(defn compiled-select-one! (defn compiled-select-one!
"Version of select-one! that takes in a selector pre-compiled with comp-paths" "Version of select-one! that takes in a selector pre-compiled with comp-paths"
[selector structure] [selector structure]
(let [res (compiled-select-one selector structure)] (let [res (compiled-select selector structure)]
(when (nil? res) (i/throw-illegal "No elements found for params: " selector structure)) (when (not= 1 (count res)) (i/throw-illegal "Expected exactly one element for params: " selector structure))
res (first res)
)) ))
(defn select-one! (defn select-one!
@ -114,9 +114,9 @@
(def VAL (i/->ValCollect)) (def VAL (i/->ValCollect))
(def LAST (i/->LastStructurePath)) (def LAST (i/->PosStructurePath last set-last))
(def FIRST (i/->FirstStructurePath)) (def FIRST (i/->PosStructurePath first set-first))
(defn srange-dynamic [start-fn end-fn] (i/->SRangePath start-fn end-fn)) (defn srange-dynamic [start-fn end-fn] (i/->SRangePath start-fn end-fn))
@ -159,12 +159,16 @@
(extend-type #?(:clj clojure.lang.AFn :cljs js/Function) (extend-type #?(:clj clojure.lang.AFn :cljs js/Function)
StructurePath StructurePath
(select* [afn structure next-fn] (select* [afn structure next-fn]
(if (afn structure) (i/filter-select afn structure next-fn))
(next-fn structure)))
(transform* [afn structure next-fn] (transform* [afn structure next-fn]
(if (afn structure) (i/filter-transform afn structure next-fn)))
(next-fn structure)
structure))) (extend-type #?(:clj clojure.lang.PersistentHashSet :cljs cljs.core/PersistentHashSet)
StructurePath
(select* [aset structure next-fn]
(i/filter-select aset structure next-fn))
(transform* [aset structure next-fn]
(i/filter-transform aset structure next-fn)))
(defn collect [& selector] (defn collect [& selector]
(i/->SelectCollector select (comp-paths* selector))) (i/->SelectCollector select (comp-paths* selector)))

View file

@ -460,23 +460,17 @@
(collect-val [this structure] (collect-val [this structure]
structure)) structure))
(deftype LastStructurePath []) (deftype PosStructurePath [getter setter])
(extend-protocol p/StructurePath (extend-protocol p/StructurePath
LastStructurePath PosStructurePath
(select* [this structure next-fn] (select* [this structure next-fn]
(next-fn (last structure))) (if-not (empty? structure)
(next-fn ((field this 'getter) structure))))
(transform* [this structure next-fn] (transform* [this structure next-fn]
(set-last structure (next-fn (last structure))))) (if (empty? structure)
structure
(deftype FirstStructurePath []) ((field this 'setter) structure (next-fn ((field this 'getter) structure))))))
(extend-protocol p/StructurePath
FirstStructurePath
(select* [this structure next-fn]
(next-fn (first structure)))
(transform* [this structure next-fn]
(set-first structure (next-fn (first structure)))))
(deftype WalkerStructurePath [afn]) (deftype WalkerStructurePath [afn])
@ -624,3 +618,12 @@
(field this 'paths)) (field this 'paths))
)) ))
(defn filter-select [afn structure next-fn]
(if (afn structure)
(next-fn structure)))
(defn filter-transform [afn structure next-fn]
(if (afn structure)
(next-fn structure)
structure))

View file

@ -361,3 +361,24 @@
(transform k1 inc) (transform k1 inc)
(transform k2 inc))) (transform k2 inc)))
)) ))
(deftest empty-pos-transform
(is (empty? (select FIRST [])))
(is (empty? (select LAST [])))
(is (= [] (transform FIRST inc [])))
(is (= [] (transform LAST inc [])))
)
(defspec set-filter-test
(for-all+
[k1 gen/keyword
k2 (gen/such-that #(not= k1 %) gen/keyword)
k3 (gen/such-that (complement #{k1 k2}) gen/keyword)
v (gen/vector (gen/elements [k1 k2 k3]))]
(= (filter #{k1 k2} v) (select [ALL #{k1 k2}] v))
))
(deftest nil-select-one-test
(is (= nil (select-one! ALL [nil])))
(is (thrown? Exception (select-one! ALL [])))
)