meander

All things about https://github.com/noprompt/meander Need help and no one responded? Feel free to ping @U5K8NTHEZ
ingesol 2020-01-28T13:45:39.100500Z

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

jimmy 2020-01-28T14:03:05.106800Z

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.

ingesol 2020-01-28T14:20:17.108200Z

@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 🙂

jimmy 2020-01-28T14:21:57.110500Z

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.

dominicm 2020-01-28T15:20:00.111Z

The all strategy would work, yeah

noprompt 2020-01-28T17:06:17.111300Z

(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}}

noprompt 2020-01-28T17:07:33.111800Z

I used nil for the namespace there but you could use anything else.

noprompt 2020-01-28T17:13:25.112600Z

m/cata is about as close to an answer to Specter as we come.

noprompt 2020-01-28T17:15:10.113400Z

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}}

đź‘Ť 1
jimmy 2020-01-28T17:41:08.114700Z

I assumed that it wasn't just nested maps, there might be vectors, seq, etc in there. But if it is just maps, then what @noprompt posted works. @ingesol.

đź‘Ť 1
noprompt 2020-01-28T17:46:06.115500Z

Ah, yeah, if its a generic thing then strategy might be better. I read “multi-level map” as “just nested maps”. 🙂