Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert lazy sequence to non-lazy in Clojure

I tried the following in Clojure, expecting to have the class of a non-lazy sequence returned:

(.getClass (doall (take 3 (repeatedly rand)))) 

However, this still returns clojure.lang.LazySeq. My guess is that doall does evaluate the entire sequence, but returns the original sequence as it's still useful for memoization.

So what is the idiomatic means of creating a non-lazy sequence from a lazy one?

like image 904
Tim Clemons Avatar asked Oct 29 '09 04:10

Tim Clemons


People also ask

What is a lazy sequence Clojure?

Lazy sequences in Clojure have two parts, the head is the first unrealized element of the sequence, and the thunk is an uninvoked, argument-less function that, when called, will realize the rest of the elements in the sequence starting from the head.

Does Clojure have lazy evaluation?

With lazy evaluation techniques, it is possible to construct infinite data structures that are evaluated as consumed. These infinite sequences utilizes lambdas, closures and recursion. In clojure, these infinite data structures are generated using lazy-seq and cons forms.


1 Answers

doall is all you need. Just because the seq has type LazySeq doesn't mean it has pending evaluation. Lazy seqs cache their results, so all you need to do is walk the lazy seq once (as doall does) in order to force it all, and thus render it non-lazy. seq does not force the entire collection to be evaluated.

like image 139
Rich Hickey Avatar answered Sep 20 '22 16:09

Rich Hickey