3

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?

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

println should flush automagically. you generally only have to worry about manually flushing if you are using print.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.