Clojure:正确的condp用法

时间:2015-08-14 04:19:00

标签: syntax clojure functional-programming conditional hiccup

我正在改编我的世界历史网站(history.clmitchell.net),并且取得了很大的进步。我已经达到了这样的程度,我需要使用条件输出打嗝代码来根据我正在加载的页面来声明CSS链接。我仔细阅读了文档,但无法理解这一点:

(defn cssfiles
  "load sets of CSS files depending on which page is being served"
  [pageName]
  (condp = pageName
         "index" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/wc3xhtml1.css")
         "add" (hic/include-css "/css/hcspry.css"
                                "/css/menus.css"
                                "/css/addform.css")
         "map" (hic/include-css "/css/hcspry.css"
                                "/css/menus.css"
                                "/css/SpryCollapsiblePanel.css"
                                "/css/SpryAccordion.css")
         "chart" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/cha.css")
         "learn" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/wc3xhtml1.css"
                                  "/css/doc.css")
         "changes" (hic/include-css "/css/hcspry.css"
                                    "/css/menus.css"
                                    "/css/filter.css"
                                    "/css/wc3xhtml1.css"
                                    "/css/clg.css")
         "seek" (hic/include-css "/css/hcspry.css"
                                 "/css/menus.css"))
  (println (str  "PRGMR. MSG: net.clm.history.pages.snippets.clj::(defn cssfiles [" pageName "]): unknown parameter or parameter not processed properly.")))

问题:这种功能的正确语法是什么?

3 个答案:

答案 0 :(得分:2)

您可以将page-css映射定义为单独的纯数据结构,并避免重复调用hic / include-css:

(def page-css-mapping
  {"index"   ["/css/hcspry.css"
              "/css/menus.css"
              "/css/filter.css"
              "/css/wc3xhtml1.css"]
   "add"     ["/css/hcspry.css"
              "/css/menus.css"
              "/css/addform.css"]
   "map"     ["/css/hcspry.css"
              "/css/menus.css"
              "/css/SpryCollapsiblePanel.css"
              "/css/SpryAccordion.css"]
   "chart"   ["/css/hcspry.css"
              "/css/menus.css"
              "/css/filter.css"
              "/css/cha.css"]
   "learn"   ["/css/hcspry.css"
              "/css/menus.css"
              "/css/filter.css"
              "/css/wc3xhtml1.css"
              "/css/doc.css"]
   "changes" ["/css/hcspry.css"
              "/css/menus.css"
              "/css/filter.css"
              "/css/wc3xhtml1.css"
              "/css/clg.css"]
   "seek"    ["/css/hcspry.css"
              "/css/menus.css"]})

(defn cssfiles
  "load sets of CSS files depending on which page is being served"
  [pageName]
  (if-let [css-files (get page-css-mapping pageName)]
    (apply hic/include-css css-files)
    (println (str "PRGMR. MSG: net.clm.history.pages.snippets.clj::(defn cssfiles [" pageName "]): unknown parameter or parameter not processed properly."))))

现在,您还可以将映射存储为纯数据结构(例如EDN文件或Datomic),而不是硬编码。

答案 1 :(得分:1)

你是说你不确定cssfiles正在做什么?在这种情况下,函数说:

如果我的参数pagename等于字符串"index",则执行可以在include-css缩写的命名空间中找到的函数hic,并传递此函数参数"/css/hcspry.css""/css/menus.css",...,并将结果值作为我的(即cssfiles')返回值返回。

如果相反pagename等于"add",则使用不同的参数集执行include-css ...,返回结果值....(等等)

依此类推其他可预测值为pagename的字符串。

但是如果pagename与我测试的任何字符串都不匹配,那么在println表达式的末尾执行condp语句以打印出错误消息。

有关语法的更多信息,请参阅condp的文档。如果需要,我很乐意提供更多细节。

答案 2 :(得分:0)

dskrvk是对的:println表格不在condp表格内;这里是纠正的结果:

(defn cssfiles
  "load sets of CSS files depending on which page is being served"
  [pageName]
  (condp = pageName
         "index" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/wc3xhtml1.css")
         "add" (hic/include-css "/css/hcspry.css"
                                "/css/menus.css"
                                "/css/addform.css")
         "map" (hic/include-css "/css/hcspry.css"
                                "/css/menus.css"
                                "/css/SpryCollapsiblePanel.css"
                                "/css/SpryAccordion.css")
         "chart" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/cha.css")
         "learn" (hic/include-css "/css/hcspry.css"
                                  "/css/menus.css"
                                  "/css/filter.css"
                                  "/css/wc3xhtml1.css"
                                  "/css/doc.css")
         "changes" (hic/include-css "/css/hcspry.css"
                                    "/css/menus.css"
                                    "/css/filter.css"
                                    "/css/wc3xhtml1.css"
                                    "/css/clg.css")
         "seek" (hic/include-css "/css/hcspry.css"
                                 "/css/menus.css")
         (println (str  "PRGMR. MSG: net.clm.history.pages.snippets.clj::(defn cssfiles [" pageName "]): unknown parameter or parameter not processed properly."))))