我想遍历表示打嗝数据结构的矢量树:
[:div {:class "special"} [:btn-grp '("Hello" "Hi")]]
然后我想调度向量的关键字,如果已经为关键字定义了一个多方法,那么它将返回另一组向量,它将替换原始标记。
例如,上述结构将转换为:
[:div {:class "special"} [:div [:button "Hello"] [:button "Hi"]]]
自定义多方法将接收列表(“hello”“hi”)作为参数。然后它将返回包含按钮的div。
如何编写遍历向量的函数,并将表单中的所有其他内容作为参数调度到关键字,然后用返回的表单替换当前表单?
答案 0 :(得分: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"}]]]