From 82f591a5d36b6dd67661ea60ec20a18bc93260ae Mon Sep 17 00:00:00 2001 From: Nathan Marz Date: Wed, 20 Apr 2016 22:09:02 -0400 Subject: [PATCH] redefine filterer in terms of subselect --- src/clj/com/rpl/specter.cljx | 34 ++++++++++--------------------- src/clj/com/rpl/specter/impl.cljx | 13 ------------ 2 files changed, 11 insertions(+), 36 deletions(-) diff --git a/src/clj/com/rpl/specter.cljx b/src/clj/com/rpl/specter.cljx index 0889a3c..423b303 100644 --- a/src/clj/com/rpl/specter.cljx +++ b/src/clj/com/rpl/specter.cljx @@ -248,29 +248,6 @@ (transform* [this structure next-fn] (i/codewalk-until afn next-fn structure))) -(defn filterer - "Navigates to a view of the current sequence that only contains elements that - match the given selector path. An element matches the selector path if calling select - on that element with the selector path yields anything other than an empty sequence. - - The input path may be parameterized, in which case the result of filterer - will be parameterized in the order of which the parameterized selectors - were declared." - [& path] - (fixed-pathed-path [late path] - (select* [this structure next-fn] - (->> structure (filter #(i/selected?* late %)) doall next-fn)) - (transform* [this structure next-fn] - (let [[filtered ancestry] (i/filter+ancestry late structure) - ;; the vec is necessary so that we can get by index later - ;; (can't get by index for cons'd lists) - next (vec (next-fn filtered))] - (reduce (fn [curr [newi oldi]] - (assoc curr oldi (get next newi))) - (vec structure) - ancestry)) - ))) - (defn subselect "Navigates to a sequence that contains the results of (select ...), but is a view to the original structure that can be transformed. @@ -347,6 +324,17 @@ structure next-fn)))) +(defn filterer + "Navigates to a view of the current sequence that only contains elements that + match the given selector path. An element matches the selector path if calling select + on that element with the selector path yields anything other than an empty sequence. + + The input path may be parameterized, in which case the result of filterer + will be parameterized in the order of which the parameterized selectors + were declared." + [& path] + (subselect ALL (selected? path))) + (defn transformed "Navigates to a view of the current value by transforming it with the specified selector and update-fn. diff --git a/src/clj/com/rpl/specter/impl.cljx b/src/clj/com/rpl/specter/impl.cljx index bdd4acd..bd44a4e 100644 --- a/src/clj/com/rpl/specter/impl.cljx +++ b/src/clj/com/rpl/specter/impl.cljx @@ -482,19 +482,6 @@ (get-cell ret) )) -(defn filter+ancestry [path aseq] - (let [aseq (vec aseq)] - (reduce (fn [[s m :as orig] i] - (let [e (get aseq i) - pos (count s)] - (if (selected?* path e) - [(conj s e) (assoc m pos i)] - orig - ))) - [[] {}] - (range (count aseq)) - ))) - (defn key-select [akey structure next-fn] (next-fn (get structure akey)))