From 6580f4df4b5e293fc59a78effda285caf49a1811 Mon Sep 17 00:00:00 2001 From: Nathan Marz Date: Tue, 7 Jun 2016 10:47:18 -0400 Subject: [PATCH] added selected-any? helper operation --- src/clj/com/rpl/specter.cljx | 8 ++++++++ src/clj/com/rpl/specter/impl.cljx | 3 +++ src/clj/com/rpl/specter/macros.clj | 8 ++++++++ 3 files changed, 19 insertions(+) diff --git a/src/clj/com/rpl/specter.cljx b/src/clj/com/rpl/specter.cljx index 4a2bc92..bc2ecf7 100644 --- a/src/clj/com/rpl/specter.cljx +++ b/src/clj/com/rpl/specter.cljx @@ -96,6 +96,14 @@ [path structure] (compiled-select-any (i/comp-paths* path) structure)) +(def ^{:doc "Version of selected-any? that takes in a path pre-compiled with comp-paths"} + compiled-selected-any? i/compiled-selected-any?*) + +(defn selected-any?* + "Returns true if any element was selected, false otherwise." + [path structure] + (compiled-selected-any? (i/comp-paths* path) structure)) + ;; Transformation functions (def ^{:doc "Version of transform that takes in a path pre-compiled with comp-paths"} diff --git a/src/clj/com/rpl/specter/impl.cljx b/src/clj/com/rpl/specter/impl.cljx index c53766e..d9ed666 100644 --- a/src/clj/com/rpl/specter/impl.cljx +++ b/src/clj/com/rpl/specter/impl.cljx @@ -665,6 +665,9 @@ (defn compiled-select-any* [path structure] (compiled-traverse* path identity structure)) +(defn compiled-selected-any?* [path structure] + (not= NONE (compiled-select-any* path structure))) + (defn compiled-transform* [^com.rpl.specter.impl.CompiledPath path transform-fn structure] (let [^com.rpl.specter.impl.TransformFunctions tfns (.-transform-fns path) diff --git a/src/clj/com/rpl/specter/macros.clj b/src/clj/com/rpl/specter/macros.clj index 23ad7f2..1f2caaa 100644 --- a/src/clj/com/rpl/specter/macros.clj +++ b/src/clj/com/rpl/specter/macros.clj @@ -574,6 +574,14 @@ [apath structure] `(i/compiled-select-any* (path ~apath) ~structure)) +(defmacro selected-any? + "Returns true if any element was selected, false otherwise. + This macro will attempt to do inline factoring and caching of the path, falling + back to compiling the path on every invocation it it's not possible to + factor/cache the path." + [apath structure] + `(i/compiled-selected-any?* (path ~apath) ~structure)) + (defmacro transform "Navigates to each value specified by the path and replaces it by the result of running the transform-fn on it.