【发布时间】:2016-02-02 11:07:21
【问题描述】:
我目前正在学习 clojure,我非常喜欢它。但是,我来自 emacs-lisp 背景,在解构方面我仍然有点困惑。 目前我收到一个带有很多键的数据库查询。我通过解构将它们分解为不同的变量,最后,在通过 let 创建了几个新值之后,我将哈希映射重建为返回值。 代码如下所示:
(fn [{:keys [metre _id num filedate divtype section sandhi lines grammar devanagari filename notes trans sectionnumber fileauthor lang]}]
(cond
;;when we have chinese
(= lang "zh")
(let [newgrammar (apply str (map (fn [{:keys [word position line tag] }]
(str "<span class=\"dropt\">" word
"<span style=\"width:500px;\"><font color=\"green\">Word:</font>" word
"<br><font color=\"red\">Function:</font> " tag
"<br><font color=\"blue\">Meaning:</font> " (db/get-chindic-meaning word tag)
"</span></span>")) grammar))
newtrans (first (map #(last (last %)) trans))]
(hash-map :metre metre :id _id :num num :filedate filedate :divtype divtype :section section :sandhi sandhi :lines lines :grammar newgrammar :devanagari devanagari :filename filename :notes notes :trans newtrans :sectionnumber sectionnumber :fileauthor fileauthor :lang lang))
这只是一个摘录,后面有许多不同的条件。所以有很多代码对我来说完全没有必要。 我想这很丑陋,而且肯定不是它应该的样子。关于如何正确执行此操作的任何建议?任何帮助将不胜感激!
【问题讨论】:
标签: variables dictionary clojure