Fork me on GitHub
#clojure
<
2022-12-22
>
vlad_poh00:12:40

How do you namespace a pre-existing map turn this

{:id 1 :name "CBC"}
to this
#:company{:id 1 :name "CBC"}

dpsutton00:12:29

That representation just means all of the keyword keys have the same namespace of company. Just update the keys of your map as needed

👍 1
pppaul02:12:43

(keyword "namespace/name") you need to do that for each key in your map. (update-keys my-map (fn [k] (keyword "company" (name k)))

👍 1
lilactown02:12:42

FWIW I would be wary of programmatically namespacing keys in a map. you might have keys in there that you don't want to change the namespace to. you can use clojure.set/rename-keys to be explicit

(rename-keys {:id 1 :name "CBC"} {:id :company/id :name :company/name})

👍 2
pppaul02:12:07

rename-keys is the ideal case

escherize16:12:21

True @U4YGF4NGM Or even non keyword types

dpsutton16:12:04

yeah. depends a lot on what is in the map. Often these are all non-namespaced keyword keys. But obviously nothing requires that to be true

1
escherize16:12:12

And its not an answer to your question but if it is nested or an odd shape you may want to look onto clojure.walk/postwalk and postwalk-demo it has some really great uses for similar actions

👍 1