diff --git a/src/clj/com/rpl/specter.cljc b/src/clj/com/rpl/specter.cljc index 948ccff..308e873 100644 --- a/src/clj/com/rpl/specter.cljc +++ b/src/clj/com/rpl/specter.cljc @@ -358,6 +358,11 @@ [apath structure] `(i/do-compiled-traverse (path ~apath) ~structure)) + (defmacro traverse-all + "Returns a transducer that traverses over each element with the given path." + [apath] + `(i/compiled-traverse-all* (path ~apath))) + (defmacro replace-in "Similar to transform, except returns a pair of [transformed-structure sequence-of-user-ret]. The transform-fn in this case is expected to return [ret user-ret]. ret is @@ -512,6 +517,10 @@ [apath structure] (compiled-traverse (i/comp-paths* apath) structure)) +(def compiled-traverse-all i/compiled-traverse-all*) + +(defn traverse-all* [apath] (compiled-traverse-all (i/comp-paths* apath))) + ;; Transformation functions (def ^{:doc "Version of transform that takes in a path precompiled with comp-paths"} diff --git a/src/clj/com/rpl/specter/impl.cljc b/src/clj/com/rpl/specter/impl.cljc index 718d2da..93a45a6 100644 --- a/src/clj/com/rpl/specter/impl.cljc +++ b/src/clj/com/rpl/specter/impl.cljc @@ -316,6 +316,20 @@ )))) +(defn compiled-traverse-all* [path] + (fn [xf] + (fn + ([] (xf)) + ([result] (xf result)) + ([result input] + (reduce + (fn [r i] + (xf r i)) + result + (do-compiled-traverse path input) + ) + )))) + (defn compiled-select* [path structure] (let [res (mutable-cell (transient [])) result-fn (fn [structure]