if my structure is
{ :a :A
:b :B
:c {
:d :D
}
:e {
:f {
:g :G
:h :H
}
}
}
I
Got a similar question, wasn't satisfied by current solutions:
"Naive" recursive approach
(require '[clojure.set :as set])
(defn all-paths
([m current]
;; base case: map empty or not a map
(if (or (not (map? m)) (empty? m))
#{current}
;; else: recursive call for every (key, value) in the map
(apply set/union #{current}
(map (fn [[k v]]
(all-paths v (conj current k)))
m))))
([m]
(-> m (all-paths []) (disj []))))
(all-paths {:a 1
:b 2
:c {:ca 3
:cb {:cba 4
:cbb 5}}
:d {:da 6
:db 7}})
=> #{[:a] [:b] [:c] [:d] [:c :ca] [:c :cb] [:d :da] [:d :db] [:c :cb :cba] [:c :cb :cbb]}