specter

Latest version: 1.1.3
ben.mumford 2019-07-11T13:20:34.017600Z

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.mumford 2019-07-11T13:21:49.018500Z

(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.mumford 2019-07-11T13:22:49.019Z

any help would be much appreciated 🙂

nathanmarz 2019-07-11T18:24:19.019500Z

@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
ben.mumford 2019-07-12T07:26:06.021Z

cheers pal

nathanmarz 2019-07-11T18:25:56.020400Z

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

dharrigan 2019-07-11T18:27:53.020700Z

that's pretty awesome