Fork me on GitHub
#specter
<
2020-06-05
>
dadair23:06:58

Can I use spectre to recursively transform java.util.HashMap of java.util.HashMaps to regular clojure maps?

dadair23:06:37

I'm basically trying to go from the top here, where all the maps are java.util.HashMap, to the bottom, where all maps are IAssociative:

{"name" "foo"
 "children" {"bar" {"name" "bar"
                    "children" {}}
             "baz" {"name" "baz"
                    "children" {"quux" {"name" "quux"
                                        "children" {}}}}}}

;;=> (addition of `ref`, keywordize the keys, make standard IAssociative)

{:name "foo"
 :children {:bar {:name "bar"
                  :children {}
                  :ref "foo/bar"}
            :baz {:name "baz"
                  :children {:quux {:name "quux"
                                    :children {}
                                    :ref "foo/baz/quux"}}
                  :ref "foo/baz"}}
 :ref "foo"}

dadair23:06:05

Had an implementation using both clojure.walk and spectre, but want to do it in 1 pass, if possible

dadair23:06:42

I can select each with:

(def HASH-MAPS
    (sp/recursive-path
     []
     p
     (sp/stay-then-continue
      (sp/must "children")
      sp/MAP-VALS
      p)))

  (clojure.pprint/pprint
   (sp/select
    [:root HASH-MAPS]
    {:root hm}))

dadair23:06:55

It's the transform in one pass that's getting me confused >.<