specter

Latest version: 1.1.3
steveb8n 2018-02-22T01:26:56.000143Z

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

steveb8n 2018-02-22T01:27:01.000112Z

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

steveb8n 2018-02-22T01:27:48.000179Z

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

nathanmarz 2018-02-22T02:11:31.000247Z

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

nathanmarz 2018-02-22T02:12:09.000204Z

e.g.

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

1
steveb8n 2018-02-22T02:15:55.000020Z

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

steveb8n 2018-02-22T02:17:35.000058Z

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.

steveb8n 2018-02-22T02:20:35.000254Z

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

steveb8n 2018-02-22T02:21:32.000208Z

And thanks 🙂

nathanmarz 2018-02-22T02:32:21.000047Z

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

steveb8n 2018-02-22T02:38:32.000226Z

Ok. That works. Thanks again

sashton 2018-02-22T17:52:20.000728Z

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}}

nathanmarz 2018-02-22T17:55:45.000612Z

@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)

sashton 2018-02-22T17:58:31.000008Z

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.

nathanmarz 2018-02-22T17:58:58.000356Z

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

nathanmarz 2018-02-22T18:02:01.000032Z

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

nathanmarz 2018-02-22T18:06:42.000210Z

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)

1
sashton 2018-02-22T20:07:05.000262Z

thanks again. that works