Clojure macro for string template replacement
This is my first Clojure macro -- I am an uber-noob.
Yesterday I posted and refined a string template replacement function. Several people suggested that the keys could be replaced at compile-time. Here is my first attempt:
(defn replace-templates*
"Return a String with each occurrence of a substring of the form {key}
replaced with the corresponding value from a map parameter.
@param str the String in which to do the replacements
@param m a map of template->value
@thanks kotarak http开发者_如何学Cs://stackoverflow.com/questions/6112534/
follow-up-to-simple-string-template-replacement-in-scala-and-clojure"
[^String text m]
(let [builder (StringBuilder.)]
(loop [text text]
(cond
(zero? (count text))
(.toString builder)
(.startsWith text "{")
(let [brace (.indexOf text "}")]
(if (neg? brace)
(.toString (.append builder text))
(if-let [[_ replacement] (find m (subs text 1 brace))]
(do
(.append builder replacement)
(recur (subs text (inc brace))))
(do
(.append builder "{")
(recur (subs text 1))))))
:else
(let [brace (.indexOf text "{")]
(if (neg? brace)
(.toString (.append builder text))
(do
(.append builder (subs text 0 brace))
(recur (subs text brace)))))))))
(def foo* 42)
(def m {"foo" foo*})
(defmacro replace-templates
[text m]
(if (map? m)
`(str
~@(loop [text text acc []]
(cond
(zero? (count text))
acc
(.startsWith text "{")
(let [brace (.indexOf text "}")]
(if (neg? brace)
(conj acc text)
(if-let [[_ replacement] (find m (subs text 1 brace))]
(recur (subs text (inc brace)) (conj acc replacement))
(recur (subs text 1) (conj acc "{")))))
:else
(let [brace (.indexOf text "{")]
(if (neg? brace)
(conj acc text)
(recur (subs text brace) (conj acc (subs text 0 brace))))))))
`(replace-templates* ~text m)))
(macroexpand '(replace-templates "this is a {foo} test" {"foo" foo*}))
;=> (clojure.core/str "this is a " foo* " test")
(println (replace-templates "this is a {foo} test" {"foo" foo*}))
;=> this is a 42 test
(macroexpand '(replace-templates "this is a {foo} test" m))
;=> (user/replace-templates* "this is a {foo} test" user/m)
(println (replace-templates "this is a {foo} test" m))
;=> this is a 42 test
Is there a better way to write this macro? In particular, the expanded version of each value is not getting namespace-qualified.
I would try reduce the repeated stuff. I adjusted the function to use your macro approach of an accumulator and let replace-templates*
do the rest via (apply str ...)
. In that way one can re-use the function in the macro.
(defn extract-snippets
[^String text m]
(loop [text text
snippets []]
(cond
(zero? (count text))
snippets
(.startsWith text "{")
(let [brace (.indexOf text "}")]
(if (neg? brace)
(conj snippets text)
(if-let [[_ replacement] (find m (subs text 1 brace))]
(recur (subs text (inc brace)) (conj snippets replacement))
(recur (subs text 1) (conj snippets \{)))))
:else
(let [brace (.indexOf text "{")]
(if (neg? brace)
(conj snippets text)
(recur (subs text brace) (conj snippets (subs text 0 brace))))))))
(defn replace-templates*
[text m]
(apply str (extract-snippets text m)))
(defmacro replace-templates
[text m]
(if (map? m)
`(apply str ~(extract-snippets text m))
`(replace-templates* ~text ~m)))
Note: in your macro, you didn't unquote the m
. So it only works, because you had def'd it before. It wouldn't with (let [m {"a" "b"}] (replace-templates "..." m))
.
Change (defn m {"foo" foo*})
to (def m {"foo" foo*})
and it seems to work.
精彩评论