Skip to content

Instantly share code, notes, and snippets.

@vermiculus
Last active May 1, 2016 22:42
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 vermiculus/c8a531a86a0a65cdb9af4a466ed0721f to your computer and use it in GitHub Desktop.
Save vermiculus/c8a531a86a0a65cdb9af4a466ed0721f to your computer and use it in GitHub Desktop.
(defn get-topic-children [topics topic-id]
(map first (filter #(= topic-id (:parent (second %))) topics)))
(defn get-routes [topics topic-id & [slug-so-far]]
(let [children (get-topic-children topics topic-id)
newslug (if (= topic-id :root) ""
(str slug-so-far "/" (:slug (get topics topic-id))))]
(if children
(into (if (= topic-id :root) {} {topic-id newslug})
(map #(get-routes topics % newslug) children))
{topic-id newslug})))
(let [topics {1 {:parent :root, :slug "lovely"}
2 {:parent 1, :slug "lovelier"}
3 {:parent 1, :slug "loveliest"}
4 {:parent :root, :slug "cool"}}
(get-routes topics :root))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment