Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does `count` realize a lazy sequence in Clojure?

Tags:

clojure

Let's say I have a LazySeq

(def s (take 10 (iterate + 0)))

Does (count s) realize the sequence?

like image 489
al3x Avatar asked Sep 10 '13 21:09

al3x


1 Answers

If you are asking about lazy sequences, Yes.

user> (def s (map #(do (println "doing work") %) (range 4)))
#'user/s
user> (count s)
doing work
doing work
doing work                       
doing work
4  

Some of the data structures can give you answers in constant time, though lazy sequences do not have a stored count, and counting always realizes them.

like image 187
Arthur Ulfeldt Avatar answered Nov 07 '22 03:11

Arthur Ulfeldt