My friend showed me a blog post that makes use of ClojureScript macros and he claims that the code provided cannot be elegantly converted to JavaScript because of JavaScript's lack of macro support in the language. Specifically, the go
macro in this code snippet is not possible in JavaScript
(def c (chan))
(defn render [q]
(apply str
(for [p (reverse q)]
(str "<div class='proc-" p "'>Process " p "</div>"))))
(go (while true (<! (timeout 250)) (>! c 1)))
(go (while true (<! (timeout 1000)) (>! c 2)))
(go (while true (<! (timeout 1500)) (>! c 3)))
(defn peekn
"Returns vector of (up to) n items from the end of vector v"
[v n]
(if (> (count v) n)
(subvec v (- (count v) n))
v))
(let [el (by-id "ex0")
out (by-id "ex0-out")]
(go (loop [q []]
(set-html! out (render q))
(recur (-> (conj q (<! c)) (peekn 10))))))
Source: http://swannodette.github.io/2013/07/12/communicating-sequential-processes/
I'm a bit hesitant to believe that it cannot be done without some sort of macro library in an elegant way. I am not looking for a solution that does the same behavior in a different way (such as three infinite loops of setTimeout, etc.), but one that has the same "spirit" as the original.