The example tree was not accepted as a valid data structure, so I used this
instead. Hopefully it represents what you had in mind:
(def tree {:parent1
{:relationship1
{:child1 1
:child2 2}
:relationship2
{:child3 3}
:_meta 4}})
I wanted to use clojure.walk to do this, but couldn't figure out how, so
this is what I ended up with instead. 'excluded' should be a set of keys
you want to exclude on the walk.
(defn walk-tree
[tree excluded]
(let [f (fn f [m path]
(apply concat (for [[k v] m]
(cond
(excluded k) nil
(map? v) (f v (conj path k))
:else (list (conj path k))))))]
(f tree [])))
user=> (walk-tree tree #{:_meta})
([:parent1 :relationship1 :child1] [:parent1 :relationship1 :child2]
[:parent1 :relationship2 :child3])
Note that if a path ends with an empty map, that path will not be printed.
Don't know if this is acceptable or not.
(def tree2 {:parent1
{:relationship1
{:child1 {}
:child2 2}
:relationship2
{:child3 3}
:_meta 4}})
user=> (walk-tree tree2 #{:_meta})
([:parent1 :relationship1 :child2] [:parent1 :relationship2 :child3])
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en