implement cond-path in terms of if-path to avoid all runtime sequence operations

This commit is contained in:
Nathan Marz 2016-06-04 16:40:34 -04:00
parent 97f7ba618c
commit 0bc26c950e
2 changed files with 19 additions and 37 deletions

View file

@ -503,29 +503,6 @@
(collect-val [this structure]
val ))
(defpathedfn cond-path
"Takes in alternating cond-path path cond-path path...
Tests the structure if selecting with cond-path returns anything.
If so, it uses the following path for this portion of the navigation.
Otherwise, it tries the next cond-path. If nothing matches, then the structure
is not selected.
The input paths may be parameterized, in which case the result of cond-path
will be parameterized in the order of which the parameterized navigators
were declared."
[& conds]
(variable-pathed-nav [compiled-paths conds]
(select* [this structure next-fn]
(if-let [selector (i/retrieve-cond-selector compiled-paths structure)]
(->> (compiled-select selector structure)
(mapcat next-fn)
doall)))
(transform* [this structure next-fn]
(if-let [selector (i/retrieve-cond-selector compiled-paths structure)]
(compiled-transform selector next-fn structure)
structure
))))
(defpathedfn if-path
"Like cond-path, but with if semantics."
([cond-p then-path]
@ -544,6 +521,25 @@
(transform* [this structure next-fn]
(i/if-transform structure next-fn late-cond late-then late-else)))))
(defpathedfn cond-path
"Takes in alternating cond-path path cond-path path...
Tests the structure if selecting with cond-path returns anything.
If so, it uses the following path for this portion of the navigation.
Otherwise, it tries the next cond-path. If nothing matches, then the structure
is not selected.
The input paths may be parameterized, in which case the result of cond-path
will be parameterized in the order of which the parameterized navigators
were declared."
[& conds]
(let [pairs (reverse (partition 2 conds))]
(reduce
(fn [p [tester apath]]
(if-path tester apath p))
STOP
pairs
)))
(defpathedfn multi-path
"A path that branches on multiple paths. For updates,
applies updates to the paths in order."

View file

@ -695,20 +695,6 @@
(next-fn structure)
))
(defn retrieve-cond-selector [cond-paths structure]
(let [aseq (seq cond-paths)]
(if aseq
(loop [s aseq]
(let [tester (first s)
s2 (next s)
res (first s2)]
(if (empty? (compiled-select* tester structure))
(let [s3 (next s2)]
(if s3 (recur s3))
)
res
))))))
(defn if-select [structure next-fn late-cond late-then late-else]
(let [apath (if (empty? (compiled-select* late-cond structure))
late-else