fix traverse-all to properly propagate reduced vals, add tests
This commit is contained in:
parent
62c998a472
commit
3693d307fc
2 changed files with 26 additions and 7 deletions
|
|
@ -293,9 +293,7 @@
|
|||
(result-fn (conj vals structure)))))))
|
||||
|
||||
|
||||
|
||||
|
||||
(defn do-compiled-traverse [apath structure]
|
||||
(defn do-compiled-traverse* [apath structure]
|
||||
(reify #?(:clj clojure.lang.IReduce :cljs cljs.core/IReduce)
|
||||
(#?(:clj reduce :cljs -reduce)
|
||||
[this afn]
|
||||
|
|
@ -312,9 +310,12 @@
|
|||
newv ; to support reduced handling during traverse
|
||||
))
|
||||
structure)
|
||||
(unreduced (get-cell cell))
|
||||
(get-cell cell)
|
||||
))))
|
||||
|
||||
(defn do-compiled-traverse [apath structure]
|
||||
(unreduced (do-compiled-traverse* apath structure)))
|
||||
|
||||
|
||||
(defn compiled-traverse-all* [path]
|
||||
(fn [xf]
|
||||
|
|
@ -326,7 +327,8 @@
|
|||
(fn [r i]
|
||||
(xf r i))
|
||||
result
|
||||
(do-compiled-traverse path input)
|
||||
;; use this one to make sure reduced vals are propagated back
|
||||
(do-compiled-traverse* path input)
|
||||
)
|
||||
))))
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
select-first transform setval replace-in
|
||||
select-any selected-any? collected? traverse
|
||||
multi-transform path dynamicnav recursive-path
|
||||
defdynamicnav]]))
|
||||
defdynamicnav traverse-all]]))
|
||||
(:use
|
||||
#?(:clj [clojure.test :only [deftest is]])
|
||||
#?(:clj [clojure.test.check.clojure-test :only [defspec]])
|
||||
|
|
@ -21,7 +21,7 @@
|
|||
select-first transform setval replace-in
|
||||
select-any selected-any? collected? traverse
|
||||
multi-transform path dynamicnav recursive-path
|
||||
defdynamicnav]]))
|
||||
defdynamicnav traverse-all]]))
|
||||
|
||||
|
||||
|
||||
|
|
@ -1358,3 +1358,20 @@
|
|||
data
|
||||
)))
|
||||
))
|
||||
|
||||
(deftest traverse-all-test
|
||||
(is (= 3
|
||||
(transduce (comp (mapcat identity)
|
||||
(traverse-all :a))
|
||||
(completing (fn [r i] (if (= i 4) (reduced r) (+ r i))))
|
||||
0
|
||||
[[{:a 1}] [{:a 2}] [{:a 4}] [{:a 5}]])))
|
||||
(is (= 6
|
||||
(transduce (traverse-all [s/ALL :a])
|
||||
+
|
||||
0
|
||||
[[{:a 1} {:a 2}] [{:a 3}]]
|
||||
)))
|
||||
(is (= [1 2]
|
||||
(into [] (traverse-all :a) [{:a 1} {:a 2}])))
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue