Re: recursion in clojure.walk
On Jan 9, 2014, at 6:33 PM, Stuart Sierra wrote: > I wrote clojure.walk, but I don't usually recommend it for anything but > casual use. > > clojure.walk very general, so it's not going to be the most efficient > approach. When you know more details about the data structure you're working > with, as in this case, you can make something that will be faster and more > space-efficient. > > -S As someone who has gotten lots of productive casual use of it, I thank you! I'm wondering, though -- shouldn't it be possible to build the same general functionality on something like zippers and iteration rather than recursion, so that the JVM recursion limit wouldn't be an issue? It might also be nice to flag the issue somehow in the docs, since most of Clojure seems to avoid this kind of recursion/limit. -Lee -- -- 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: recursion in clojure.walk
I wrote clojure.walk, but I don't usually recommend it for anything but casual use. clojure.walk very general, so it's not going to be the most efficient approach. When you know more details about the data structure you're working with, as in this case, you can make something that will be faster and more space-efficient. -S On Thursday, January 9, 2014 5:17:50 PM UTC-5, Lee wrote: > > > Perhaps this is well known to others, but on the chance that maybe it > isn't I thought I'd share. > > In clojure.walk both prewalk and postwalk use recursion in ways that will > blow the stack for sufficiently deep nested structures. We had been using > them happily until recently when things got too big, and then we had > problems. > > For our application I was able to replace my previous walking with a call > to prewalkseq as defined below (which is only for seqs and uses zippers to > avoid the recursion -- and note that I'm using Michał Marczyk's recently > patched version of seq-zip so that it doesn't do the wrong thing with > embedded instances of ().) > > (defn prewalkseq > "Like prewalk but only for seqs and uses zippers." > [f s] > (loop [z (seq-zip s)] > (if (zip/end? z) > (zip/root z) > (recur (zip/next (zip/replace z (f (zip/node z > > That's all I need at the moment, but it's not immediately obvious to me > how this would be done for postwalk, and it would be nice if all of > clojure.walk could be made safe for large structures. > > -Lee -- -- 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.
recursion in clojure.walk
Perhaps this is well known to others, but on the chance that maybe it isn't I thought I'd share. In clojure.walk both prewalk and postwalk use recursion in ways that will blow the stack for sufficiently deep nested structures. We had been using them happily until recently when things got too big, and then we had problems. For our application I was able to replace my previous walking with a call to prewalkseq as defined below (which is only for seqs and uses zippers to avoid the recursion -- and note that I'm using Michał Marczyk's recently patched version of seq-zip so that it doesn't do the wrong thing with embedded instances of ().) (defn prewalkseq "Like prewalk but only for seqs and uses zippers." [f s] (loop [z (seq-zip s)] (if (zip/end? z) (zip/root z) (recur (zip/next (zip/replace z (f (zip/node z That's all I need at the moment, but it's not immediately obvious to me how this would be done for postwalk, and it would be nice if all of clojure.walk could be made safe for large structures. -Lee -- -- 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.