Why does applying seq on LazySeq return ChunkedCons?

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

From my understanding, LazySeq is already a sequence, because:

 (seq? (range 10)) ;=> true 
+4
source share
2 answers

To expand your answer (and because comments do not support newlines):

 user=> (def r (range 10)) #'user/r user=> (realized? r) false user=> (class r) clojure.lang.LazySeq user=> (def r2 (rest r)) #'user/r2 user=> (realized? r2) ClassCastException clojure.lang.ChunkedCons cannot be cast to clojure.lang.IPending clojure.core/realized? (core.clj:6607) user=> (class r2) clojure.lang.ChunkedCons user=> (realized? r) true 
+1
source

I think I have an answer.

This is because using seq provides an estimate of the first LazySeq element. Since seq returns nil when the collection and sequence are empty, it must parse the element to solve this.

That is why rest more lazy than next , because (next s) is just (seq (rest s)) .

0
source

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


All Articles