I would like to create a lazy sequence that repeats elements from another collection. It should generate one of each element before repeating. And the order of elements must be random.
Here's what it should behave like:
=> (take 10 x)
(B A C B A C A B C C)
This seems to work:
(def x (lazy-seq (concat (lazy-seq (shuffle ['A 'B 'C])) x)))
However it is using two lazy-seq
's. Is there a way to write this sort of lazy sequence by using just one lazy-seq
?
If this cannot be done with one lazy-seq
, how do values get generated? Since my collection has only three items, I expect the inner lazy-seq to be calculated completely in the first chunk.
Before coming up with the sequence above I tried the one below:
=> (def x (lazy-seq (concat (shuffle ['A 'B 'C]) x)))
=> (take 10 x)
(C A B C A B C A B C)
I would appreciate any explanation why this one doesn't randomize each batch.