I am trying to build a quick toc from an html content. (to make it short)
The code is dead simple:
(defn toc [content]
(doseq [i (take 5 (iterate inc 1))]
(let [h (str "h" i)]
(println ($ content h)))))
where content is the html content, and $ is a macro required from clojure-soup
While
($ content "h1")
works, and returns a list of all the tags.
Simple:
($ content (str "h" 1))
just won’t make it whatever I do.
How do I force
(str "h" 1)
to be properly eval-ed before the macro is called ?
Bonus points for explaining why 🙂
This is not possible if, as you imply,
$is a macro: it’s simply not how macros work. The macro needs to expand into something, at compile time, and it can only do so once. You have run-time data, like the various values ofh, but there is no way to use that at compile-time. To me it sounds like$should have been a function.