Fork me on GitHub
#specter
<
2022-06-29
>
hanDerPeder12:06:14

given

[{:id 1
    :some {:nested {:value 11}}}
   {:id 2
    :some {:nested {:value 3}}}]
I want to remove all top-level objects where [:some :nested :value] is greater than 10. How do I do that?

hanDerPeder12:06:03

not correct:

(transform
   (subselect ALL :some :nested :value #(> % 10))
   NONE
   data)
gives:
[{:id 1, :some {:nested {}}} {:id 2, :some {:nested {:value 3}}}]
but I want:
[{:id 2, :some {:nested {:value 3}}}]

hanDerPeder12:06:05

this is close, but I feel I’m missing something

(transform
   [ALL (fn [x]
          (> (-> x :some :nested :value) 10))]
   NONE
   data)
=> [nil {:id 2, :some {:nested {:value 3}}}]

hanDerPeder12:06:03

this does what i need:

(setval
 [ALL (fn [x]
        (> (-> x :some :nested :value) 10))]
 NONE
 data)
but I am expecting there to be something I can replace the function with. like:
(setval
 [ALL (something [:some :nested :value #(> % 10)])]
 NONE
 data)

rolt12:06:36

(setval (filterer :some :nested :value (pred> 10)) [] data)

hanDerPeder12:06:35

aha pred I see. why empty vector?

rolt12:06:46

filterer works on a collection, it returns the filtered collection like filter

rolt12:06:31

i don't think pred is necessary here, you can use your function directly

hanDerPeder12:06:11

hmm, have to think on this. still don’t grok this. thanks!

rolt12:06:25

but i'm surprised that (setval (subselect ALL :some :nested :value (pred> 10)) [] data) doesn't work though

rolt13:06:55

just though of something that you might prefer: (setval [ALL (if-path [:some :nested :value #(> % 10)] STAY)] NONE data). btw you could simply use filter/remove here, with selected-any? if you want to use specter for the path, or is it inside a nested structure too ?

rolt13:06:40

And even if the beginning is a path, it's just a select operation select-one (filterer :some :nested :value #(< % 10))