I’m struggling to understand compact
. in the example, shouldn’t the :c that has a value be left alone?
(setval [ALL :a (compact :c)]
NONE
[{:a {:b 1 :c 2}}
{:a {:b 1 :c nil}}])
=>
[{:a {:b 1}} {:a {:b 1}}]
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
@steveb8n compact
applies to the value being navigated on, in this case the submaps
e.g.
(setval [ALL :a (compact :c)]
NONE
[{:a {:c 2}}
{:a {:b 1 :c nil}}])
;; => [{} {:a {:b 1}}]
That makes sense. Reading that expression it makes sense that it unconditionally removes the value.
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.
I can write a transform fn or conditional navigation but wonder if you would use compact or some other elegance for that as well?
And thanks 🙂
@steveb8n it's just (setval [ALL :a :c nil?] NONE data)
Ok. That works. Thanks again
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}}
@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)
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.
there's also the submap
navigator which for selects is equivalent to select-keys
though it currently changes sorted maps to unsorted maps if used in a select
https://github.com/nathanmarz/specter/issues/235
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)
thanks again. that works