How does clojure.walk/walk work for deeply nested data structures?

Just learned that walk is an interesting function for structures like:

(walk first #(apply max %) [ [1 2] [3 4] [5 6] ])
(w/walk (fn [[k v]] [k (* 10 v)]) identity {:a 1 :b 2 :c 3})

https://clojuredocs.org/clojure.walk/walk

which made me very curious how it works in deeper nested data structures? I tried but found no examples on Google. Does anyone have an example, like walk applying upon trees?

I recently used walk (well, prewalk) for traversing large trees of hiccup data. It works pretty much how you would expect: recursively applying the functions you pass in to each leaf until it hits something with no children. Prewalk & postwalk are specific variations on this.

In the same namespace, there’s prewalk-demo and postwalk-demo for getting a kind of visualization of how the prewalk/postwalk functions get applied.

I may be misunderstanding your question, but a very basic answer to the question “how does walk work on nested data structures” is “it uses recursion, calling itself on each nested data structure inside of an enclosing data structure”. If you are familiar with recursion, e.g. to search a binary tree data structure, then that is the basic idea.

I suspect there are good tutorials on how recursion works, with multiple examples, but don’t have a link handy for something I consider a good tutorial on that topic. This Wikipedia page might contain examples you find illuminating: https://en.wikipedia.org/wiki/Recursion_(computer_science) (specifically the “recursive procedures” section).

If that wasn’t the intent of your question, it might be a good idea to try to ask a more specific one.

Yeah, I know it’s using recursion. And I know spector too that Clojure has the power of handling data that is really complicated. The thing is walk does not come with enough examples. I can learn by reading(or running) examples of walk. The examples listed in the docs is too simple, I was trying to find more.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.