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?
(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}}
any help would be much appreciated 🙂
@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)
cheers pal
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
that's pretty awesome