Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's no way to avoid some kind of nested loop. But you don't need <code>domap</code> at all, Compojure is smart enough (sometimes) to expand a seq for you. <code>list</code> and <code>map</code> and <code>for</code> are enough. For example Michał Marczyk's answer, or:</p> <pre><code>(defn map-tag [tag xs] (map (fn [x] [tag x]) xs)) (defn create-table [] (list [:h1 "Schedule"] [:hr] [:table {:style "border: 0; width: 90%"} [:tr (map-tag :th ["Name" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun"])] [:tr (for [[category people] smpl2] (list* [:tr [:td category]] (for [person people [name hours] person] [:tr [:td name] (map-tag :td hours)])))]])) </code></pre> <p>The "map over a seq and wrap everything in the same tag" pattern is common enough that I like to use a helper function for it sometimes.</p> <p>Compojure expands one level of <code>seq</code> for you. So you can throw some stuff into a <code>list</code> to get tags to appear sequentially in your HTML output, which I did to get the h1 and hr to show up. In your code, you're just throwing the h1 and hr away.</p> <p>But note that it only expands one level. When you have a list of lists, or a list of seqs, the outer seq will expand but the inner ones won't.</p> <pre><code>user&gt; (println (html (list [:div "foo"] (for [x [1 2 3]] [:div x])))) &lt;div&gt;foo&lt;/div&gt;clojure.lang.LazySeq@ea73bbfd </code></pre> <p>See how the inner seq makes Compojure barf. Look into <code>compojure.html.gen/expand-seqs</code> to see how this works, or change/fix it if you care to.</p> <p>This can be an issue when you have nested <code>for</code> or a <code>for</code> in a <code>list</code>, since <code>for</code> returns a lazy seq. But you just have to avoid having a seq-in-a-seq. I use <code>list*</code> above. A combination of <code>list</code> and <code>html</code> would work too. There are lots of other ways.</p> <pre><code>user&gt; (println (html (list* [:div "foo"] (for [x [1 2 3]] [:div x])))) &lt;div&gt;foo&lt;/div&gt;&lt;div&gt;1&lt;/div&gt;&lt;div&gt;2&lt;/div&gt;&lt;div&gt;3&lt;/div&gt; user&gt; (println (html (list [:div "foo"] (html (for [x [1 2 3]] [:div x]))))) &lt;div&gt;foo&lt;/div&gt;&lt;div&gt;1&lt;/div&gt;&lt;div&gt;2&lt;/div&gt;&lt;div&gt;3&lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload