How can I query a data structure like so:
{:a 1
:b {:a 2}}
Where I want to find all :a
maps? I tried walker
but it stops when it finds a match, and I also tried:
(select
[(specter/recursive-path [] p
(specter/cond-path
coll? [specter/VAL [specter/ALL p]]
:else specter/STAY)))
]
)
But it didn't finish, and I think that's because it was recursing into the VALIt might not finish because it was trying to print such a massive value, actually. My bad 🙂 I can't seem to filter the results like I want to though.
so, when you say that you want all :a
maps, do you mean you want to traverse the entire tree and get all the values for any :a
keys in any nested substructure or you only want to descend the :a
branches to look for sub values?
err, what is the intended output with input {:a 1 :b {:a 2}}
are :a values always terminal? can you have {:a {:a 5}}
?
(spr/select [(spr/stay-then-continue spr/MAP-VALS) (spr/must :a)] {:a 1 :b {:a 2}})
=> [1 2]
@idiomancy I'm interested in the map itself, rather than the values. I want to traverse the entire tree and get all the values with an :a
key.
is my solution giving you the correct answer?
I crave validation
@idiomancy for my particular data structure, just running the stay-then-continue gives me:
clojure.lang.PersistentHashMap cannot be cast to java.util.Map$Entry
:thinking_face:in the actual operation are you doing, like (map #(select [...] %) yourData)
?
Nope, but my data is a list
is it a list of maps or a list of lists?
Oh, I realize I've probably neglected something important. While it is a list of maps, those maps might look like:
{:a 1
:b [{:a 2}]}
hahaha oh-ho!
well then
yes, mapvals will blow up there
(def dominicm-selector
(spr/recursive-path [desired-key] p
(spr/cond-path
sequential? [spr/ALL p]
map? [(spr/stay-then-continue [spr/MAP-VALS p])
(spr/must desired-key)]
:else spr/STAY)))
(spr/select (dominicm-selector :a) [{:a 1 :b [{:a {:a 4}}]}])
=> [1 4]
@idiomancy that's returning a much smaller list than I expected :thinking_face:
I also realize that else branch is unnecessary, I assume all lists only contain maps right? because I realize that selector will also catch any values in lists that aren't maps
Nope 😄 lists can contain anything
this is a massive data structure, and I'm trying to pull out just the bits that I'm interested in.
hahah hmm