Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure, lazy evaluation issue

Tags:

clojure

Consider the following code in clojure:

(let [a (find-a), b (find-b)] (println a) (println b) )

Where b is a sequence. There are also some println statements in function find-a. What I would expect to see in standard outputs is: a, results from println statements in find-a, b. However, what I do get is : a, part of b, results from println statements in find-a, rest of b.

Is this due to lazy evaluation of sequences?

like image 579
kostas Avatar asked Aug 12 '26 18:08

kostas


1 Answers

Nothing in this code is inherently lazy - it should all get executed in the correct sequence.

However, depending on what a and b are there could be something lazy embedded inside them, which only gets executed when (println a) and (println b) are executed. In particular, if a and b are lazy sequences created with map or something similar then the later parts of the sequences will only get evaluated when execution is forced within the println statement. To be more specific than that, you'd need to describe the internal structure of a and b.

I actually suspect however that the problem could be due to buffers not getting flushed - see Clojure - Side Effects Happening Out Of Order

like image 105
mikera Avatar answered Aug 16 '26 16:08

mikera