Fork me on GitHub
#specter
<
2017-05-12
>
ksmithbaylor22:05:11

Hey everyone! I've got a nested map (It's a deserialization of a flat file JSON "database" for a budgeting app I'm extending) that looks similar to this (stripped down somewhat):

{:categories [{:name "Monthly bills"
               :subcategories [{:name "Electric"}
                               {:name "Cable" :inactive true}
                               {:name "Mortgage"}
                               {:name "Natural Gas"}]}
              {:name "Food"
               :subcategories [{:name "Eating Out"}
                               {:name "Groceries"}]}
              {:name "Stuff"
               :inactive true
               :subcategories [{:name "Things" :inactive true}
                               {:name "Whatever"}]}]
 :accounts [{:name "Checking"
             :balance 100}
            {:name "Old account"
             :balance 0
             :inactive true}
            {:name "Savings"
             :balance 500}]}
I'm fairly new to specter, and am still learning about what's possible. How would I recursively remove all maps at all levels that have an :inactive true flag? The result I would get from the above would be:
{:categories [{:name "Monthly bills"
               :subcategories [{:name "Electric"}
                               {:name "Mortgage"}
                               {:name "Natural Gas"}]}
              {:name "Food"
               :subcategories [{:name "Eating Out"}
                               {:name "Groceries"}]}]
 :accounts [{:name "Checking"
             :balance 100}
            {:name "Savings"
             :balance 500}]}

nathanmarz23:05:15

@ksmithbaylor it would be something like:

(def NODES
  (recursive-path [] p
    (continue-then-stay
      (must :subcategories) ALL p
      )))

(setval [(multi-path :categories :accounts)
         ALL
         NODES
         (pred :inactive)]
  NONE
  data)

ksmithbaylor23:05:05

This is doing almost what I want: (setval [(walker #(and (map? %) (:inactive %)))] nil data)

ksmithbaylor23:05:29

but now I need to recursively remove the nils

ksmithbaylor23:05:10

I'd like to keep it generic so I don't need to know/care what keys are in each level

nathanmarz23:05:56

it's generally better to be more precise about the structure of your data

nathanmarz23:05:07

walker doesn't perform well and has some pitfalls

nathanmarz23:05:23

you can just replace nil with NONE there to get the maps removed

nathanmarz23:05:40

actually nevermind, walker doesn't implement NONE removal