From 8293f6869633023e184a12903185f2144e5db626 Mon Sep 17 00:00:00 2001 From: Nathan Marz Date: Sun, 31 Jan 2016 10:01:08 -0800 Subject: [PATCH] allow defprotocolpath to be defined with no params argument for consistency with declarepath --- CHANGES.md | 1 + src/clj/com/rpl/specter/macros.clj | 157 +++++++++++++++-------------- 2 files changed, 80 insertions(+), 78 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 71e3148..9a12977 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ * ALL on maps auto-coerces MapEntry to vector, enabling smoother transformation of map keys * declarepath can now be parameterized * Added params-reset which calls its path with the params index reset to 0. This enables recursive parameterized paths +* Added convenience syntax for defprotocolpath with no params, e.g. (defprotocolpath foo) ## 0.9.2 * Added VOID selector which navigates nowhere diff --git a/src/clj/com/rpl/specter/macros.clj b/src/clj/com/rpl/specter/macros.clj index ee513c0..2763dc7 100644 --- a/src/clj/com/rpl/specter/macros.clj +++ b/src/clj/com/rpl/specter/macros.clj @@ -205,91 +205,92 @@ (defn- protpath-sym [name] (-> name (str "-prot") symbol)) -(defmacro defprotocolpath [name params] - (let [prot-name (protpath-sym name) - m (-> name (str "-retrieve") symbol) - num-params (count params) - ssym (gensym "structure") - rargs [(gensym "params") (gensym "pidx") (gensym "vals") ssym (gensym "next-fn")] - retrieve `(~m ~ssym) - ] - `(do - (defprotocol ~prot-name (~m [structure#])) - (def ~name - (if (= ~num-params 0) - (no-params-compiled-path - (->TransformFunctions - RichPathExecutor - (fn ~rargs - (let [path# ~retrieve - selector# (compiled-selector path#)] - (selector# ~@rargs) - )) - (fn ~rargs - (let [path# ~retrieve - transformer# (compiled-transformer path#)] - (transformer# ~@rargs) - )))) - (->ParamsNeededPath - (->TransformFunctions - RichPathExecutor - (fn ~rargs - (let [path# ~retrieve - selector# (params-needed-selector path#)] - (selector# ~@rargs) - )) - (fn ~rargs - (let [path# ~retrieve - transformer# (params-needed-transformer path#)] - (transformer# ~@rargs) - ))) - ~num-params - ) - ))))) +(defmacro defprotocolpath + ([name] + `(defprotocolpath ~name [])) + ([name params] + (let [prot-name (protpath-sym name) + m (-> name (str "-retrieve") symbol) + num-params (count params) + ssym (gensym "structure") + rargs [(gensym "params") (gensym "pidx") (gensym "vals") ssym (gensym "next-fn")] + retrieve `(~m ~ssym) + ] + `(do + (defprotocol ~prot-name (~m [structure#])) + (def ~name + (if (= ~num-params 0) + (no-params-compiled-path + (->TransformFunctions + RichPathExecutor + (fn ~rargs + (let [path# ~retrieve + selector# (compiled-selector path#)] + (selector# ~@rargs) + )) + (fn ~rargs + (let [path# ~retrieve + transformer# (compiled-transformer path#)] + (transformer# ~@rargs) + )))) + (->ParamsNeededPath + (->TransformFunctions + RichPathExecutor + (fn ~rargs + (let [path# ~retrieve + selector# (params-needed-selector path#)] + (selector# ~@rargs) + )) + (fn ~rargs + (let [path# ~retrieve + transformer# (params-needed-transformer path#)] + (transformer# ~@rargs) + ))) + ~num-params + ) + )))))) (defn declared-name [name] (symbol (str name "-declared"))) -(defn- declarepath* [name num-params] - (let [declared (declared-name name) - rargs [(gensym "params") (gensym "pidx") (gensym "vals") - (gensym "structure") (gensym "next-fn")]] - `(do - (declare ~declared) - (def ~name - (if (= ~num-params 0) - (no-params-compiled-path - (->TransformFunctions - RichPathExecutor - (fn ~rargs - (let [selector# (compiled-selector ~declared)] - (selector# ~@rargs) - )) - (fn ~rargs - (let [transformer# (compiled-transformer ~declared)] - (transformer# ~@rargs) - )))) - (->ParamsNeededPath - (->TransformFunctions - RichPathExecutor - (fn ~rargs - (let [selector# (params-needed-selector ~declared)] - (selector# ~@rargs) - )) - (fn ~rargs - (let [transformer# (params-needed-transformer ~declared)] - (transformer# ~@rargs) - ))) - ~num-params - ) - ))))) - - (defmacro declarepath - ([name] (declarepath* name 0)) + ([name] + `(declarepath ~name [])) ([name params] - (declarepath* name (count params)))) + (let [num-params (count params) + declared (declared-name name) + rargs [(gensym "params") (gensym "pidx") (gensym "vals") + (gensym "structure") (gensym "next-fn")]] + `(do + (declare ~declared) + (def ~name + (if (= ~num-params 0) + (no-params-compiled-path + (->TransformFunctions + RichPathExecutor + (fn ~rargs + (let [selector# (compiled-selector ~declared)] + (selector# ~@rargs) + )) + (fn ~rargs + (let [transformer# (compiled-transformer ~declared)] + (transformer# ~@rargs) + )))) + (->ParamsNeededPath + (->TransformFunctions + RichPathExecutor + (fn ~rargs + (let [selector# (params-needed-selector ~declared)] + (selector# ~@rargs) + )) + (fn ~rargs + (let [transformer# (params-needed-transformer ~declared)] + (transformer# ~@rargs) + ))) + ~num-params + ) + )))))) (defmacro providepath [name apath] `(let [comped# (comp-paths* ~apath)