Re: lazy-seq and somewhat greedy apply
In case it's useful, here is a fully lazy variant of 'apply concat': https://github.com/plumatic/plumbing/blob/master/src/plumbing/core.cljx#L179 -Jason On Friday, June 3, 2016 at 2:26:43 AM UTC+8, Andy L wrote: > > Hi, > > I have a riddle I am not sure how to solve: > ``` > user=> (defn just-a-demo-seq[] (println "in just-a-demo-seq") [1 2 3 4 5 6 > 7 8 9]) > #'user/just-a-demo-seq > user=> (def do-not-print-it (apply concat (repeatedly just-a-demo-seq))) > in just-a-demo-seq > in just-a-demo-seq > in just-a-demo-seq > in just-a-demo-seq > #'user/do-not-print-it > ``` > > Question is why `just-a-demo-seq` is invoked 4 times. > > We spent some time trying to answer the question to the point of analyzing > RT.java and core.clj and pretty much ruled out `concat`. It seems that > `apply` os somewhat greedy exercising too many elements of the lazy > sequence. > > Any insight? > > Thanks, > Andy > -- 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/d/optout.
Re: lazy-seq and somewhat greedy apply
> 1- concat is defined to have 4 different arities: 0-arg, 1-arg, 2-arg, > 3+args. In order to figure out which arity to use, apply has to realize at > least as many elements as the minimum number of args required by the > largest arity, which in this case is 3 > 2- apply has a small bug that causes it to realize one element more than > necessary, in this case making it realize 4 elements rather than 3, here's > a ticket with a patch fixing that issue: > http://dev.clojure.org/jira/browse/CLJ-1583 > > Thx for the explanation. In my original code, I have a "lazy" sequence of a size determined by specified in the content and the entire thing read of I/O without a way of telling that I/O is closed. "read" function hangs and blocks the code if performed too many times. I redesigned my code to avoid that combination. Best, Andy -- 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/d/optout.
Re: lazy-seq and somewhat greedy apply
Two things at play here: 1- concat is defined to have 4 different arities: 0-arg, 1-arg, 2-arg, 3+args. In order to figure out which arity to use, apply has to realize at least as many elements as the minimum number of args required by the largest arity, which in this case is 3 2- apply has a small bug that causes it to realize one element more than necessary, in this case making it realize 4 elements rather than 3, here's a ticket with a patch fixing that issue: http://dev.clojure.org/jira/browse/CLJ-1583 Nicola > On 2 Jun 2016, at 19:26, Andy Lwrote: > > Hi, > > I have a riddle I am not sure how to solve: > ``` > user=> (defn just-a-demo-seq[] (println "in just-a-demo-seq") [1 2 3 4 5 6 7 > 8 9]) > #'user/just-a-demo-seq > user=> (def do-not-print-it (apply concat (repeatedly just-a-demo-seq))) > in just-a-demo-seq > in just-a-demo-seq > in just-a-demo-seq > in just-a-demo-seq > #'user/do-not-print-it > ``` > > Question is why `just-a-demo-seq` is invoked 4 times. > > We spent some time trying to answer the question to the point of analyzing > RT.java and core.clj and pretty much ruled out `concat`. It seems that > `apply` os somewhat greedy exercising too many elements of the lazy sequence. > > Any insight? > > Thanks, > Andy > > -- > 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/d/optout. -- 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/d/optout. signature.asc Description: Message signed with OpenPGP using GPGMail