2012-05-15 6 views
3

내가 딸꾹질 데이터 구조를 나타내는 벡터 트리를 탐색 할 벡터 트리를 탐색 원래의 태그를 대체 할 다른 벡터 집합을 반환합니다. 파라미터로서 ("안녕하세요", "안녕하세요")리스트 수신처

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

맞춤 multimethod 예 :

, 상기 구조로 변환한다. 그런 다음 버튼이 포함 된 div를 반환합니다.

벡터를 탐색하고 매개 변수로 양식의 다른 모든 항목을 키워드에 전달한 다음 현재 양식을 반환 된 양식으로 바꿔주는 함수를 작성하려면 어떻게해야합니까? 그것을 사용

답변

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 [email protected]))) 

(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"}]]] 
+0

다니, 당신의 답장을 보내 주셔서 감사합니다. 그렇다면 전체 트리를 데이터 구조로 표현하는 데 가치가 없다고 말하는 이유는 무엇입니까? Hiccup은 HTML 트리를 벡터 트리로 조작 할 수 있기 때문에 테이블에 값을 제공하므로 커스텀 태그도 마찬가지로 표현할 수 있다면 도움이되지 않을까요? 나는 학습의 정신으로 이것을 요구하고 있으므로, 당신의 의견을 알려주십시오. – murtaza52

+0

당신의 요점을 봅니다. 내 응답 업데이트 중. – DanLebrero

+0

훌륭한 코드를 보내 주셔서 감사합니다! – murtaza52