Search code examples
clojurereduceclojurescriptlazy-sequenceshiccup

Should a doall Clojure function be used with a reduce call?


I have been using Clojure, ClojureScript, lein, shadow-cljs, re-frame, reagent, Emacs, and CIDER to work on a Clojure/ClojureScript dynamic web app project. I am new to Clojure.

At some point in the codebase there is a big use of doall command followed by the use of reduce in order to generate hiccup (HTML renderer):


(doall
   (reduce
      (fn  ...) ...)
   []            ; hiccup-output
  project-variable)
    

I am new to Clojure. But this felt weird to me considering documentation:

When lazy sequences are produced via functions that have side effects, any effects other than those needed to produce the first element in the seq do not occur until the seq is consumed. doall can be used to force any effects. Walks through the successive nexts of the seq, retains the head and returns it, thus causing the entire seq to reside in memory at one time.

1 - Isn't doall supposed to be used with lazy sequences?

2 - I believe reduce is not one. Am I wrong?

3 - If doall should not be used with reduce in this case, what would be the recommendation for a refactoring here?


Solution

    1. Yes, doall only makes sense with lazy collections
    2. reduce does not by itself create a lazy collection. But the reducing function (the first argument to reduce) might end up returning a lazy collection - in that case, doall might make sense
    3. Impossible to say because the code that you've provided is incomplete and the parentheses are unbalanced