105

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?

David J.
  • 31,569
  • 22
  • 122
  • 174
Tim Clemons
  • 6,231
  • 4
  • 25
  • 23
  • I'm surprised nobody has asked why you are concerned about the actual type of the return value of `doall` – tar Nov 01 '17 at 17:14
  • 1
    You could convert to a vector: `(vec (take 3 (repeatedly rand)))` – Kris Jan 23 '18 at 12:33

5 Answers5

180

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.

Magnar
  • 28,550
  • 8
  • 60
  • 65
Rich Hickey
  • 1,832
  • 1
  • 12
  • 2
84

This is to some degree a question of taxonomy. a lazy sequence is just one type of sequence as is a list, vector or map. So the answer is of course "it depends on what type of non lazy sequence you want to get:
Take your pick from:

  • an ex-lazy (fully evaluated) lazy sequence (doall ... )
  • a list for sequential access (apply list (my-lazy-seq)) OR (into () ...)
  • a vector for later random access (vec (my-lazy-seq))
  • a map or a set if you have some special purpose.

You can have whatever type of sequence most suites your needs.

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • This is the best answer. – Felipe Jul 25 '14 at 16:20
  • 8
    The accepted answer is technically correct, but this answer was most useful to me. I was trying to map a function over a vector and then spit the results to a file, and even after calling doall, the file contained "clojure.lang.LazySeq@address" instead of the contents of the sequence. Calling vec on value map returned got me what I needed to spit out to the file. – Jesse Rosalia Nov 23 '15 at 23:17
  • 2
    @JesseRosalia It's good to know that the one and only Rich Hickey response in all of SO was was technically correct. ;-) – Phil Cooper Mar 15 '16 at 21:02
  • 1
    `(vec (my-lazy-seq))` is not so nice in situations like the following: `(vec (json/parse-string "{\"foo\":\"bar\"}")) ;; => [["foo" "bar"]]` Since `cheshire` chooses to produce a lazy-seq from `(json/parse-string)` – codeasone Feb 07 '18 at 16:06
  • Mitigation for the above was to use eager `(json/parse-string-strict)` – codeasone Feb 07 '18 at 16:38
24

This Rich guy seems to know his clojure and is absolutely right.
Buth I think this code-snippet, using your example, might be a useful complement to this question :

=> (realized? (take 3 (repeatedly rand))) 
false
=> (realized? (doall (take 3 (repeatedly rand)))) 
true

Indeed type has not changed but realization has

Peter
  • 47,963
  • 46
  • 132
  • 181
8

I stumbled on this this blog post about doall not being recursive. For that I found the first comment in the post did the trick. Something along the lines of:

(use 'clojure.walk)
(postwalk identity nested-lazy-thing)

I found this useful in a unit test where I wanted to force evaluation of some nested applications of map to force an error condition.

SiKing
  • 10,003
  • 10
  • 39
  • 90
leeor
  • 17,041
  • 6
  • 34
  • 60
5
(.getClass (into '() (take 3 (repeatedly rand))))
agf
  • 171,228
  • 44
  • 289
  • 238
stupito
  • 67
  • 1