5

I am experimenting with clojure's lazy sequences. In order to see when the evaluation of an item would occur, I created a function called square that prints the result before returning it. I then apply this function to a vector using map.

(defn square [x]
  (let [result (*  x x)]
  (println "printing " result)
  result))

(def s (map square [1 2 3 4 5])) ; outputs nothing

Here in my declaration of s, the REPL does not output anything. This signals that the computation has not started yet. This appears to be correct. I then do:

(first s)

The function "first" takes only the first item. So I expect that only 1 will be evaluated. My expectation is the REPL will output the following:

printing 1
1

However, the REPL outputted the following instead.

printing 1
printing 4
printing 9
printing 16
printing 25
1

So rather than evaluating only the first item, it seems it evaluates all items, even though I am accessing just the first item.

If the state of a lazy sequence can only be either all values computed and no values computed, then how can it gain the advantages of lazy evaluation? I came from a scheme background and I was expecting more like the behavior of streams. Looks like I am mistaken. Can anyone explain what is going on?

0

1 Answer 1

9

Laziness isn't all-or-nothing, but some implementations of seq operate on 'chunks' of the input sequence (see here for an explanation). This is the case for vector which you can test for with chunked-seq?:

(chunked-seq? (seq [1 2 3 4 5]))

When given a collection map checks to see if the underlying sequence is chunked, and if so evaluates the result on a per-chunk basis instead of an element at a time.

The chunk size is usually 32 so you can see this behaviour by comparing the result of

(first (map square (vec (range 35))))

This should only display a message for the first 32 items and not the entire sequence.

2
  • Is performance the only reason for this deviation from the just-evaluate-enough principle of lazy eval?
    – user5536315
    Oct 20, 2021 at 17:43
  • @Lee I wonder why the sseq1 function in the blog you linked is still not yet in the official library. It would have been nice to control the chunk size of a sequence (E.G. if each call makes an http request, it would be nice to make it execute 1 at a time, 32 at a time will consume a lot of resources). Oct 21, 2021 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.