Fork me on GitHub
#specter
<
2018-06-07
>
nathanmarz01:06:29

@sophiago it's a lot easier if your maps have more information about what they represent

nathanmarz01:06:50

this would be a way to do it, though it's not very efficient:

(def data {:baz "qux", :env [{:locals "foo" :k "v"} {:locals "bar"} {:baz "qux"}]})

(def MAP-NODES
  (recursive-path [] p
   (cond-path map? (stay-then-continue MAP-VALS p)
              coll? [ALL p]
              )))

(transform
  [MAP-NODES
   (not-selected?
     MAP-VALS
     MAP-NODES
     (must :locals))]
  #(select-keys % [:locals])
  data)

sophiago01:06:22

Unfortunately, I don't have control over data representation in this case. But I think we're converging on something similar. I'm trying something like this: (setval [MAP-NODES #(and (not= :locals (first %)) (not (coll? (second %))))] NONE {:baz "qux", :env [{:locals "foo"} {:locals "bar"} {:baz "qux"}]})

nathanmarz01:06:27

this is a cleaner way to write that:

(setval
  [MAP-NODES
   ALL
   (not-selected? FIRST (pred= :locals))
   LAST
   (complement coll?)]
  NONE
  data)

nathanmarz01:06:18

if you want to get rid of the empty map you can do this:

(def data {:baz "qux", :env [{:locals "foo" :k "v"} {:locals "bar"} {:baz "qux"}]})

(def MAP-NODES
  (recursive-path [] p
   (cond-path map? (stay-then-continue MAP-VALS p)
              coll? [(compact ALL p)]
              )))

(setval
  [MAP-NODES
   ALL
   (not-selected? FIRST (pred= :locals))
   LAST
   (complement coll?)]
  NONE
  data)

sophiago01:06:45

Nice. I think this might be what I'm looking for

sophiago01:06:14

Okay, yeah I think that's good enough I can follow it from here. compact is being called on a keyword at some point in the traversal of a full ast, but I probably want to clean up the results further. Thanks yet again, @nathanmarz

sophiago01:06:31

I should probably think about contributing to your docs if you think this is a common enough use case. I would think Specter is ideal for working with tools.analyzer and in fact there was one Jira ticket for the latter related to Ridley.