独特的价值观让我们假设我们有值这个网格,可以是0或1:Clojure的列表理解和试剂
(def grid [[1 0 1]
[1 0 0]
[1 0 1]])
现在我想改变grid
到HTML中使用列表理解打嗝一样的格式:
(defn cell-component [is-it-1 key]
^{:key key} [:td (if (= is-it-1 1) {:class "is-it-1"})])
(defn grid-html []
([:table
[:tbody
(for [row grid]
^{:key row} [:tr
(for [cell row]
(cell-component cell how-i-can-generate-a-index?))])]]))
表正确生成,但我没有任何想法,如何使unique index
我td
。 how-i-can-generate-a-index?
应该是什么?
附注:grid-html中'[:table ...]'的圆括号是多余的(当它们转换成函数调用时,代码将不会与它们一起运行 - 需要返回组件:'(defn grid-html [] [:table [:tbody ...]])') –
只需使用map-indexed而不是for。 – ClojureMostly