Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why applying seq on a LazySeq returns ChunkedCons?

Tags:

clojure

(class (range 10))
;=> clojure.lang.LazySeq

(class (seq (range 10))
;=> clojure.lang.ChunkedCons

From my understanding, LazySeq is already an sequence, since:

(seq? (range 10))
;=> true
like image 310
M. Tong Avatar asked Nov 03 '22 16:11

M. Tong


1 Answers

I guess I have an answer.

That's because using seq enforces the evaluation of the first element of LazySeq. Because seq returns nil when the collection & sequence is empty, it has to eval the element to decide that.

That's the exactly reason why rest is lazier than next, because (next s) is just (seq (rest s)).

like image 129
M. Tong Avatar answered Nov 15 '22 07:11

M. Tong