Clojure扩展读者宏

时间:2016-06-17 13:10:48

标签: clojure

我们怎样才能找出读者宏的作用?

例如,我知道正则表达式#"abc"(re-pattern "abc")相同。

但是以下内容:(macroexpand #"abc" )会产生#"abc",那么我怎样才能从读者特殊形式转到"正常"功能形式?

1 个答案:

答案 0 :(得分:3)

您可以使用read-string

了解读者扩展到的内容
(read-string "`(rest ~f)")
=>
(clojure.core/seq
 (clojure.core/concat (clojure.core/list (quote clojure.core/rest)) (clojure.core/list f)))
(read-string "#(x %&)")
=> (fn* [& rest__92046#] (x rest__92046#))
(read-string "`(~@x)")
=> (clojure.core/seq (clojure.core/concat x))
(read-string "`(~x)")
=> (clojure.core/seq (clojure.core/concat (clojure.core/list x)))
(read-string "'map")
=> (quote map)
(read-string "#'map")
=> (var map)
(read-string "@x")
=> (clojure.core/deref x)
(binding [*print-meta* true]
  (pr-str (read-string "(def ^:foo x 'map)")))
=> "(def ^{:foo true} x (quote map))"
(read-string "#\"x\"")
=> #"x"

在您的特定情况下:正则表达式以同样的方式打印给您,因为that's how it's printed in clojure

(class #"abc")
=> java.util.regex.Pattern