This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-19
Channels
- # aleph (11)
- # aws (1)
- # beginners (14)
- # bitcoin (1)
- # boot (41)
- # cider (6)
- # cljs-dev (1)
- # cljsrn (13)
- # clojure (138)
- # clojure-italy (10)
- # clojure-nl (1)
- # clojure-poland (2)
- # clojure-russia (62)
- # clojure-sg (2)
- # clojure-spec (31)
- # clojure-uk (51)
- # clojurescript (109)
- # core-matrix (1)
- # core-typed (1)
- # cursive (63)
- # datomic (10)
- # emacs (9)
- # euroclojure (1)
- # hoplon (112)
- # immutant (16)
- # jobs (2)
- # lumo (5)
- # off-topic (14)
- # om (54)
- # onyx (17)
- # parinfer (23)
- # pedestal (2)
- # re-frame (41)
- # ring-swagger (23)
- # spacemacs (9)
- # specter (10)
- # uncomplicate (5)
- # vim (1)
Hi, I’m new to specter and have a select I keep trying to do but I’m not quite sure how. I have data like this:
{:foo [{:data "text" :required true}]
:bar [{:data "text"}]
:baz [{:data "text" :required true}]}
and I’d like the list of keys whose value has :required in the inner map. This seems to do it: (map first (s/select [s/ALL (fn[[k,v]] (:required (first v)))] data))
and return the expected (:foo :baz)
Is there a better way? There have been a few places where I’d like the outer key depending on some value lower in the data structure.@slpssm can those inner vectors have multiple elements?
if so then it's (select [ALL (selected? LAST ALL (must :required)) FIRST] data)
otherwise it's (select [ALL (selected? LAST FIRST (must :required)) FIRST] data)
to match your code
this code just checks for existence of a :required key, if you need to match only when :required is truthy replace (must :required)
with :required identity
I think it can have more than one item, but I’ve never seen one so I’m going with the first version. 🙂 With a note on the second version just in case. Now I just need to parse this so I know how it works.
selected?
is a filter based on whether the subpath selects at least a single value
LAST
gets you into the array, the next ALL
looks at key/values, (must :required)
does the :required
check and the last FIRST
is the outer key. About right?
the initial LAST
goes to the value for that key/value pair, the subsequent ALL
looks at all maps inside that vector