Fork me on GitHub
#specter
<
2018-02-22
>
steveb8n01:02:56

I’m struggling to understand compact. in the example, shouldn’t the :c that has a value be left alone?

steveb8n01:02:01

(setval [ALL :a (compact :c)] 
        NONE
        [{:a {:b 1 :c 2}}
         {:a {:b 1 :c nil}}])
=>
[{:a {:b 1}} {:a {:b 1}}]

steveb8n01:02:48

the docstring says that it only applies when the value is empty. that is exactly what I want but I’m missing how to make that work

nathanmarz02:02:31

@steveb8n compact applies to the value being navigated on, in this case the submaps

nathanmarz02:02:09

e.g.

(setval [ALL :a (compact :c)] 
        NONE
        [{:a {:c 2}}
         {:a {:b 1 :c nil}}])
;; => [{} {:a {:b 1}}]

steveb8n02:02:55

That makes sense. Reading that expression it makes sense that it unconditionally removes the value.

steveb8n02:02:35

But what if I want to conditionally remove it i.e. when value is nil. Is there an idiomatic way to do that? The docstring suggests that behaviour, hence my confusion.

steveb8n02:02:35

I can write a transform fn or conditional navigation but wonder if you would use compact or some other elegance for that as well?

steveb8n02:02:32

And thanks 🙂

nathanmarz02:02:21

@steveb8n it's just (setval [ALL :a :c nil?] NONE data)

steveb8n02:02:32

Ok. That works. Thanks again

sashton17:02:20

i’m just trying out specter. what’s the best way to select-keys at various levels of a tree:

;; input
{:a {:aa 1 :ab 2 :ac 3}
 :b {:ba 10 :ba 11}
 :c {:ca 100}}

;; desired output
{:a {:aa 1 :ab 2}
 :c {:ca 100}}

nathanmarz17:02:45

@sashton to maintain the structure of the input, select what to remove rather than what to keep:

(setval (multi-path :b [:a :ac]) NONE data)

sashton17:02:31

thanks @nathanmarz. is remove the only option? the data i’m looking at has lots of extra fields which i’m not interested in. while i could list all the fields to delete, it might get tedious.

nathanmarz17:02:58

there's also the submap navigator which for selects is equivalent to select-keys

nathanmarz18:02:01

though it currently changes sorted maps to unsorted maps if used in a select https://github.com/nathanmarz/specter/issues/235

nathanmarz18:02:42

this is how you could go about doing it with submap:

(defdynamicnav viewed [path viewnav]
  (transformed path (fn [s] (select-any viewnav s)))
  )

(select-any [(viewed :a (submap [:aa :ab])) (submap [:a :c])] data)

sashton20:02:05

thanks again. that works