This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-03-31
Channels
- # announcements (20)
- # asami (14)
- # aws (6)
- # babashka (15)
- # beginners (83)
- # biff (6)
- # calva (93)
- # cider (3)
- # clj-kondo (21)
- # cljdoc (106)
- # cljs-dev (32)
- # clojure (165)
- # clojure-dev (78)
- # clojure-europe (54)
- # clojure-italy (9)
- # clojure-nl (9)
- # clojure-norway (24)
- # clojure-uk (4)
- # clojurescript (6)
- # community-development (2)
- # conjure (2)
- # core-typed (14)
- # datahike (4)
- # datomic (2)
- # emacs (40)
- # events (1)
- # fulcro (11)
- # graalvm-mobile (29)
- # graphql (8)
- # honeysql (19)
- # java (1)
- # jobs (1)
- # lsp (232)
- # malli (5)
- # membrane (112)
- # nextjournal (11)
- # off-topic (63)
- # portal (12)
- # re-frame (6)
- # reagent (3)
- # reitit (4)
- # rewrite-clj (2)
- # shadow-cljs (25)
- # tools-deps (6)
https://app.slack.com/team/U02CX2V8PJN [2:47 PM]
Quick question?
I am trying to convert a map with nested maps into a vector with nested vectors while retaining the key as vector entry and value as vector entry.
Taking this map: {:keya 1 :keyb {:abc 12 :xyz 34} :keyc 3}
I want it to look like this:`[:keya 1] [:keyb [:abc 12] [:xyz 34]] [:keyc 3]`
This works but fails when there are nested maps in the map:
(let [mykeys (into [] (for [keyword (keys {:keya 1 :keyb 2 :keyc 3})]
(symbol keyword)))]
(for [key mykeys]
[(symbol (format ":x:%s" (str key))) key]))
;;=> ([:x:keya keya] [:x:keyb keyb] [:x:keyc keyc])
with a nested map:
(let [mykeys {:keya 1 :keyb {:abc 12 :xyz 34} :keyc 3}]
(for [key (keys mykeys)]
[(keyword (format "x%s" (str key))) (key mykeys)]))
;; => ([:x:keya 1] [:x:keyb {:abc 12, :xyz 34}] [:x:keyc 3])
I want to avoid adding an unknown amount of for loops to handle the nested maps.
Reading up on how to do this brought me to postwalk, but I am not familiar with it.
How would I go about getting an output like mentioned above?
All I got so far is this:
(clojure.walk/postwalk
#(if (map? %)
(into [] (vals %)) %)
{:keya 1 :keyb {:abc 12 :xyz 34} :keyc 3})
;;=> [1 [12 34] 3]
(println
(clojure.walk/postwalk
#(if (map? %)
(mapv vec %)
%)
{:keya 1 :keyb {:abc 12 :xyz 34} :keyc 3}))