Last active
August 16, 2017 16:10
-
-
Save st/db8b11f5bff4cbff81b3 to your computer and use it in GitHub Desktop.
useful for interop with "java" maps
This file contains hidden or 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
(defn clojurify | |
[exp] | |
(cond | |
(instance? java.util.Map exp) (into {} (for [[k v] exp] [(keyword k) v])) | |
(instance? java.util.List exp) (into [] exp) | |
:else exp)) | |
(defn walk-to-clojure-map | |
[java-map] | |
(clojure.walk/prewalk clojurify java-map)) | |
(deftest walk-to-clojure-map-should-recursively-keywordize-a-java-map | |
(is (= | |
{:A 42 :c {:B [{:Z 111} {:e 1515}]}} | |
(walk-to-clojure-map | |
(java.util.HashMap. {"A" 42 "c" {"B" (java.util.ArrayList. [(java.util.HashMap. {"Z" 111}) {"e" 1515}])}}))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Max,
started with your idea, but couldn't make it in one pass.
I mean, one traversal.