Fork me on GitHub
#specter
<
2019-07-11
>
ben.mumford13:07:34

given a nested data structure (values could be strings, nil, vectors of maps or maps) what is the best way to prune the tree so that nil entries, empty map entries, empty vectors are removed?

ben.mumford13:07:49

(do-something {:a {:aa 1}                                             
     :b {:ba -1                                             
         :bb 2                                              
         :bc nil
         :bd ""
         :be []
         :bf {}
         :bg {:bga nil}
         :bh [nil]
         :bi [{}]
         :bj [{:bja nil}]}
     :c nil
     :d ""
     :e []
     :f {}
     :g {:ga nil}
     :h [nil]
     :i [{}]
     :j [{:ja nil}]}
=>
{:a {:aa 1} 
     :b {:ba -1 
         :bb 2}}

ben.mumford13:07:49

any help would be much appreciated 🙂

nathanmarz18:07:19

@ben.mumford620 here's how to do that:

(def COMPACTED-VALS-PATH
  (recursive-path [] p
    (continue-then-stay
      (cond-path
        map? [(compact MAP-VALS) p]
        vector? [(compact ALL) p]
        ))))
        
(setval [COMPACTED-VALS-PATH #(or (nil? %) (= "" %))] NONE data)

👍 1
nathanmarz18:07:56

you can insert a MAP-VALS at the start of the path to make sure the top-level data structure stays an empty map instead of becoming NONE if everything gets compacted

dharrigan18:07:53

that's pretty awesome