Last active
January 3, 2016 11:29
-
-
Save janherich/8456078 to your computer and use it in GitHub Desktop.
Nested from flat
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 data {:b--name "B", :a--id 1, :b--id 2, :a--name "A"}) | |
(defn transform [data delimiter] | |
(reduce (fn [acc [k v]] | |
(let [[f-k s-k] (-> k (name) (str/split delimiter))] | |
(assoc-in acc [(keyword f-k) (keyword s-k)] v))) {} data)) | |
(transform data #"--") ;; {:a {:name "A", :id 1}, :b {:id 2, :name "B"}} | |
(defn transform [data delimiter] | |
(reduce (fn [acc [k v]] | |
(let [key-strs (-> k (name) (str/split delimiter))] | |
(assoc-in acc (mapv keyword key-strs) v))) {} data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment