如何在clojure中使用宏来查看多行的xy-plot?

时间:2013-02-09 19:37:17

标签: macros clojure incanter

我尝试编写以数据集为参数的宏,并在单个xy-plot上查看数据集中的所有数据。 例如,我创建数据集

(def test-data
 [["RECALL" "CAFE" "CLIPPERS"]
 [0 0 0]
 [14 15 13]
 [160 146 155]])

并写下这个

(defmacro figure
  [datas]
  (let [x `(range 0 (nrow ~datas)) y `(rest (:column-names ~datas))]
   `(let [datas# ~datas]
      (with-data datas#
        (doto
          (xy-plot ~x ($ (first (:column-names datas#))))
          ~@(map (fn [arg] `(add-lines ~x ($ ~arg ))) (eval y));;this line, wheh rest of columns have added to xy plot, get me a trouble
          view))))) 
(figure test-data)  

但是代码中的eval存在问题。我认为,这不是Clojure惯用的方式,在某些情况下这不起作用。 我已经尝试了各种各样的疯狂技巧来获取数据集的列名作为评估参数,但这不起作用。

在宏中扩展时是否存在eval表达式的存在方法?

1 个答案:

答案 0 :(得分:0)

您不需要在此处使用宏。普通功能可以完成这项工作:

(defn figure [data]
  (let [x (range (nrow data))
        [h & t] (:column-names data)]
    (with-data data
      (let [plot (xy-plot x ($ h))]
        (doseq [col-name t]
          (add-lines plot x ($ col-name)))
        (view plot)))))