This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-05-19
Channels
- # announcements (1)
- # asami (75)
- # beginners (16)
- # calva (14)
- # cider (4)
- # clj-kondo (11)
- # cljs-dev (3)
- # clojure (110)
- # clojure-australia (1)
- # clojure-china (1)
- # clojure-europe (38)
- # clojure-hk (1)
- # clojure-indonesia (1)
- # clojure-japan (1)
- # clojure-korea (1)
- # clojure-my (1)
- # clojure-nl (1)
- # clojure-sg (1)
- # clojure-spec (1)
- # clojure-taiwan (1)
- # clojure-uk (2)
- # clojurescript (34)
- # conjure (1)
- # data-science (9)
- # datahike (7)
- # datalevin (1)
- # datascript (1)
- # datomic (5)
- # etaoin (1)
- # fulcro (23)
- # graalvm (50)
- # helix (4)
- # hyperfiddle (8)
- # introduce-yourself (1)
- # jobs (3)
- # kaocha (10)
- # malli (8)
- # mid-cities-meetup (1)
- # minecraft (1)
- # off-topic (13)
- # pathom (14)
- # polylith (38)
- # reitit (1)
- # releases (1)
- # sci (65)
- # shadow-cljs (28)
- # specter (12)
- # tools-deps (8)
- # vim (1)
- # vscode (11)
- # xtdb (31)
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
?
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?
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?
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.you should be able to just add (nthpath 0)
to the end of your path
> (specter/select [(specter/view frequencies) specter/ALL (specter/pred #(> (second %) 1)) (specter/nthpath 0)]
"akdsfhaiohflaiseflakejhlkajsefhs9")
[\a \e \f \h \i \j \k \l \s]
Thanks, that worked… but why does pred lose the MapEntry-ness of the elements? Why shouldn’t MAP-KEYS work there?
I don't think it does. I think MAP-KEYS expects a map
> (type(first(specter/select [(specter/view frequencies) specter/ALL (specter/pred #(> (second %) 1)) ]
"akdsfhaiohflaiseflakejhlkajsefhs9")))
clojure.lang.MapEntry
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?
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)
@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.