Create a lazy sequence by concatenating collections.
Consider the following function:
(defn req []
(Thread/sleep 1000)
(repeat 4 (rand-int 10)))
The sleep is added since the function will finally be a http request, thus it should emulate a delay.
Sample outputs:
(req)
;; (8 8 8 8)
(req)
;; (4 4 4 4)
I'm thinking of a function now, that creates a lazy sequence build by the concatenation of subsequent req
results.
(take 10 (f req))
;; (3 3 3 3 2 2 2 2 9 9)
Here is one implementation:
(defn f [g]
(lazy-seq (concat (g) (f g))))
Is this the way to go? I'm somehow guessing that there might be already an abstraction for this available.. I tried lazy-cat
, but this macro seems to work only for a fixed number of given sequences.
It turns out that this is a working abstraction:
(take 10 (apply concat (repeatedly req)))
However it looks like chunking of lazy sequences causes req
to be called more often than needed here, which would not be acceptable if it's an http request.