-
-
Save Tyruiop/dc372ebc5ea4cbf6da50ef5000a79ed7 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
(ns nested-parser | |
(:require [clojure.string :as str])) | |
(defmacro parse-symbol | |
[s v] | |
(let [n `(name ~s) | |
lvls `(map keyword (str/split ~n #"\."))] | |
`(assoc-in {} ~lvls ~v))) | |
(parse-symbol 'foo.bar.baz 1) | |
;; => {:foo {:bar {:baz 1}}} | |
(defn nested-merge | |
[m1 m2] | |
(if (and (map? m1) (map? m2)) | |
(merge-with nested-merge m1 m2) | |
m2)) | |
(defn parse-nested | |
[m] | |
(reduce | |
(fn [acc [s v]] | |
(nested-merge acc (parse-symbol s v))) | |
{} | |
m)) | |
(parse-nested | |
{'foo.bar.baz 1 | |
'foo.baz 2 | |
'foo.bar.bim 3}) | |
;; => {:foo {:bar {:baz 1, :bim 3}, :baz 2}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment