如何解决Clojure宏,用于替换字符串模板
| 这是我的第一个Clojure宏-我是超级用户。 昨天我发布并完善了字符串模板替换功能。一些人建议可以在编译时替换密钥。这是我的第一次尝试:(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
有没有更好的方法来编写此宏?特别是,每个值的扩展版本都没有获得名称空间限定。
解决方法
我会尽量减少重复的东西。我调整了函数以使用您的累加器宏方法,然后让
replace-templates*
通过(apply 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)))
注意:在宏中,您没有取消对ѭ4的引用。因此它只能工作,因为您之前已经对其进行过定义。 with5ѭ不会。
,将(defn m {\"foo\" foo*})
更改为(def m {\"foo\" foo*})
,它似乎可以工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。