fix if-path/selected?/not-selected? so that vals are passed along to condition paths

This commit is contained in:
nathanmarz 2017-02-14 08:25:54 -05:00
parent baf658365e
commit 3dbc775334
4 changed files with 49 additions and 26 deletions

View file

@ -839,30 +839,34 @@
[& path] [& path]
(if-let [afn (n/extract-basic-filter-fn path)] (if-let [afn (n/extract-basic-filter-fn path)]
afn afn
(late-bound-nav [late (late-path path)] (late-bound-richnav [late (late-path path)]
(select* [this structure next-fn] (select* [this vals structure next-fn]
(i/filter-select (i/filter-select
#(n/selected?* late %) #(n/selected?* late vals %)
vals
structure structure
next-fn)) next-fn))
(transform* [this structure next-fn] (transform* [this vals structure next-fn]
(i/filter-transform (i/filter-transform
#(n/selected?* late %) #(n/selected?* late vals %)
vals
structure structure
next-fn))))) next-fn)))))
(defdynamicnav not-selected? [& path] (defdynamicnav not-selected? [& path]
(if-let [afn (n/extract-basic-filter-fn path)] (if-let [afn (n/extract-basic-filter-fn path)]
(fn [s] (not (afn s))) (fn [s] (not (afn s)))
(late-bound-nav [late (late-path path)] (late-bound-richnav [late (late-path path)]
(select* [this structure next-fn] (select* [this vals structure next-fn]
(i/filter-select (i/filter-select
#(n/not-selected?* late %) #(n/not-selected?* late vals %)
vals
structure structure
next-fn)) next-fn))
(transform* [this structure next-fn] (transform* [this vals structure next-fn]
(i/filter-transform (i/filter-transform
#(n/not-selected?* late %) #(n/not-selected?* late vals %)
vals
structure structure
next-fn))))) next-fn)))))
@ -1039,7 +1043,7 @@
vals vals
structure structure
next-fn next-fn
#(n/selected?* late-cond %) #(n/selected?* late-cond vals %)
late-then late-then
late-else)) late-else))
(transform* [this vals structure next-fn] (transform* [this vals structure next-fn]
@ -1047,7 +1051,7 @@
vals vals
structure structure
next-fn next-fn
#(n/selected?* late-cond %) #(n/selected?* late-cond vals %)
late-then late-then
late-else)))))) late-else))))))

View file

@ -271,10 +271,10 @@
#?( #?(
:clj :clj
(defmacro compiled-traverse* [path result-fn structure] (defmacro compiled-traverse-with-vals* [path result-fn vals structure]
`(exec-select* `(exec-select*
~path ~path
[] ~vals
~structure ~structure
(fn [vals# structure#] (fn [vals# structure#]
(if (identical? vals# []) (if (identical? vals# [])
@ -282,10 +282,10 @@
(~result-fn (conj vals# structure#)))))) (~result-fn (conj vals# structure#))))))
:cljs :cljs
(defn compiled-traverse* [path result-fn structure] (defn compiled-traverse-with-vals* [path result-fn vals structure]
(exec-select* (exec-select*
path path
[] vals
structure structure
(fn [vals structure] (fn [vals structure]
(if (identical? vals []) (if (identical? vals [])
@ -293,6 +293,9 @@
(result-fn (conj vals structure))))))) (result-fn (conj vals structure)))))))
(defn compiled-traverse* [path result-fn structure]
(compiled-traverse-with-vals* path result-fn [] structure))
(defn do-compiled-traverse* [apath structure] (defn do-compiled-traverse* [apath structure]
(reify #?(:clj clojure.lang.IReduce :cljs cljs.core/IReduce) (reify #?(:clj clojure.lang.IReduce :cljs cljs.core/IReduce)
(#?(:clj reduce :cljs -reduce) (#?(:clj reduce :cljs -reduce)
@ -379,8 +382,10 @@
(defn compiled-select-any* [path structure] (defn compiled-select-any*
(unreduced (compiled-traverse* path reduced structure))) ([path structure] (compiled-select-any* path [] structure))
([path vals structure]
(unreduced (compiled-traverse-with-vals* path reduced vals structure))))
(defn compiled-select-first* [path structure] (defn compiled-select-first* [path structure]
(let [ret (compiled-select-any* path structure)] (let [ret (compiled-select-any* path structure)]
@ -461,14 +466,14 @@
(.-dynamic? c)) (.-dynamic? c))
(defn filter-select [afn structure next-fn] (defn filter-select [afn vals structure next-fn]
(if (afn structure) (if (afn structure)
(next-fn structure) (next-fn vals structure)
NONE)) NONE))
(defn filter-transform [afn structure next-fn] (defn filter-transform [afn vals structure next-fn]
(if (afn structure) (if (afn structure)
(next-fn structure) (next-fn vals structure)
structure)) structure))
(defn ^:direct-nav pred* [afn] (defn ^:direct-nav pred* [afn]

View file

@ -13,14 +13,14 @@
(defn not-selected?* (defn not-selected?*
[compiled-path structure] [compiled-path vals structure]
(->> structure (->> structure
(i/compiled-select-any* compiled-path) (i/compiled-select-any* compiled-path vals)
(identical? i/NONE))) (identical? i/NONE)))
(defn selected?* (defn selected?*
[compiled-path structure] [compiled-path vals structure]
(not (not-selected?* compiled-path structure))) (not (not-selected?* compiled-path vals structure)))
(defn walk-select [pred continue-fn structure] (defn walk-select [pred continue-fn structure]
(let [ret (i/mutable-cell i/NONE) (let [ret (i/mutable-cell i/NONE)

View file

@ -1394,3 +1394,17 @@
(deftest select-any-vals-test (deftest select-any-vals-test
(is (= [1 1] (select-any s/VAL 1)))) (is (= [1 1] (select-any s/VAL 1))))
(deftest conditional-vals-test
(is (= 2 (select-any (s/with-fresh-collected
(s/collect-one (s/keypath 0))
(s/if-path (collected? [n] (even? n))
(s/keypath 1)
(s/keypath 2)))
[4 2 3])))
(is (= [4 2 3]
(select-any (s/with-fresh-collected
(s/collect-one (s/keypath 0))
(s/selected? (collected? [n] (even? n))))
[4 2 3])))
)