I want to take a sequence or vector and create an infinite, looping, lazy version of it. This is what I tried:
(def test-seq '(1 2 3))
(take 5 (repeat test-seq))
And I got
((1 2 3) (1 2 3) (1 2 3) (1 2 3) (1 2 3))
When what I wanted was
(1 2 3 1 2)
I know this works
(take 5 (flatten (repeat test-seq)))
but that seems a bit unsatisfying and flabby. I'm assuming re-structuring a sequence of sequences is expensive but I may well be wrong :)
You're looking for cycle
:
(take 5 (cycle '(1 2 3))) ;; => (1 2 3 1 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With