String 用于字符串模板替换的Clojure宏

String 用于字符串模板替换的Clojure宏,string,macros,clojure,String,Macros,Clojure,这是我的第一个Clojure宏——我是uber noob 昨天我改进了一个字符串模板替换函数。一些人建议可以在编译时替换密钥。这是我的第一次尝试: (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 t

这是我的第一个Clojure宏——我是uber noob

昨天我改进了一个字符串模板替换函数。一些人建议可以在编译时替换密钥。这是我的第一次尝试:

(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 https://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

有没有更好的方法来编写这个宏?特别是,每个值的扩展版本没有获得名称空间限定。

(defn m{“foo”foo*})
更改为
(def m{“foo”foo*})
,它似乎可以工作。

我会尝试减少重复的内容。我调整了函数以使用累加器的宏方法,并让
替换模板*
通过
(应用str…
)执行其余操作。这样就可以在宏中重复使用该函数

(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)))

注意:在宏中,您没有解压缩
m
。所以它只起作用,因为你以前定义过它。使用
(让[m{“a”“b”}(替换模板“…”m))

使用
(map?m)
:)这个主意不错谢谢你的重构。我正在读《让Lambda过去》(Doug Hoyte)——这本书都是关于常见的Lisp宏的。还感谢您在
~m
上给出的答案。我之前试过,但由于宏中的其他问题,没有注意到这是“正确”的答案。