我有一组URL,有些URL有间接引用(作为向量)。任何没有间接引用的URL都只有nil
。我开始使用以下测试图:
{"URL 1" nil,
"URL 2" ["indirect 1" "indirect 2"]}
我正在使用hiccup来构建HTML报告,所以我想要这个输出:
[:div "Imports: "
[:ul
[:li "URL 1"]
[:li "URL 2"]
[:ul
[:li "indirect 1"]
[:li "indirect 2"]
[:li "indirect 3"]]]]
当URL没有间接引用时,我遇到了一些返回nil的问题。我目前的代码如下:
(defn list-imports
[imports]
(if-not (nil? imports)
[:div "Imports: "
[:ul
(for [direct (keys imports)]
[[:li direct]
(if-let [indirects (get imports direct)]
[:ul
(for [indirect indirects]
[:li indirect])]
[:span])])]]
[:div "Imports: none" [:br] [:br]]))
问题是,它正在归还......
[:div
"Imports: "
[:ul
([[:li "URL 1"] [:span]]
[[:li "URL 2"] [:ul ([:li "indirect 1"] [:li "indirect 2"])]])]]
我必须添加一个[:span]
标签作为间接导入nil
的情况,我不是真的想要...但是,它放入{{1那里。
另一个问题是它最终包含在nil
和一个额外的向量中,因为我在for语句中做了很多事情。当我尝试用打嗝转换它时,我得到()
答案 0 :(得分:1)
这可能是构建打嗝标签的棘手问题。有时它有助于将问题分解成更小的部分。
(defn list-indirects
[indirects]
(when (seq indirects)
[(into [:ul] (mapv (fn [i] [:li i]) indirects))]))
(defn list-imports
[imports]
(if (some? imports)
[:div "Imports: "
(into [:ul]
(for [[url indirects] imports]
(into [:li url] (list-indirects indirects))))]
[:div "Imports: none" [:br] [:br]]))
这些功能可以为您提供所需的输出。
(list-imports {"URL 1" nil
"URL 2" ["indirect 1" "indirect 2"]})
=>
[:div "Imports: "
[:ul
[:li "URL 1"]
[:li "URL 2"
[:ul
[:li "indirect 1"]
[:li "indirect 2"]]]]]
此输出与您的预期输出略有不同,但我认为它与您实际需要的内容更接近,即示例中的[:li "URL 2"]
标记应该包含 :ul
" indirects"是有效的HTML。
另外要注意的是,如果这些项目的顺序很重要,那么地图可能不会按照您期望的方式排序,特别是一旦您拥有超过一定数量的密钥。当您遍历地图以构建打嗝时,"URL 2"
可能会在 "URL 1"
之前来到。您可以使用元组向量或可能是有序映射来解决此问题。