Skip to content

Instantly share code, notes, and snippets.

@hkjels
Created September 30, 2017 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkjels/5637e59933d4c8cebf73d4052d75b2f8 to your computer and use it in GitHub Desktop.
Save hkjels/5637e59933d4c8cebf73d4052d75b2f8 to your computer and use it in GitHub Desktop.
(defn find-in
"Find all of the keys & indices of the [tree] that leads to the
value of [s]earch"
[tree s]
(some
(fn [[k v]]
(cond
(= v s) [k]
(map? v) (if-let [r (find-in v s)]
(into [k] r))
(vector? v) (letfn [(append [n m] (when-let [r (find-in m s)]
(conj [k] n r)))]
(when-let [b (-> (keep-indexed append v)
(flatten))]
(when (last b) [b])))))
tree))
(find-in {:a :_ :b :_ :c {:d "foo" :e "bar"} :f [{:g "pale" :h "brown" :i {:j "ale" :k [{:l "brown"}]}}]}
;"bar"
"brown"
;"ale"
)
;; current result => ([:f 0 :h])
;; wanted result => ([:f 0 :h] [:f 0 :i :k 0 :l])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment