How do I avoid Clojure’s chunking behavior for lazy seqs that I want to short circuit?

CORRECTED TWICE: A simpler way to un-chunk a lazy sequence:

(defn unchunk [s]
  (when (seq s)
    (lazy-seq
      (cons (first s)
            (unchunk (next s))))))

First version omitted (when ... so it returned an infinite seq of nil’s after the input sequence ended.

Second version used first instead of seq so it stopped on nil.

RE: your other question, “how do I avoid chunking when doing the apply, which seems to be consuming in chunked groups of four”:

This is due to the definition of =, which, when given a sequence of arguments, forces the first 4:

(defn =
  ;; ... other arities ...
  ([x y & more]
   (if (= x y)
     (if (next more)
       (recur y (first more) (next more))
       (= y (first more)))
     false)))

Leave a Comment