Fork me on GitHub
#specter
<
2017-02-03
>
nha16:02:51

Is specter suited to transform clojure map keys?

{:a {:b {:c 123}} :d 456}
{:a_b_c 123 :d 456}
Something like that, back and forth.

nathanmarz16:02:29

@nha you can do something like this:

user=> (def PATH-MAP-WALKER
  #_=>   (recursive-path [] p
  #_=>     (if-path map?
  #_=>       [ALL (collect-one FIRST) LAST p]
  #_=>       STAY
  #_=>       )))
#'user/PATH-MAP-WALKER
user=> 

user=> 

user=> (reduce
  #_=>   (fn [m path]
  #_=>     (assoc m (apply str (butlast path)) (last path)))
  #_=>   {}
  #_=>   (traverse PATH-MAP-WALKER {:a {:b {:c 1}} :d 2}))
{":a:b:c" 1, ":d" 2}

nha16:02:36

Nice 🙂 I don’t really understand the traverse part: it returns a function? Can I view it as a collection? (I am trying to make the reverse function now - but I am also just starting out with specter)

nha16:02:32

Ah, get it reducible object.

nathanmarz16:02:46

@nha traverse is very efficient, there's no materialization of intermediate data structures (similar to transducers)

nha16:02:38

Yes, starting to understand that 🙂 Seems easy enough to modify even without understanding every fn in there. Thanks!