我正在尝试创建一个我以前使用 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 问题:)。
如果您知道任何更好的方法来做到这一点,甚至我作为新手应该知道的提示或技巧,他们绝对是受欢迎的。
谢谢。