Last active
January 8, 2016 13:00
-
-
Save domgetter/138b14ac558b4aa52d18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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