獨特的價值觀讓我們假設我們有值這個網格,可以是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