8

我正在尝试创建一个我以前使用 python 编写过的表(一个工作计划),我认为这对我来说是对 Clojure 语言的一个很好的介绍。

我在 Clojure(或 lisp 方面)方面的经验很少,而且我已经在 google 中进行了几次试验和错误,但似乎无法理解这种编码风格。

这是我的示例数据(将来将来自 sqlite 数据库):

(def smpl2 (ref {"Salaried" 
             [{"John Doe" ["12:00-20:00" nil nil nil "11:00-19:00"]}
              {"Mary Jane" [nil "12:00-20:00" nil nil nil "11:00-19:00"]}]
             "Shift Manager"
             [{"Peter Simpson" ["12:00-20:00" nil nil nil "11:00-19:00"]}
              {"Joe Jones" [nil "12:00-20:00" nil nil nil "11:00-19:00"]}]
             "Other"
             [{"Super Man" ["07:00-16:00" "07:00-16:00" "07:00-16:00" 
                       "07:00-16:00" "07:00-16:00"]}]}))

我试图通过这个最初使用for然后移动到doseq最后是 domap(这似乎更成功)并将内容转储到 html 表中(我原来的 python 程序将它从 sqlite 数据库输出到使用 COM 的 excel 电子表格中)。

这是我的尝试(create-table fn):

(defn html-doc [title & body] 
  (html (doctype "xhtml/transitional") 
    [:html [:head [:title title]] [:body body]])) 

(defn create-table []
  [:h1 "Schedule"]
  [:hr]
  [:table (:style "border: 0; width: 90%")
   [:th "Name"][:th "Mon"][:th "Tue"][:th "Wed"]
   [:th "Thur"][:th "Fri"][:th "Sat"][:th "Sun"]
   [:tr
    (domap [ct @smpl2] 
       [:tr [:td (key ct)]
        (domap [cl (val ct)]
           (domap [c cl]
              [:tr [:td (key c)]]))])
    ]])

(defroutes tstr
  (GET "/" ((html-doc "Sample" create-table)))
  (ANY "*" 404))

输出带有部分(受薪,经理等)和部分中的名称的表,我只是觉得我通过嵌套太多次来滥用 domap,因为我可能需要添加更多的 domap 只是为了得到正确列中的班次时间和代码给人一种“肮脏”的感觉。

如果我没有提供足够的信息,我会提前道歉,我通常不会寻求编码方面的帮助,这也是我的第一个 SO 问题:)。

如果您知道任何更好的方法来做到这一点,甚至我作为新手应该知道的提示或技巧,他们绝对是受欢迎的。

谢谢。

4

2 回答 2

4

没有办法避免某种嵌套循环。但是您根本不需要domap,Compojure 足够聪明(有时)可以为您扩展序列。 list并且足够了mapfor例如 Michał Marczyk 的回答,或者:

(defn map-tag [tag xs]
  (map (fn [x] [tag x]) xs))

(defn create-table []
  (list
   [:h1 "Schedule"]
   [:hr]
   [:table {:style "border: 0; width: 90%"}
    [:tr (map-tag :th ["Name" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun"])]
    [:tr (for [[category people] smpl2]
           (list* [:tr [:td category]]
                  (for [person people
                        [name hours] person]
                    [:tr [:td name] (map-tag :td hours)])))]]))

“映射 seq 并将所有内容包装在同一个标​​签中”模式很常见,以至于我有时喜欢使用辅助函数。

Compojureseq为您扩展了一个级别。因此,您可以将一些东西放入 alist以使标签按顺序出现在您的 HTML 输出中,我这样做是为了让 h1 和 hr 显示出来。在您的代码中,您只是将 h1 和 hr 扔掉了。

但请注意,它只扩展了一层。当您有一个列表列表或序列列表时,外部序列将扩展但内部序列不会。

user> (println (html (list [:div "foo"] (for [x [1 2 3]] [:div x]))))
<div>foo</div>clojure.lang.LazySeq@ea73bbfd

看看内部 seq 如何让 Compojure 吐槽。看看compojure.html.gen/expand-seqs它是如何工作的,或者如果你愿意,可以更改/修复它。

当您在 a 中嵌套for或 a时,这可能是一个问题,因为返回一个惰性序列。但是您只需要避免使用 seq-in-a-seq。我用上面的。和的组合也可以。还有很多其他的方法。forlistforlist*listhtml

user> (println (html (list* [:div "foo"] (for [x [1 2 3]] [:div x]))))
<div>foo</div><div>1</div><div>2</div><div>3</div>

user> (println (html (list [:div "foo"] (html (for [x [1 2 3]] [:div x])))))
<div>foo</div><div>1</div><div>2</div><div>3</div>
于 2010-03-15T07:43:35.037 回答
3

我认为您的代码存在一些小问题。我已经尝试在下面的代码中更正它们。使用 Compojure 0.3.2 进行测试,我敢说它有效。(当然,请随意指出任何需要改进或似乎不适合您的地方。)

(use 'compojure) ; you'd use a ns form normally

;;; I'm not using a ref here; this doesn't change much,
;;; though with a ref / atom / whatever you'd have to take care
;;; to dereference it once per request so as to generate a consistent
;;; (though possibly outdated, of course) view of data;
;;; this doesn't come into play here anyway
(def smpl2 {"Salaried"      [{"John Doe" ["12:00-20:00" nil nil nil "11:00-19:00"]}
                             {"Mary Jane" [nil "12:00-20:00" nil nil nil "11:00-19:00"]}]
            "Shift Manager" [{"Peter Simpson" ["12:00-20:00" nil nil nil "11:00-19:00"]}
                             {"Joe Jones" [nil "12:00-20:00" nil nil nil "11:00-19:00"]}]
            "Other"         [{"Super Man" ["07:00-16:00" "07:00-16:00" "07:00-16:00" 
                                           "07:00-16:00" "07:00-16:00"]}]})

(defn html-doc [title & body] 
  (html (doctype :xhtml-transitional) ; the idiomatic way to insert
                                      ; the xtml/transitional doctype
        [:html
         [:head [:title title]]
         [:body body]]))

(defn create-table []
  (html
   [:h1 "Schedule"]
   [:hr]
   [:table {:style "border: 0; width: 90%;"}
    [:tr
     [:th "Name"][:th "Mon"][:th "Tue"][:th "Wed"]
     [:th "Thur"][:th "Fri"][:th "Sat"][:th "Sun"]]
    (for [category smpl2]
      [:div [:tr [:td (key category)]] ; for returns just one thing per
                                       ; 'iteration', so I'm using a div
                                       ; to package two things together;
                                       ; it could be avoided, so tell me
                                       ; if it's a problem
       (for [people (val category)]
         (for [person people]
           [:tr
            [:td (key person)]
            (for [hours (val person)]
              [:td hours])]))])]))

(defn index-html [request]
  (html-doc "Sample" (create-table)))

(defroutes test-routes
  (GET "/" index-html)
  (ANY "*" 404))

(defserver test-server
  {:port 8080}
  "/*"
  (servlet test-routes))
于 2010-03-15T01:57:10.047 回答