How to make endless repeating sequence in clojure?

I want to take a sequence or vector and create an infinite, looping, lazy version. 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 I wanted to

 (1 2 3 1 2) 

I know it works

 (take 5 (flatten (repeat test-seq))) 

but it seems a little unsatisfactory and flabby. I suppose sequence restructuring is expensive, but I could be wrong :)

+4
source share
1 answer

Are you looking for cycle :

 (take 5 (cycle '(1 2 3))) ;; => (1 2 3 1 2) 
+9
source

Source: https://habr.com/ru/post/1494774/


All Articles