specter

Matthew Davidson 2022-05-19T04:47:49.783719Z

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 2022-05-19T05:15:42.823519Z

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 2022-05-19T05:42:39.715589Z

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 2022-05-19T05:46:05.712249Z

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.

phronmophobic 2022-05-19T06:09:00.202939Z

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

phronmophobic 2022-05-19T06:09:29.812919Z

> (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 2022-05-19T06:14:01.381429Z

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

phronmophobic 2022-05-19T06:17:51.910979Z

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

phronmophobic 2022-05-19T06:18:03.376019Z

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

Matthew Davidson 2022-05-19T06:29:55.546899Z

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?

rolt 2022-06-08T12:31:41.826049Z

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 2022-06-09T06:20:28.353339Z

@rolthiolliere 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.