Fork me on GitHub
#specter
<
2020-06-23
>
ramblurr13:06:14

hey folks, given a vector of vectors of integers, and a separate vector of integers to remove, how can I remove all integers from the sub-vectors that are in the separate vector?

(def to-remove [ 4 5 6 ])
(def coll [ [1 2 4] [3 5 8 ] [6] [] ])
(specter/transform [(specter/filterer not-empty) specter/ALL ..?.. ] specter/NONE coll)

;; => [ [1 2] [3 8] [] [] ]

ramblurr13:06:54

Seems like this works, is there a more efficient way to write it?

(specter/setval [specter/ALL specter/ALL (fn [val]  (some #(= val %) to-remove))] specter/NONE coll)

jsa-aerial17:06:47

Might be nicer if you made to-remove a set, then it is just (to-remove val) or replace fn form with #(to-remove %)

aviflax18:06:58

Hi, sorry if this is a FAQ, but how can I navigate to a map, and then filter the entries in the map, based on certain nested values in the values?

aviflax18:06:23

Here’s what I’m doing in “plain old Clojure” (plus Medley) that I’m curious how to translate to Specter:

(->> (db/read "db")
     (:technologies)
     (medley/filter-vals
       (fn [tech] (some (fn [rec] (rec "ratified"))
                        (get tech "recommendations")))))

smith.adriane18:06:22

not sure it's the most straightforward approach, but you could probably use:

[:technology MAP-VALS]
for the path. in your transform function: • return the original value to keep it • return spec/NONE to remove a value

aviflax18:06:48

Thanks! But I’m not looking to transform, just to query.

aviflax18:06:32

Yeah, I left if off but when I ran the above code, the last form in the thread-last form was keys

smith.adriane18:06:41

[:technology MAP-VALS (fn [tech] (some (fn [rec] (rec "ratified"))
                        (get tech "recommendations")))]

aviflax18:06:01

interesting, I thought I tried that… I’ll try again, brb!

smith.adriane18:06:33

i'm not specter expert either ¯\(ツ)

aviflax18:06:09

Wait… that’ll navigate to the results of the predicate, right? But I want the map under :technologies, just filtered

aviflax18:06:21

I’m very new to Specter but I’d think it’d have something roughly equivalent to Medley’s filter-vals (which filters a map by applying a predicate to the vals in the map, and returns a map containing the matching entries)

aviflax18:06:45

(Or something equivalent to filter that I could use to the same effect)

aviflax18:06:44

Right! I tried that but had trouble with it. Thought maybe I just didn’t understand it. I’ll try again… brb

smith.adriane18:06:12

imo, specter is much more useful if you're transforming or setting data in a deeply nested data structure. if you're just drilling into a nested data structure, I usually just use the normal clojure functions

aviflax18:06:32

that makes sense.

aviflax18:06:54

But I’d like to build up my own intuition as to when to use Specter, by seeing what it’s like to use it in cases like this

aviflax18:06:46

Also, I had to walk a Clojure newbie through the above code and it was non-trivial to explain. I think the path concept could maybe be much more straightforward for folks new to Clojure and Lisps.

smith.adriane18:06:13

i'm only a specter novice, so there's probably a very straightforward way to do it that I don't know about

aviflax18:06:21

I hear ya, I have the same suspicion

aviflax18:06:58

Trying this:

(select [:technologies
         (filterer MAP-VALS "recommendations" ALL "ratified")
         MAP-KEYS]
        db)
getting: java.lang.ClassCastException: "class java.lang.String cannot be cast to class java.util.Map$Entry…

smith.adriane18:06:43

specter doesn't recognize strings as map keys like it does for keywords

smith.adriane18:06:01

I think "recommendations" needs to be (keypath "recommendations")

aviflax18:06:04

I thought i saw that work, one sec…

smith.adriane18:06:39

hmmm, maybe it does

aviflax18:06:46

Yeah, this works: (select [:technologies MAP-VALS "recommendations" ALL] db)

smith.adriane18:06:43

filterer is complaining because it's turning the map into a sequence

smith.adriane18:06:50

maybe something like:

(spec/select [:technologies
              (spec/filterer  (spec/nthpath 1) "recommendations" spec/ALL "ratified")
              ALL
              (spec/nthpath 0)]
        db)

aviflax18:06:47

huh, that’s surprising. I wonder why it’s doing that. I’d think that’d be antithetical to Specter’s general behavior of leaving types as-is

aviflax18:06:58

also: thank you!

aviflax18:06:33

interesting… this “works”, but the result is incorrect:

(select [:technologies
         (filterer (nthpath 1) "recommendations" ALL "ratified")
         MAP-KEYS]
        db)

smith.adriane18:06:42

> with the path yields anything other than an empty sequence.

smith.adriane18:06:48

the path is probably yielding false

aviflax18:06:04

the path in filterer ?

aviflax18:06:12

I wouldn’t think so…?

aviflax18:06:18

why would it yield false?

aviflax18:06:28

I thought any path to something that doesn’t exist yields nil?

smith.adriane18:06:29

or whatever is in the ratified key

smith.adriane18:06:52

does the ratified key exist even for unratified recommendations?

aviflax18:06:22

right. but most of the maps in the sequences that correspond to the "recommendations" keys do not have the key "ratified"; only a few do. I’m trying to find those technologies that have recommendations that have been ratified

smith.adriane18:06:17

this seems to work:

(def data {:technologies
           {0 {"recommendations" [{"ratified" true}]}
            1 {"recommendations" [{"ratified" false}]}
            2 {"recommendations" []}}})

(spec/select [:technologies

              (spec/filterer  (spec/nthpath 1) "recommendations" spec/ALL "ratified" identity)
              spec/MAP-KEYS
              ;; ALL
              ;;(spec/nthpath 0)
              ]
             data)

smith.adriane18:06:53

the above will return [0]

aviflax18:06:07

I don’t get it though 🙃

aviflax18:06:36

why is the identity needed? what is it doing?

smith.adriane18:06:27

so without identity and removing filterer:

(def data {:technologies
           {0 {"recommendations" [{"ratified" true}]}
            1 {"recommendations" [{"ratified" false}]}
            2 {"recommendations" []}}})

(spec/select [:technologies
              ALL
              (spec/nthpath 1) "recommendations" spec/ALL "ratified" 
              ;; spec/MAP-KEYS
              ;; ALL
              ;;(spec/nthpath 0)
              ]
             data)

smith.adriane18:06:36

you get [true false]

aviflax18:06:10

That’s not what I’m getting

smith.adriane18:06:10

basically, it can navigate to the "ratified" key

aviflax18:06:17

oh wait…

smith.adriane18:06:22

I also removed the filterer

aviflax18:06:29

ah interesting

smith.adriane18:06:33

and the path exists if it can navigate there

aviflax18:06:45

I see, you did that as a way to debug the filterer path?

smith.adriane18:06:04

so filterer will keep paths that it can navigate to, even if the value it navigates to is falsey

aviflax18:06:06

cool, that’s clever, makes sense!

aviflax18:06:14

that’s… surprising

aviflax18:06:17

but, so be it

smith.adriane18:06:25

so identity is the simplest function that I can think of to filter based on truthiness

smith.adriane18:06:56

I think filterer has the right design, but it is a little surprising at first

smith.adriane18:06:49

(some? false) => true

smith.adriane19:06:15

actually, boolean is probably clearer

smith.adriane19:06:39

(def data {:technologies
           {0 {"recommendations" [{"ratified" true}]}
            1 {"recommendations" [{"ratified" false}]}
            2 {"recommendations" []}}})

(spec/select [:technologies
              (spec/filterer  (spec/nthpath 1) "recommendations" spec/ALL "ratified" boolean)
              spec/MAP-KEYS
              ]
             data)

smith.adriane19:06:37

some?'s doc string is correct:

clojure.core/some?
 [x]
Added in 1.6
  Returns true if x is not nil, false otherwise.

aviflax19:06:28

So some? is equivalent to (complement nil?) …. ?

aviflax19:06:08

Anyway, I still don’t quite get filterer — but a prerequisite to getting it is seeing what works. Which I’ve got now. Thank you so much!

ramblurr18:06:58

Is there away to setval on a path but only set the first matching value?

ramblurr18:06:25

Using my example from before, if coll was (def coll [ [1 2 4] [3 5 8 4] [6] [] ]) I'd like the result to be ;; => [ [1 2] [3 8 4] [] [] ] (that is, the 4 is only removed once)

ramblurr18:06:24

If I add a FIRST after the (fn ...) in the path, I get a Error: 4 is not ISeqable