Re: println / for unexpected behaviour
On Tue, Nov 26, 2013 at 8:58 AM, Alex Miller wrote: > Realizing a lazy sequence incurs overhead on every item. Chunked seqs > amortize that cost by realizing a chunk of items at a time giving you > better overall performance at the cost of less laziness. > And in this case that resulted in all the side effects occurring before any of the "nil"s were printed, instead of the [:empty :empty...] vectors being interleaved with the nils in the output. -- -- 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.
Re: println / for unexpected behaviour
Realizing a lazy sequence incurs overhead on every item. Chunked seqs amortize that cost by realizing a chunk of items at a time giving you better overall performance at the cost of less laziness. -- -- 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.
Re: println / for unexpected behaviour
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 '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 '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 '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.
Re: println / for unexpected behaviour
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 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.
Re: println / for unexpected behaviour
Thanks very much Cedric. (What do you mean by: 'Clearly "board" is a chunked seq in this case.'?) On Monday, November 25, 2013 1:58:36 PM UTC, Cedric Greevey wrote: > > Clearly "board" is a chunked seq in this case. > > Use doseq when you want side effects for-each of some seqable, but don't > care about the return values. The arguments for doseq are identical to > those for for, but a) doseq will return nil and b) if the output of for was > discarded (rather than the repl realizing the sequence to print the nils) > the side effects would never take place, whereas doseq forces them to take > place whether or not the nil *it* returns is used or discarded. > > > On Mon, Nov 25, 2013 at 8:25 AM, Ambrose Bonnaire-Sergeant < > abonnair...@gmail.com > wrote: > >> Hi Edward, >> >> I believe the return value of your expression is (nil nil nil nil ...), >> but the printlns are forced just after the ( is printed. >> >> Thanks, >> Ambrose >> >> >> On Mon, Nov 25, 2013 at 9:14 PM, >wrote: >> >>> Some (println) weirdness (board is a vector to vectors): >>> >>> (println (board 0)) >>> (println (board 1)) >>> (println (board 2)) >>> (println (board 3)) >>> (println (board 4)) >>> (println (board 5)) >>> (println (board 6)) >>> (println (board 7)) >>> >>> Works as I would expect, printing to the console. >>> >>> However: >>> >>> (for [row board] >>> (println row)) >>> >>> Doesn't: the output from println is part of the result of evaluating the >>> for (along with a slew of nils). >>> >>> ([:empty :empty :empty :empty :empty :empty :empty :empty] >>> [:empty :empty :empty :empty :empty :empty :empty :empty] >>> [:empty :empty :empty :empty :empty :empty :empty :empty] >>> [:empty :empty :empty :white :black :empty :empty :empty] >>> [:empty :empty :empty :black :white :empty :empty :empty] >>> [:empty :empty :empty :empty :empty :empty :empty :empty] >>> [:empty :empty :empty :empty :empty :empty :empty :empty] >>> [:empty :empty :empty :empty :empty :empty :empty :empty] >>> nil nil nil nil nil nil nil nil) >>> >>> Any idea why there is any difference at all between the two? The only >>> thing I can think of is for's lazy evaluation but I don't see how. >>> >>> -- >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To post to this group, send email to clo...@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+u...@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+u...@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 clo...@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+u...@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+u...@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.
Re: println / for unexpected behaviour
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 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.
Re: println / for unexpected behaviour
Clearly "board" is a chunked seq in this case. Use doseq when you want side effects for-each of some seqable, but don't care about the return values. The arguments for doseq are identical to those for for, but a) doseq will return nil and b) if the output of for was discarded (rather than the repl realizing the sequence to print the nils) the side effects would never take place, whereas doseq forces them to take place whether or not the nil *it* returns is used or discarded. On Mon, Nov 25, 2013 at 8:25 AM, Ambrose Bonnaire-Sergeant < abonnaireserge...@gmail.com> wrote: > Hi Edward, > > I believe the return value of your expression is (nil nil nil nil ...), > but the printlns are forced just after the ( is printed. > > Thanks, > Ambrose > > > On Mon, Nov 25, 2013 at 9:14 PM, wrote: > >> Some (println) weirdness (board is a vector to vectors): >> >> (println (board 0)) >> (println (board 1)) >> (println (board 2)) >> (println (board 3)) >> (println (board 4)) >> (println (board 5)) >> (println (board 6)) >> (println (board 7)) >> >> Works as I would expect, printing to the console. >> >> However: >> >> (for [row board] >> (println row)) >> >> Doesn't: the output from println is part of the result of evaluating the >> for (along with a slew of nils). >> >> ([:empty :empty :empty :empty :empty :empty :empty :empty] >> [:empty :empty :empty :empty :empty :empty :empty :empty] >> [:empty :empty :empty :empty :empty :empty :empty :empty] >> [:empty :empty :empty :white :black :empty :empty :empty] >> [:empty :empty :empty :black :white :empty :empty :empty] >> [:empty :empty :empty :empty :empty :empty :empty :empty] >> [:empty :empty :empty :empty :empty :empty :empty :empty] >> [:empty :empty :empty :empty :empty :empty :empty :empty] >> nil nil nil nil nil nil nil nil) >> >> Any idea why there is any difference at all between the two? The only >> thing I can think of is for's lazy evaluation but I don't see how. >> >> -- >> -- >> 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. >> > > -- > -- > 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. > -- -- 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.
Re: println / for unexpected behaviour
Hi Edward, I believe the return value of your expression is (nil nil nil nil ...), but the printlns are forced just after the ( is printed. Thanks, Ambrose On Mon, Nov 25, 2013 at 9:14 PM, wrote: > Some (println) weirdness (board is a vector to vectors): > > (println (board 0)) > (println (board 1)) > (println (board 2)) > (println (board 3)) > (println (board 4)) > (println (board 5)) > (println (board 6)) > (println (board 7)) > > Works as I would expect, printing to the console. > > However: > > (for [row board] > (println row)) > > Doesn't: the output from println is part of the result of evaluating the > for (along with a slew of nils). > > ([:empty :empty :empty :empty :empty :empty :empty :empty] > [:empty :empty :empty :empty :empty :empty :empty :empty] > [:empty :empty :empty :empty :empty :empty :empty :empty] > [:empty :empty :empty :white :black :empty :empty :empty] > [:empty :empty :empty :black :white :empty :empty :empty] > [:empty :empty :empty :empty :empty :empty :empty :empty] > [:empty :empty :empty :empty :empty :empty :empty :empty] > [:empty :empty :empty :empty :empty :empty :empty :empty] > nil nil nil nil nil nil nil nil) > > Any idea why there is any difference at all between the two? The only > thing I can think of is for's lazy evaluation but I don't see how. > > -- > -- > 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. > -- -- 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.
println / for unexpected behaviour
Some (println) weirdness (board is a vector to vectors): (println (board 0)) (println (board 1)) (println (board 2)) (println (board 3)) (println (board 4)) (println (board 5)) (println (board 6)) (println (board 7)) Works as I would expect, printing to the console. However: (for [row board] (println row)) Doesn't: the output from println is part of the result of evaluating the for (along with a slew of nils). ([:empty :empty :empty :empty :empty :empty :empty :empty] [:empty :empty :empty :empty :empty :empty :empty :empty] [:empty :empty :empty :empty :empty :empty :empty :empty] [:empty :empty :empty :white :black :empty :empty :empty] [:empty :empty :empty :black :white :empty :empty :empty] [:empty :empty :empty :empty :empty :empty :empty :empty] [:empty :empty :empty :empty :empty :empty :empty :empty] [:empty :empty :empty :empty :empty :empty :empty :empty] nil nil nil nil nil nil nil nil) Any idea why there is any difference at all between the two? The only thing I can think of is for's lazy evaluation but I don't see how. -- -- 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.