Here's a beginner's question: is there a way in Clojure to lazily concatenate an arbitrary number of sequences? I know the lazy-cat macro, but I cannot come up with its correct application for an arbitrary number of sequences.
My use case is lazy loading data from an API through programmed (biased / restricted) requests. Each query executed using request-fn below will retrieve 100 results:
(map request-fn (iterate (partial + 100) 0))
If there are no more results, request-fn returns an empty sequence. This is when I stop the iteration:
(take-while seq (map request-fn (iterate (partial + 100) 0)))
For example, an API can return up to 500 results and can be ridiculed as:
(defn request-fn [offset] (when (< offset 500) (list offset)))
If I want to concatenate the results, I can use (apply concat results) , but it readily evaluates the sequence of results:
(apply concat (take-while seq (map request-fn (iterate (partial + 100) 0))))
Is there a way to lazily concatenate a sequence of results using either lazy-cat or something else?
clojure lazy-evaluation seq
Jindřich mynarz
source share