added STAY, stay-then-continue, and continue-then-stay selectors

This commit is contained in:
Nathan Marz 2016-01-14 13:15:19 -05:00
parent 6528945797
commit 1328551a19
2 changed files with 44 additions and 0 deletions

View file

@ -146,6 +146,15 @@
structure structure
)) ))
(defpath
^{:doc "Stays navigated at the current point. Essentially a no-op selector."}
STAY
[]
(select* [this structure next-fn]
(next-fn structure))
(transform* [this structure next-fn]
(next-fn structure)))
(def ALL (comp-paths (i/->AllStructurePath))) (def ALL (comp-paths (i/->AllStructurePath)))
(def VAL (i/->ValCollect)) (def VAL (i/->ValCollect))
@ -235,6 +244,30 @@
ancestry)) ancestry))
))) )))
(defn stay-then-continue
"Navigates to the current element and then navigates via the provided path.
This can be used to implement pre-order traversal."
[& path]
(fixed-pathed-path [late path]
(select* [this structure next-fn]
(concat (next-fn structure)
(doall (mapcat next-fn (compiled-select late structure)))))
(transform* [this structure next-fn]
(compiled-transform late next-fn (next-fn structure))
)))
(defn continue-then-stay
"Navigates to the provided path and then to the current element. This can be used
to implement post-order traversal."
[& path]
(fixed-pathed-path [late path]
(select* [this structure next-fn]
(concat (doall (mapcat next-fn (compiled-select late structure)))
(next-fn structure)))
(transform* [this structure next-fn]
(next-fn (compiled-transform late next-fn structure))
)))
(defpath keypath [key] (defpath keypath [key]
(select* [this structure next-fn] (select* [this structure next-fn]

View file

@ -578,6 +578,17 @@
(s/transform [s/ALL even?] inc s1)) (s/transform [s/ALL even?] inc s1))
))) )))
(deftest stay-continue-tests
(is (= [[1 2 [:a :b]] [3 [:a :b]] [:a :b [:a :b]]]
(s/setval [(s/stay-then-continue s/ALL) s/END] [[:a :b]] [[1 2] [3]])))
(is (= [[1 2 [:a :b]] [3 [:a :b]] [:a :b]]
(s/setval [(s/continue-then-stay s/ALL) s/END] [[:a :b]] [[1 2] [3]])))
(is (= [[1 2 3] 1 3]
(s/select (s/stay-then-continue s/ALL odd?) [1 2 3])))
(is (= [1 3 [1 2 3]]
(s/select (s/continue-then-stay s/ALL odd?) [1 2 3])))
)
#+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))