Good MOrning!
mΓ₯ning!
Morning
π
Hello π
morning
Bore da :welsh_flag:
hey @agile_geek, long time!
was thinking the same thing
Hello All...
I have a weird question... How would you filter a lazySeq of maps from a map of keys and values that would make up the predicates? e.g.
{:place-city "Manchester"
:place-iso-country-code "GB"}
Would need to be run as two successive filters on lazySeq "coll":
(->> coll
(filter #(= "Manchester" (:place-city %)))
(filter #(= "GB" (:place-iso-country-code %))))
juxt
?
i'd use juxt and then collect those two seqs i think
I thought of turning the map into a list of vector pairs and reducing over it, but it appears to cause all kinds of type exception horror and basically doesn't work. Same with extracting the keys from the map and reducing over the list of keys
why not just use juxt?
@alex.lynham - I don't know how I would...
It's core library, but I've never used it.
((juxt :place-city :place-iso-country-code) coll)
Also it's not two seqs, it's one Lazy Seq that needs to get smaller and smaller with each successive filter - the map of filter predicates could be a lot bigger
oh
are you filtering by a set?
sorry i thought you wanted several split out seqs
no, I am taking a bunch of querystring params and turning them into a map in order to filter a LazySeq of maps based on equality predicates made from each key-value pair in the map.
can you give an example of the input->output mappings @maleghast? i'm struggling to understand the specification atm
it sounds like you're filtering with a contains predicate over a set?
ahh no i mean some
input -> LazySeq of Maps output -> List of Maps that conform to a single or multiple filters
The predicates for the filters are equality based on key/value from a simple 1d map that is created from querystring parameters
(some #{:your-keys} coll)
?
ahh no that'll only return the first
my bad
keys and values, @alex.lynham I want all the maps that have a match for the value of one or more keys
I can take the LazySeq of maps and ->>
filter it, but creating the anonymous functions to provide the predicates inside the filter forms is the bit I can't seem to wrap my head around.
If I knew the keys and values in advance it would be fine, but I don't
i guess it's just 2 dimensional in that case
my brain jumps to it's something like, make a list of paths, then iterate those on each element, presto
so
/api/places/search?place-city=Manchester&place-iso-country-code=GB
gives rise to the map above ^^
From that I want to programatically create the procession of filters, or at least the same effect as filtering the LazySeq of maps several times
the resulting list of maps should be places that are located in Manchester, GB
yeah like i say it feels simple, grab those values, and iterate over 'em
Yeah, but how..?
so you want output equivalent to filtering the places
collection first with #(= "Manchester" (:city %))
and then #(= "GB" (:iso-country-code %))
?
@mccraigmccraig - Yes, but the predicates could be anything, I don't know in advance what will be on the querystring.
(fn make-pred [k v] (fn [r] (= v (get r k))))
so I am turning the querystring into a map and then trying to drive this process off that map.
dammit craig
how did you type that so fast
i had just written the inner bit lol
Hold on, this is writing a function to return a function ^^ right?
yes, to make a filter predicate from a key-value pair
So I get how I would do that if I had one key / value pair, but if I have 1 or more how do I run filter on all of them one after another..?
Am I on the right track about reducing over the list of predicates..? That seems to throw a wobbler
yes, reduce the predicates onto the seq
something like (reduce (fn [coll' pred] (filter pred coll')) coll pred-list)
I think that I can make that work... Thanks! Just want to say that I realise having asked the question that I already knew that I could return a function from a function, but I would never have thought of it in a million years without your help, so thanks both. For making me explain it and for pointing out the big "return a function" blindspot in my thinking π
@mccraigmccraig - Worked, first time - thanks SO MUCH!
@alex.lynham - Thanks also for engaging and helping me realise I was not explaining well
yw