我想将函数的var-args发送到宏,仍然作为var-args.这是我的代码:
(defmacro test-macro
[& args]
`(println (str "count=" ~(count args) "; args=" ~@args)))
(defn test-fn-calling-macro
[& args]
(test-macro args))
Run Code Online (Sandbox Code Playgroud)
输出(test-macro "a" "b" "c")是我想要的:count=3; args=abc
输出(test-fn-calling-macro "a" "b" "c")是:count=1; args=("a" "b" "c")因为args作为单个参数发送到宏.如何在我的函数中展开这个args以便用3个参数调用宏?
我想我只是错过了一个简单的核心功能,但我无法找到它.谢谢
编辑2 - 我的"真实"代码,如下面的编辑部分所示,不是使用此技术的有效情况.
正如@Brian所指出的,宏xml-to-cass可以用这样的函数替换:
(defn xml-to-cass
[zipper table key attr & path]
(doseq [v (apply zf/xml-> zipper path)] (cass/set-attr! table key attr v)))
Run Code Online (Sandbox Code Playgroud)
编辑 - 以下部分超出了我原来的问题,但欢迎任何见解
上面的代码是我能找到的最简单的代码来查明我的问题.我的真实代码处理clj-cassandra和zip-filter.它也可能看起来过度工程,但它只是一个玩具项目,我正在尝试同时学习这门语言.
我想解析mlb.com上的一些XML,并将找到的值插入到cassandra数据库中.这是我的代码及其背后的思想.
第1步 - 功能正常,但包含代码重复
(ns stats.importer
(:require
[clojure.xml :as xml]
[clojure.zip :as zip]
[clojure.contrib.zip-filter.xml :as zf]
[cassandra.client :as cass]))
(def root-url "http://gd2.mlb.com/components/game/mlb/year_2010/month_05/day_01/")
(def games-table (cass/mk-cf-spec "localhost" 9160 "mlb-stats" "games"))
(defn import-game-xml-1
"Import the content of xml into cassandra"
[game-dir]
(let [url (str root-url game-dir "game.xml")
zipper (zip/xml-zip (xml/parse url))
game-id (.substring game-dir 4 (- (.length game-dir) 1))]
(doseq [v (zf/xml-> zipper (zf/attr :type))] (cass/set-attr! games-table game-id :type v))
(doseq [v (zf/xml-> zipper (zf/attr :local_game_time))] (cass/set-attr! games-table game-id :local_game_time v))
(doseq [v (zf/xml-> zipper :team [(zf/attr= :type "home")] (zf/attr :name_full))] (cass/set-attr! games-table game-id :home_team v))))
Run Code Online (Sandbox Code Playgroud)
参数import-game-xml-1可以是例如"gid_2010_05_01_colmlb_sfnmlb_1/".我删除了"gid_"和尾部斜杠,使其成为我数据库中ColumnFamily游戏的关键.
我发现3 doseq个很多重复(最终版本中应该有3个以上).所以使用宏的代码模板在这里似乎是合适的(如果我错了,请纠正我).
第2步 - 介绍代码模板的宏(仍然有效)
(defmacro xml-to-cass
[zipper table key attr & path]
`(doseq [v# (zf/xml-> ~zipper ~@path)] (cass/set-attr! ~table ~key ~attr v#)))
(defn import-game-xml-2
"Import the content of xml into cassandra"
[game-dir]
(let [url (str root-url game-dir "game.xml")
zipper (zip/xml-zip (xml/parse url))
game-id (.substring game-dir 4 (- (.length game-dir) 1))]
(xml-to-cass zipper games-table game-id :type (zf/attr :type))
(xml-to-cass zipper games-table game-id :local_game_time (zf/attr :local_game_time))
(xml-to-cass zipper games-table game-id :home_team :team [(zf/attr= :type "home")] (zf/attr :name_full))))
Run Code Online (Sandbox Code Playgroud)
我相信这是一个改进但我仍然看到一些重复,总是在我的调用中重复使用相同的3个参数xml-to-cass.那就是我引入了一个中间函数来处理那些.
第3步 - 添加一个函数来调用宏(问题出在这里)
(defn import-game-xml-3
"Import the content of xml into cassandra"
[game-dir]
(let [url (str root-url game-dir "game.xml")
zipper (zip/xml-zip (xml/parse url))
game-id (.substring game-dir 4 (- (.length game-dir) 1))
save-game-attr (fn[key path] (xml-to-cass zipper games-table game-id key path))]
(save-game-attr :type (zf/attr :type)) ; works well because path has only one element
(save-game-attr :local_game_time (zf/attr :local_game_time))
(save-game-attr :home :team [(zf/attr= :type "home"] (zf/attr :name_full))))) ; FIXME this final line doesn't work
Run Code Online (Sandbox Code Playgroud)
这是一些可能具有启发性的简单代码。
宏与代码生成有关。如果您希望在运行时发生这种情况,出于某种原因,那么您必须在运行时构建和评估代码。这可能是一项强大的技术。
(defmacro test-macro
[& args]
`(println (str "count=" ~(count args) "; args=" ~@args)))
(defn test-fn-calling-macro
[& args]
(test-macro args))
(defn test-fn-expanding-macro-at-runtime
[& args]
(eval (cons `test-macro args)))
(defmacro test-macro-expanding-macro-at-compile-time
[& args]
(cons `test-macro args))
;; using the splicing notation
(defmacro test-macro-expanding-macro-at-compile-time-2
[& args]
`(test-macro ~@args))
(defn test-fn-expanding-macro-at-runtime-2
[& args]
(eval `(test-macro ~@args)))
(test-macro "a" "b" "c") ;; count=3; args=abc nil
(test-fn-calling-macro "a" "b" "c") ;; count=1; args=("a" "b" "c") nil
(test-fn-expanding-macro-at-runtime "a" "b" "c") ; count=3; args=abc nil
(test-macro-expanding-macro-at-compile-time "a" "b" "c") ; count=3; args=abc nil
(test-macro-expanding-macro-at-compile-time-2 "a" "b" "c") ; count=3; args=abc nil
(test-fn-expanding-macro-at-runtime "a" "b" "c") ; count=3; args=abc nil
Run Code Online (Sandbox Code Playgroud)
如果对上述内容的思考没有得到启发,我可以推荐几篇我自己的博客文章吗?
在这一章中,我从头开始介绍宏,以及 clojure 的工作原理:
http://www.learningclojure.com/2010/09/clojure-macro-tutorial-part-i-getting.html
在这一章中,我将展示为什么运行时代码生成可能有用:
http://www.learningclojure.com/2010/09/clojure-faster-than-machine-code.html