Skip to content

Instantly share code, notes, and snippets.

@sritchie sritchie/unnest.clj
Last active Aug 29, 2015

Embed
What would you like to do?
;; First stab an unnesting.
(defn fixed-point [f seed eq]
(letfn [(eval-guess [guess]
(let [next-guess (f guess)]
(if (eq guess next-guess)
guess
(eval-guess next-guess))))]
(eval-guess seed)))
(s/defn unnest [m]
(letfn [(flat [[k v]]
(cond (map? v) (u/mapk (fn [inner-k] (format "%s[%s]" k inner-k)) v)
(vector? v) (map (fn [inner-v]
[(str k "[]") inner-v])
v)
:else [[k v]]))
(one-level [m]
(mapcat flat m))]
(fixed-point one-level m =)))
(comment
(unnest
{"a" "z" "hotel" {"a" "1" "b" "2"} "ace" [{"one" "1" "two" "2" "three" "3"}]})
'(["a" "z"] ["ace[][one]" "1"] ["ace[][two]" "2"] ["ace[][three]" "3"] ["hotel[a]" "1"] ["hotel[b]" "2"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.