我有这个功能:
(defn handler [request]
(case (request :uri)
"/" (home request)
"/good" (good request)
"/evil" (evil request)
"/neutral" (neutral request)
(status-response 404 (str "<h1>404 Not Found: " (:uri request) "</h1>" ))))
但我一直在更改页面列表-which-resolve-to-functions-with-the-same-name,我希望能够写:
(def-handler 善恶中立)
反而:
但我被困住了。到目前为止,我最好的镜头如下:
(defmacro def-handler [& addresses]
`(defn handler [request#]
(case (request# :uri)
~@(mapcat (fn[x] [(str "/" x) (list x 'request)]) addresses)
"/" (home request#)
(status-response 404 (str "<h1>404 Not Found: " (:uri request#) "</h1>" )))))
但它不太有效,因为生成的调用中的请求不是 gensym,我不知道如何在其中获取 gensym。
这看起来很有希望,直到我注意到它产生了一个新的 gensym:
(defmacro def-handler [& addresses]
`(defn handler [request#]
(case (request# :uri)
~@(mapcat (fn[x] [(str "/" x) `( ~x request#)]) addresses)
"/" (home request#)
(status-response 404 (str "<h1>404 Not Found: " (:uri request#) "</h1>" )))))