title: Lazy concatenation of sequence in Clojure
body: <p>Here's a beginner's question: Is there a way in Clojure to lazily concatenate an arbitrary number of sequences? I know there's <a href="http://clojuredocs.org/clojure.core/lazy-cat" rel="noreferrer"><code>lazy-cat</code></a> macro, but I can't think of its correct application for an arbitrary number of sequences.</p>
  
  <p>My use case is lazy loading data from an API via paginated (offseted/limited) requests. Each request executed via <code>request-fn</code> below retrieves 100 results:</p>
  
  <pre><code>(map request-fn (iterate (partial + 100) 0))
  </code></pre>
  
  <p>When there are no more results, <code>request-fn</code> returns an empty sequence. This is when I stop the iteration:</p>
  
  <pre><code>(take-while seq (map request-fn (iterate (partial + 100) 0)))
  </code></pre>
  
  <p>For example, the API might return up to 500 results and can be mocked as:</p>
  
  <pre><code>(defn request-fn [offset] (when (&lt; offset 500) (list offset)))
  </code></pre>
  
  <p>If I want to concatenate the results, I can use <code>(apply concat results)</code> but that eagerly evaluates the results sequence:</p>
  
  <pre><code>(apply concat (take-while seq (map request-fn (iterate (partial + 100) 0))))
  </code></pre>
  
  <p>Is there a way how to concatenate the results sequence lazily, using either <code>lazy-cat</code> or something else?</p>