> Sorry, I forgot to ask: how rapid is "rapidly"?

Oh, I'd say I misused that word, at least it's way more than I need
for what I use this for. I created this post only to see if someone
would have an idea for a fully lazy version out of curiosity. From my
experiments, the non-recursive version blow the stack around 486
recursions on my computer.

> Can you provide a simple example that rapidly blows the stack
> so we can experiment  with lazy solutions?

Here's what I used to test the depth of recursion:

(def pit (iterate list "bottom!"))

(defn test-walk [walker shallowest deepest]
  (doseq [depth (range shallowest deepest)]
    (pprint (walker #(str depth " reached " %)
              (last (take depth pit))))))

Here's the recursive walks functions and a new one using your first
attempt at lazy-walk:

(defn recursive-string-walk [f form]
  (walk #(if (string? %) (f %) (recursive-string-walk f %))
    identity form))

(defn lazy-recursive-string-walk [f form]
  (walk #(cond
           (string? %) (f %)
           (seq? %)    (lazy-seq (lazy-recursive-string-walk f %))
           :default    (lazy-recursive-string-walk f %))
    identity form))

(defn really-lazy-recursive-string-walk [f form]
  (lazy-walk #(if (string? %) (f %) (really-lazy-recursive-string-walk
f %))
    identity form))

And here's the results:

 * (test-walk recursive-string-walk 450 500) -> ...(...("487 reached
bottom!")...); Evaluation aborted.
 * (test-walk lazy-recursive-string-walk 800 900) -> ...(...("828
reached bottom!")...); Evaluation aborted.
 * (test-walk really-lazy-recursive-string-walk 800 900) -> ...(...
("829 reached bottom!")...); Evaluation aborted.

So just one more, there's still lots of room for improvement ;-). For
whatever reason the lazy version isn't able to reach 1000 while
yesterday it was able to go slightly above that and the non-lazy
version didn't changed. The more I look at the code the more I don't
get why it's not fully lazy. I'll try with zippers as Laurent
suggested.

Thanks a lot!

- budu
-- 
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

Reply via email to