Fork me on GitHub
#specter
<
2022-05-19
>
Matthew Davidson (kingmob)04:05:49

When would you use transformed over view? Transformed takes a path relative to your current position, but wouldn’t it be easier to navigate normally and use view?

Matthew Davidson (kingmob)05:05:42

Does specter maintain sorted status? I.e., if I transform a sorted-map, should I get a sorted map back, or is that not part of the contract?

Matthew Davidson (kingmob)05:05:39

OK, maybe this is a dumb one, but how on earth do I filter a map based on the values, but retrieve the associated keys? E.g., I called frequencies, and want to filter out all keys whose val is 1. Is there a way to do this without using collect?

Matthew Davidson (kingmob)05:05:05

I tried

(select [(view frequencies) ALL (pred #(> (second %) 1))]
        my-data)
but if I add MAP-KEYS to the path to get what I want, I get a blank ClassCastException with no message or trace. Addendum: I get a surprising number of super-unhelpful blank NPEs and ClassCastExceptions. Any message is better than none.

phronmophobic06:05:00

you should be able to just add (nthpath 0) to the end of your path

phronmophobic06:05:29

> (specter/select [(specter/view frequencies) specter/ALL (specter/pred #(> (second %) 1)) (specter/nthpath 0)]
                                                 "akdsfhaiohflaiseflakejhlkajsefhs9")
[\a \e \f \h \i \j \k \l \s]

Matthew Davidson (kingmob)06:05:01

Thanks, that worked… but why does pred lose the MapEntry-ness of the elements? Why shouldn’t MAP-KEYS work there?

phronmophobic06:05:51

I don't think it does. I think MAP-KEYS expects a map

phronmophobic06:05:03

> (type(first(specter/select [(specter/view frequencies) specter/ALL (specter/pred #(> (second %) 1)) ]
                                                        "akdsfhaiohflaiseflakejhlkajsefhs9")))
clojure.lang.MapEntry

Matthew Davidson (kingmob)06:05:55

So ALL transforms it into MapEntries…but the doc for MAP-VALS say it’s faster than [ALL FIRST], which really makes it sound like it’s equivalent to it. I think I get it in this case, since we turn them into MapEntries and then use the pred on the MapEntry, which Clojure prtends is a 2-elt vec… How would I filter a map’s values but keep the navigator at the map level?

rolt12:06:41

it's more like: you can view a map entry as a 2 element vec,. you can (and you should) use [MAP-VALS] instead of [ALL SECOND] (if it says FIRST in the doc it's a mistake, it would be equivalent to MAP-KEYS). To filter a map's value while staying at the map level you can use transformed or view: (sp/select [(sp/transformed [sp/MAP-VALS (sp/pred (complement your-pred)] (constantly sp/NONE))] data) (sp/select [(sp/view #(into {} (filter (comp your-pred val)) %))] data)

Matthew Davidson (kingmob)06:06:28

@U02F0C62TC1 Thanks for responding. I appreciate it. But I think I’m going to pass on Specter. There’s some really cool core usefulness there, but I find it hard to debug (the NPEs really don’t help, those should be fixed), or figure out which navigators I need to solve a problem sometimes.