I have a case: I want to transform a multi-level map, where all keys on all levels should be updated to have a certain namespace. Is this a natural problem to solve for meander? Been browsing the examples, did not find anything obviously similar
If you want to just generically update all the keys on all levels, no I don't think that is a great fit for meander alone. You can definitely accomplish that with clojure.walk and meander together. I've done plenty of things combining those two. On my phone but the basic idea is to do the walk and then have a function with
(m/match x
(m/keyword nil ?name)
(m/keyword "othernamespace" ?name)
?x ?)
Like I said on my phone so that may not be exactly right, but that is the idea.@jimmy Thanks! Yes I suspected this might be a bit on the side. I considered the clojure.walk solution, as well as using specter. No problem finding a simple enough solution 🙂
Yeah Specter is definitely designed around generic transformations like that. You could also accomplish the same thing with meander strategies. But it is definitely not what meander is focused on. Instead we focus on more concrete, specific transformations.
The all strategy would work, yeah
(m/rewrite {:foo 1 :bar {:baz 3}}
{(m/keyword nil ?name) (m/cata ?value) & (m/cata ?rest)}
{(m/keyword "new-namespace" ?name) ?value & ?rest}
{?k (m/cata ?value) & (m/cata ?rest)}
{?k ?value & ?rest}
?x ?x)
;; =>
#:new-namespace{:foo 1, :bar #:new-namespace{:baz 3}}
I used nil
for the namespace there but you could use anything else.
m/cata
is about as close to an answer to Specter as we come.
If you don’t want to use rewrite you can use find
instead:
(m/find {:foo 1 :bar {:baz 3}}
{(m/keyword nil ?name) (m/cata ?value) & (m/cata ?rest)}
(merge {(keyword "new-namespace" ?name) ?value} ?rest)
{?k (m/cata ?value) & (m/cata ?rest)}
(merge {?k ?value} ?rest)
?x ?x)
;; =>
#:new-namespace{:foo 1, :bar #:new-namespace{:baz 3}}
Ah, yeah, if its a generic thing then strategy might be better. I read “multi-level map” as “just nested maps”. 🙂