But it is indeed a characteristic of the seq. The repl reads and evals the
for expression, which yields a lazy seq, so basically nothing has happened
at this point.

Then the repl needs to print the result. So first it prints an open
parenthesis as the result is a seq. Then it needs to evaluate the first
element of the seq to print it. In the course of the evaluation, it
executes the println., so you get your first row after the open
parenthesis. But the rows are not part of the return value, only the seq of
nils is the return value. The fact that the rows appear to be part of the
seq is an accident of the order of evaluation.

Now you know why you don't get

[:empty ...] ... (nil ...)

But perhaps, given the explanation I just gave, you would expect something
like:

([:empty ...]nil [:empty ...]nil ...)

This is where chunked seq come in, according to Cedric. A chunked seq is a
lazy seq, but instead of evaluating its elements one at a time when they
are required, it evaluates them in chunks. So when the dirst element is
requested of the lazy seq, it immediately realize the first, say, 32
elements. So you get all the prints before any nil.

Hope that helps.

On Tuesday, 26 November 2013, wrote:

> Thanks Stefan.
>
> I had a suspicion that it was to do with for's laziness but I had assumed
> it was a characteristic of the seq it built rather than the forms inside
> it. Seems a bit strange but will have to get used to it :-)
>
> On Monday, November 25, 2013 2:11:45 PM UTC, Stefan Kamphausen wrote:
>>
>> Hi Edward,
>>
>>
>> you are being hit by laziness here.  Clojure's 'for' is not like the
>> 'for' you may know from other programming languages.  It is made for list
>> comprehensions, that is it is building new list-y things.  It does not do
>> this instantly, the items may be realized only when the caller asks for
>> them.  In your case the caller is your REPL which prints the return value,
>> thereby realizing the lazy sequence.  Thus, the output for the REPL and
>> your println mix.
>>
>> As Cedric already wrote, if you want to process the board for side-effect
>> like printing, use doseq.  Use for only for its return value and make no
>> assumptions as to when those values are created.
>>
>>
>> Kind regards,
>> Stefan
>>
>  --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to 
> clojure@googlegroups.com<javascript:_e({}, 'cvml', 
> 'clojure@googlegroups.com');>
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com <javascript:_e({}, 'cvml',
> 'clojure%2bunsubscr...@googlegroups.com');>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com <javascript:_e({}, 'cvml',
> 'clojure%2bunsubscr...@googlegroups.com');>.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to