This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-12-22
Channels
- # adventofcode (12)
- # announcements (6)
- # aws (5)
- # babashka (57)
- # beginners (40)
- # calva (17)
- # clojure-europe (10)
- # clojure-nl (1)
- # clojure-norway (21)
- # clojure-uk (3)
- # clojuredesign-podcast (4)
- # cursive (3)
- # datomic (9)
- # etaoin (5)
- # fulcro (12)
- # hyperfiddle (42)
- # missionary (2)
- # off-topic (11)
- # reagent (6)
- # scittle (131)
- # squint (3)
- # tools-deps (4)
- # uncomplicate (1)
- # vscode (1)
I'm trying to filter a map based on two values (by key-lookup). This does not work as intended, but perhaps it illustrates what I'm trying to accomplish and what I'm doing wrong:
(filter #(and (= "python-test-env" (:projectName %))
(= "dev" (:stackName %))))
coll
(the list of maps) is piped into filter
with ->>
I have a list of maps, each map containing several key-vals:
({:orgName "my-org",
:projectName "my-project",
:stackName "stack-1,
:lastUpdate 1695305272,
:resourceCount 14}
{:orgName "my-org",
:projectName "my-project",
:stackName "stack-2",
:lastUpdate 1695106891,
:resourceCount 16}
{:orgName "my-org",
:projectName "my-project",
:stackName "stack-3",
:lastUpdate 1698324490,
:resourceCount 16}
...)
And I want to get only those maps where the criteria match.
How can I do that?(filter #(= ["dev" "python-test-env"] ((juxt :stackName :projectName) %)))
Wow, nice! Thanks. Here's a working code sample in case anyone else finds it useful:
(let [data (list {:orgName "my-org"
:projectName "my-project"
:stackName "stack-1"
:lastUpdate 1695305272
:resourceCount 14}
{:orgName "my-org"
:projectName "python-test-env"
:stackName "dev"
:lastUpdate 1695106891
:resourceCount 16}
{:orgName "my-org"
:projectName "my-project"
:stackName "stack-3"
:lastUpdate 1698324490
:resourceCount 16})]
(->> data
(filter #(= ["dev" "python-test-env"]
((juxt :stackName :projectName) %)))))
however, your snippet should work just fine. My example does not do anything different
Ah, wow! Yes, it does work with my test data! But not with the data returned from the API I'm using. Weird.
maybe keys a not keywords? some clients doesn't translate keys automaticaly from strings
I'm converting it to keywords like so:
(defn response-body->json [response]
(-> (:body response)
(json/parse-string keyword)))
(defn make-request [request]
(-> request
(http-kit/request response-body->json)
deref))
(defn fetch-page [request & [token]]
(-> request
(cond-> token (assoc-in [:query-params :continuationToken] token))
make-request))
(defn fetch-all-pages [request val-fn key-fn]
(-> (iteration (partial fetch-page request)
:vf val-fn
:kf key-fn)
seq
flatten))
Using (json/parse-string keyword)
in response-body->json
And here is my whole ->>
thread:
(doall
(->> (fetch-all-pages stack-request :stacks :continuationToken)
(filter #(and (= "python-test-env" (:projectName %))
(= "dev" (:stackName %))))
(create-permission-request "PULUMI_ALL_STACKS_READ" :read)
make-request))
(doall
(->> (fetch-all-pages stack-request :stacks :continuationToken)
(filter #(and (= "python-test-env" (:projectName %))
(= "dev" (:stackName %))))
(create-permission-request "PULUMI_ALL_STACKS_READ" :read)
make-request))
you missed filter
call. probably just a typoAh, sorry, that was actually a copy-paste error from when I tried your juxt
solution and reverted back to mine. I edited my thread above again now.
I found out that (fetch-all-pages stack-request :stacks :continuationToken)
returns a clojure.lang.LazySeq
(not a list of maps) which is passed on to filter
.
I would start step by step debugging but usually this depends on IDE support. another way could be this:
(filter (fn [x]
(prn "---DBG" x)
(= ["dev" "python-test-env"] ((juxt :stackName :projectName) x))))
just print every item that is passed to the filter to see unexpected data structure
Oh my Lord, I'm such a dumbass. python-test-repo
was the actual project name. I just filtered for the wrong string 😅:man-facepalming:
And my code was correct, haha! Sorry for wasting your time, @U04V4KLKC
Hey all, I get this error when trying to build the uberjar, where do I look for the files to add to exclusions?
Execution error (ExceptionInfo) at clojure.tools.build.tasks.uber/explode (uber.clj:172).
Cannot write META-INF/license/LICENSE.aix-netbsd.txt from io.grpc/grpc-netty-shaded as parent dir is a file from another lib. One of them must be excluded.
Full report at:
/var/folders/mr/vq80v4cn4rgdtl1pbfrcfvvr0000gp/T/clojure-5285222528425535868.edn
I searched Slack for exclude license
What happens if you add-watch and reuse the keyword?
it looks like the watches are added to a map, and add-watch calls assoc
, so the old watch is replaced
hm. so if you are creating watches programmatically, what should you do about a key? just grab something from your context that would be unique?
thanks guys
Why is (seq? [1 2 3])
false?
The sort of glib answer is that a vector doesn't implement ISeq. If the desired is a function that can identify if its argument can be converted to a sequence with the seq
function, that's seqable?
vectors are not seqs, but can provide a seq view
seq functions seq
their argument to obtain the view
Thank you. This raises more questions but I will try to divine the answers myself first.
You might find https://insideclojure.org/2015/01/02/sequences/ interesting
You might find https://insideclojure.org/2015/01/02/sequences/ interesting