Skip to content

Instantly share code, notes, and snippets.

@hadronzoo
Last active January 4, 2016 07:49
Show Gist options
  • Save hadronzoo/8591122 to your computer and use it in GitHub Desktop.
Save hadronzoo/8591122 to your computer and use it in GitHub Desktop.
(def data
[["Community" "community.png" "Community"]
["News" "news.png" "Community"]
["Messages" "messages.png" "Community"]
["Inbox" "inbox.png" "Messages"]
["Sent" "sent.png" "Messages"]])
(defn map-menu [menu]
(reduce (fn [m [child image parent]]
(let [children (-> (:children (get m parent) #{})
(disj parent)
(conj child))]
(-> (assoc-in m [parent :children] children)
(assoc-in [child :image] image))))
{}
menu))
(defn map->tree [m root]
(let [{:keys [image children]} (get m root)]
{root {:image image
:children (map (partial map->tree m) children)}}))
(map->tree (map-menu data) "Community")
;; {"Community" {:image "community.png",
;; :children ({"Messages" {:image "messages.png",
;; :children ({"Inbox" {:image "inbox.png",
;; :children ()}}
;; {"Sent" {:image "sent.png",
;; :children ()}})}}
;; {"News" {:image "news.png",
;; :children ()}})}}
@clifton
Copy link

clifton commented Apr 25, 2014

Awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment