From 3dbc7753341e64a8fa8b2d103c89791e3b5e39e6 Mon Sep 17 00:00:00 2001 From: nathanmarz Date: Tue, 14 Feb 2017 08:25:54 -0500 Subject: [PATCH] fix if-path/selected?/not-selected? so that vals are passed along to condition paths --- src/clj/com/rpl/specter.cljc | 28 ++++++++++++++++------------ src/clj/com/rpl/specter/impl.cljc | 25 +++++++++++++++---------- src/clj/com/rpl/specter/navs.cljc | 8 ++++---- test/com/rpl/specter/core_test.cljc | 14 ++++++++++++++ 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/clj/com/rpl/specter.cljc b/src/clj/com/rpl/specter.cljc index 4a14685..5a27480 100644 --- a/src/clj/com/rpl/specter.cljc +++ b/src/clj/com/rpl/specter.cljc @@ -839,30 +839,34 @@ [& path] (if-let [afn (n/extract-basic-filter-fn path)] afn - (late-bound-nav [late (late-path path)] - (select* [this structure next-fn] + (late-bound-richnav [late (late-path path)] + (select* [this vals structure next-fn] (i/filter-select - #(n/selected?* late %) + #(n/selected?* late vals %) + vals structure next-fn)) - (transform* [this structure next-fn] + (transform* [this vals structure next-fn] (i/filter-transform - #(n/selected?* late %) + #(n/selected?* late vals %) + vals structure next-fn))))) (defdynamicnav not-selected? [& path] (if-let [afn (n/extract-basic-filter-fn path)] (fn [s] (not (afn s))) - (late-bound-nav [late (late-path path)] - (select* [this structure next-fn] + (late-bound-richnav [late (late-path path)] + (select* [this vals structure next-fn] (i/filter-select - #(n/not-selected?* late %) + #(n/not-selected?* late vals %) + vals structure next-fn)) - (transform* [this structure next-fn] + (transform* [this vals structure next-fn] (i/filter-transform - #(n/not-selected?* late %) + #(n/not-selected?* late vals %) + vals structure next-fn))))) @@ -1039,7 +1043,7 @@ vals structure next-fn - #(n/selected?* late-cond %) + #(n/selected?* late-cond vals %) late-then late-else)) (transform* [this vals structure next-fn] @@ -1047,7 +1051,7 @@ vals structure next-fn - #(n/selected?* late-cond %) + #(n/selected?* late-cond vals %) late-then late-else)))))) diff --git a/src/clj/com/rpl/specter/impl.cljc b/src/clj/com/rpl/specter/impl.cljc index 15dd22f..c8030c5 100644 --- a/src/clj/com/rpl/specter/impl.cljc +++ b/src/clj/com/rpl/specter/impl.cljc @@ -271,10 +271,10 @@ #?( :clj - (defmacro compiled-traverse* [path result-fn structure] + (defmacro compiled-traverse-with-vals* [path result-fn vals structure] `(exec-select* ~path - [] + ~vals ~structure (fn [vals# structure#] (if (identical? vals# []) @@ -282,10 +282,10 @@ (~result-fn (conj vals# structure#)))))) :cljs - (defn compiled-traverse* [path result-fn structure] + (defn compiled-traverse-with-vals* [path result-fn vals structure] (exec-select* path - [] + vals structure (fn [vals structure] (if (identical? vals []) @@ -293,6 +293,9 @@ (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] (reify #?(:clj clojure.lang.IReduce :cljs cljs.core/IReduce) (#?(:clj reduce :cljs -reduce) @@ -379,8 +382,10 @@ -(defn compiled-select-any* [path structure] - (unreduced (compiled-traverse* path reduced structure))) +(defn compiled-select-any* + ([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] (let [ret (compiled-select-any* path structure)] @@ -461,14 +466,14 @@ (.-dynamic? c)) -(defn filter-select [afn structure next-fn] +(defn filter-select [afn vals structure next-fn] (if (afn structure) - (next-fn structure) + (next-fn vals structure) NONE)) -(defn filter-transform [afn structure next-fn] +(defn filter-transform [afn vals structure next-fn] (if (afn structure) - (next-fn structure) + (next-fn vals structure) structure)) (defn ^:direct-nav pred* [afn] diff --git a/src/clj/com/rpl/specter/navs.cljc b/src/clj/com/rpl/specter/navs.cljc index 490ca86..8814153 100644 --- a/src/clj/com/rpl/specter/navs.cljc +++ b/src/clj/com/rpl/specter/navs.cljc @@ -13,14 +13,14 @@ (defn not-selected?* - [compiled-path structure] + [compiled-path vals structure] (->> structure - (i/compiled-select-any* compiled-path) + (i/compiled-select-any* compiled-path vals) (identical? i/NONE))) (defn selected?* - [compiled-path structure] - (not (not-selected?* compiled-path structure))) + [compiled-path vals structure] + (not (not-selected?* compiled-path vals structure))) (defn walk-select [pred continue-fn structure] (let [ret (i/mutable-cell i/NONE) diff --git a/test/com/rpl/specter/core_test.cljc b/test/com/rpl/specter/core_test.cljc index 44ecd30..9633ad6 100644 --- a/test/com/rpl/specter/core_test.cljc +++ b/test/com/rpl/specter/core_test.cljc @@ -1394,3 +1394,17 @@ (deftest select-any-vals-test (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]))) + )