auto-coerce map entries to vectors during ALL

This commit is contained in:
Nathan Marz 2016-01-28 12:41:36 -08:00
parent 3a38c844e6
commit 7e54757659
4 changed files with 27 additions and 8 deletions

View file

@ -1,3 +1,6 @@
## 0.9.3 (unreleased)
* ALL on maps auto-coerces MapEntry to vector, enabling smoother transformation of map keys
## 0.9.2 ## 0.9.2
* Added VOID selector which navigates nowhere * Added VOID selector which navigates nowhere
* Better syntax checking for defpath * Better syntax checking for defpath

View file

@ -1 +1 @@
0.9.2 0.9.3-SNAPSHOT

View file

@ -500,19 +500,30 @@
(assoc structure akey (next-fn (get structure akey)) (assoc structure akey (next-fn (get structure akey))
)) ))
(defn all-select [structure next-fn]
(into [] (r/mapcat next-fn structure)))
(defn all-transform [structure next-fn]
(let [empty-structure (empty structure)]
(cond (list? empty-structure)
;; this is done to maintain order, otherwise lists get reversed
(doall (map next-fn structure))
(map? structure)
(->> structure (r/map vec) (r/map next-fn) (into empty-structure))
:else
(->> structure (r/map next-fn) (into empty-structure))
)))
(deftype AllStructurePath []) (deftype AllStructurePath [])
(extend-protocol p/StructurePath (extend-protocol p/StructurePath
AllStructurePath AllStructurePath
(select* [this structure next-fn] (select* [this structure next-fn]
(into [] (r/mapcat next-fn structure))) (all-select structure next-fn))
(transform* [this structure next-fn] (transform* [this structure next-fn]
(let [empty-structure (empty structure)] (all-transform structure next-fn)))
(if (list? empty-structure)
;; this is done to maintain order, otherwise lists get reversed
(doall (map next-fn structure))
(->> structure (r/map next-fn) (into empty-structure))
))))
(deftype ValCollect []) (deftype ValCollect [])

View file

@ -615,6 +615,11 @@
) )
(deftest all-map-test
(is (= {3 3} (s/transform [s/ALL s/FIRST] inc {2 3})))
(is (= {3 21 4 31} (s/transform [s/ALL s/ALL] inc {2 20 3 30})))
)
#+clj #+clj
(deftest large-params-test (deftest large-params-test
(let [path (apply s/comp-paths (repeat 25 s/keypath)) (let [path (apply s/comp-paths (repeat 25 s/keypath))