3

我想遍历代表打嗝数据结构的向量树:

[:div {:class "special"} [:btn-grp '("Hello" "Hi")]] 

然后我想对向量的关键字进行调度,如果已经为关键字定义了多方法,那么它将返回另一组向量,它将替换原始标签。

例如,上面的结构将转换为:

[:div {:class "special"} [:div [:button "Hello"] [:button "Hi"]]]

自定义多方法将接收列表(“hello”“hi”)作为参数。然后它将返回包含按钮的 div。

如何编写一个遍历向量并以表单中的所有其他内容作为参数调度关键字的函数,然后用返回的表单替换当前表单?

4

1 回答 1

1
(ns customtags
  (:require [clojure.walk :as walk]))

(def customtags (atom {}))

(defn add-custom-tag [tag f]
  (swap! customtags assoc tag f))

(defn try-transform [[tag & params :as coll]]
  (if-let [f (get @customtags tag)]
    (apply f params)
    coll))

(defmacro defcustomtag [tag params & body]
  `(add-custom-tag ~tag (fn ~params ~@body)))

(defn apply-custom-tags [coll]
  (walk/prewalk
    (fn [x]
      (if (vector? x)
        (try-transform x)
        x)) coll))

使用它:

(require '[customtags :as ct])
(ct/defcustomtag :btn-grp [& coll] (into [:div] (map (fn [x] [:button x]) coll)))
(ct/defcustomtag :button [name] [:input {:type "button" :id name}])

(def data [:div {:class "special"} [:btn-grp "Hello" "Hi"]])

(ct/apply-custom-tags data)
[:div {:class "special"} [:div [:input {:type "button", :id "Hello"}] [:input {:type "button", :id "Hi"}]]]
于 2012-05-15T10:18:40.080 回答