Using doseq/for with infinite sequences

Viewed 253

in Clojure, one can iterate over a sequence by the help of the function for or similarly with doseq for side-effects and getting nil as return value:

(doseq [x (range 3)]
    (prn x))

; 0
; 1
; 2

for the case the sequence is infinite, there is a way to introduce a break condition:

(doseq [x (range) :while (< x 3)]
    (prn x))

This would produce the same output as above.

As a specialty there is a very interesting behavior when you use more than one sequence. As the documentation calls it: "Collections are iterated in a nested fashion, rightmost fastest".

(doseq [x (range 3) y (range 3)]
    (prn x y))

; 0 0
; 0 1
; 0 2
; 1 0
; 1 1
; 1 2
; 2 0
; 2 1
; 2 2

What happens however, if the sequences are infinite again. When the last one is infinite, it works quite well. this would work equally as the example before:

(doseq [x (range 3) y (range) :while (< y 3)]
    (prn x y))

If the first one is infinite, the resulting output is as expected, but for some reasons the loop does not stop after the last line is printed. In other words: the repl keeps working.

(doseq [x (range) y (range 3) :while (< x 3)]
    (prn x y))

Can anybody explain this behavior?

2 Answers
Related