On Mon, Jan 13, 2014 at 8:49 AM, Kashyap CK <ckkash...@gmail.com> wrote:
> > I'd really appreciate it if someone could tell me what's going on when I > do the following - > > user> (defn xx [] (map println [1 2 3 4 5 6])) > #'user/xx > user> (take 2 (xx)) > (1 > 2 > 3 > 4 > 5 > 6 > nil nil) > user> > There are many things that you need to be aware of here: 1. Lazy seqs are chunked. As an optimization they are realized 32 elementes at a time. There are ways to go around this: http://blog.fogus.me/2010/01/22/de-chunkifying-sequences-in-clojure/ 2. map returns a lazy seq of the return value of the function it uses, and println returns nil as a result. 3. Lazy seqs and IO usually are a confusing mix. So, as a result of chunking a println for all six elements is made. Each of this calls, as a side effect prints the number and, returns nil. Then you ask for the first two return values of that sequence, hence the "nil nil". Everything is getting mixed up at the repl so it seems confusing, but the result of xx is (nil nil) and the side-effect of it is the printing of 1 2 3 4 5 6. Cheers, Mauricio -- -- 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.