Created
April 1, 2022 15:48
-
-
Save stathissideris/1b8e1b4e64ea7f1e6a48be00fc34c97b 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
(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 {}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(flatten-routes x)
produces:
;; => {"/foo/bar" :bar, "/foo/baz/quux" :quux, "/yo/man" :man, "/yo/manman" :manman}