allow sets to be used directly as selector (acts as filter)

This commit is contained in:
Nathan Marz 2015-06-29 18:30:30 -04:00
parent bcc15b1587
commit 8053245c48
3 changed files with 27 additions and 5 deletions

View file

@ -161,12 +161,16 @@
(extend-type clojure.lang.AFn (extend-type clojure.lang.AFn
StructurePath StructurePath
(select* [afn structure next-fn] (select* [afn structure next-fn]
(if (afn structure) (filter-select afn structure next-fn))
(next-fn structure)))
(transform* [afn structure next-fn] (transform* [afn structure next-fn]
(if (afn structure) (filter-transform afn structure next-fn)))
(next-fn structure)
structure))) (extend-protocol StructurePath
clojure.lang.PersistentHashSet
(select* [aset structure next-fn]
(filter-select aset structure next-fn))
(transform* [aset structure next-fn]
(filter-transform aset structure next-fn)))
(defn collect [& selector] (defn collect [& selector]
(->SelectCollector select (comp-paths* selector))) (->SelectCollector select (comp-paths* selector)))

View file

@ -532,3 +532,12 @@
(.paths this)) (.paths this))
)) ))
(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

@ -369,3 +369,12 @@
(is (= [] (transform FIRST inc []))) (is (= [] (transform FIRST inc [])))
(is (= [] (transform LAST 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))
))