Skip to content

Instantly share code, notes, and snippets.

@stathissideris
Created April 1, 2022 15:48
Show Gist options
  • Save stathissideris/1b8e1b4e64ea7f1e6a48be00fc34c97b to your computer and use it in GitHub Desktop.
Save stathissideris/1b8e1b4e64ea7f1e6a48be00fc34c97b to your computer and use it in GitHub Desktop.
(def x [["/foo"
["/bar" :bar]
["/baz"
["/quux" :quux]]]
["/yo"
[["/man" :man]
["/manman" :manman]]]])
(require '[clojure.zip :as z]
'[clojure.string :as str])
(defn flatten-routes [routes]
(loop [res {} z (z/vector-zip routes)]
(cond (z/end? z) res
(keyword? (z/node z)) (recur (assoc res (str/join (->> z z/path (filter (comp string? first)) (map first))) (z/node z))
(z/next z))
:otherwise (recur res (z/next z)))))
(defn flatten-routes [routes]
(->> (iteration identity
:initk (z/vector-zip routes)
:kf z/next
:somef (complement z/end?)
:vf #(when (-> % z/node keyword?)
[(str/join (->> % z/path (filter (comp string? first)) (map first))) (z/node %)]))
(into {})))
@stathissideris
Copy link
Author

(flatten-routes x)

produces:

;; => {"/foo/bar" :bar, "/foo/baz/quux" :quux, "/yo/man" :man, "/yo/manman" :manman}

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