Skip to content

Instantly share code, notes, and snippets.

@domgetter
Last active January 8, 2016 13:00
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 domgetter/138b14ac558b4aa52d18 to your computer and use it in GitHub Desktop.
Save domgetter/138b14ac558b4aa52d18 to your computer and use it in GitHub Desktop.
(defn find-with-key
([ds key] (find-with-key (:root ds) key [:root]))
([ds key path]
(if (= key (:key (:data ds)))
path
(if (empty? (:children ds)) nil
(first (filter identity (for [[index child] (map-indexed list (:children ds))]
(find-with-key ((:children ds) index) key (conj path :children index)))))))))
(def my-map {:key nil
:root {:children [{:children []
:data {:key "234"}}
{:children [{:children []
:data {:key "456"}}]
:data {:key "345"
:x 12}}]
:data {:key "123"}}})
(find-with-key my-map "123")
; [:root]
(find-with-key my-map "234")
; [:root :children 0]
(find-with-key my-map "345")
; [:root :children 1]
(find-with-key my-map "456")
; [:root :children 1 :children 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment