clojure-uk

A place for people in the UK, near the UK, visiting the UK, planning to visit the UK or just vaguely interested to randomly chat about things (often vi and emacs, occasionally clojure). More general the #ldnclj
dharrigan 2020-08-20T06:13:03.000200Z

Good MOrning!

mccraigmccraig 2020-08-20T06:25:33.000400Z

mΓ₯ning!

jiriknesl 2020-08-20T06:28:54.000600Z

Morning

djm 2020-08-20T07:01:57.000800Z

πŸ‘‹

Jakob Durstberger 2020-08-20T07:34:28.001100Z

Hello πŸ™‚

alexlynham 2020-08-20T08:29:10.001200Z

morning

agile_geek 2020-08-20T08:39:37.001500Z

Bore da :welsh_flag:

mccraigmccraig 2020-08-20T09:05:11.002Z

hey @agile_geek, long time!

πŸ‘‹ 1
alexlynham 2020-08-20T09:05:58.002200Z

was thinking the same thing

maleghast 2020-08-20T14:55:27.002600Z

Hello All...

maleghast 2020-08-20T15:01:35.007500Z

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 %))))

alexlynham 2020-08-20T15:02:46.008700Z

juxt?

alexlynham 2020-08-20T15:03:15.008900Z

i'd use juxt and then collect those two seqs i think

maleghast 2020-08-20T15:03:30.009100Z

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

alexlynham 2020-08-20T15:03:45.009300Z

why not just use juxt?

maleghast 2020-08-20T15:03:53.009600Z

@alex.lynham - I don't know how I would...

maleghast 2020-08-20T15:04:11.009900Z

It's core library, but I've never used it.

alexlynham 2020-08-20T15:04:58.010300Z

((juxt :place-city :place-iso-country-code) coll)

maleghast 2020-08-20T15:05:24.011Z

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

alexlynham 2020-08-20T15:05:52.011100Z

oh

alexlynham 2020-08-20T15:05:59.011200Z

are you filtering by a set?

alexlynham 2020-08-20T15:06:12.011400Z

sorry i thought you wanted several split out seqs

maleghast 2020-08-20T15:07:00.012800Z

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.

mccraigmccraig 2020-08-20T15:07:11.013100Z

can you give an example of the input->output mappings @maleghast? i'm struggling to understand the specification atm

alexlynham 2020-08-20T15:07:36.013600Z

it sounds like you're filtering with a contains predicate over a set?

alexlynham 2020-08-20T15:08:15.014400Z

ahh no i mean some

maleghast 2020-08-20T15:08:15.014500Z

input -> LazySeq of Maps output -> List of Maps that conform to a single or multiple filters

maleghast 2020-08-20T15:08:48.015300Z

The predicates for the filters are equality based on key/value from a simple 1d map that is created from querystring parameters

alexlynham 2020-08-20T15:08:55.015400Z

(some #{:your-keys} coll)?

alexlynham 2020-08-20T15:09:13.015700Z

ahh no that'll only return the first

alexlynham 2020-08-20T15:09:16.015800Z

my bad

maleghast 2020-08-20T15:09:27.016200Z

keys and values, @alex.lynham I want all the maps that have a match for the value of one or more keys

maleghast 2020-08-20T15:10:42.017300Z

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.

maleghast 2020-08-20T15:11:01.017800Z

If I knew the keys and values in advance it would be fine, but I don't

alexlynham 2020-08-20T15:11:18.018100Z

i guess it's just 2 dimensional in that case

alexlynham 2020-08-20T15:11:53.018700Z

my brain jumps to it's something like, make a list of paths, then iterate those on each element, presto

maleghast 2020-08-20T15:12:54.020200Z

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

maleghast 2020-08-20T15:13:30.021Z

the resulting list of maps should be places that are located in Manchester, GB

alexlynham 2020-08-20T15:14:02.021600Z

yeah like i say it feels simple, grab those values, and iterate over 'em

maleghast 2020-08-20T15:14:14.022Z

Yeah, but how..?

mccraigmccraig 2020-08-20T15:14:56.023300Z

so you want output equivalent to filtering the places collection first with #(= "Manchester" (:city %)) and then #(= "GB" (:iso-country-code %)) ?

maleghast 2020-08-20T15:15:42.024200Z

@mccraigmccraig - Yes, but the predicates could be anything, I don't know in advance what will be on the querystring.

mccraigmccraig 2020-08-20T15:16:22.025200Z

(fn make-pred [k v] (fn [r] (= v (get r k))))

maleghast 2020-08-20T15:16:27.025300Z

so I am turning the querystring into a map and then trying to drive this process off that map.

alexlynham 2020-08-20T15:16:37.025400Z

dammit craig

alexlynham 2020-08-20T15:16:42.025500Z

how did you type that so fast

alexlynham 2020-08-20T15:17:01.025800Z

i had just written the inner bit lol

maleghast 2020-08-20T15:17:31.026500Z

Hold on, this is writing a function to return a function ^^ right?

mccraigmccraig 2020-08-20T15:17:53.027Z

yes, to make a filter predicate from a key-value pair

maleghast 2020-08-20T15:18:13.027500Z

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..?

maleghast 2020-08-20T15:18:49.028300Z

Am I on the right track about reducing over the list of predicates..? That seems to throw a wobbler

mccraigmccraig 2020-08-20T15:19:10.028700Z

yes, reduce the predicates onto the seq

mccraigmccraig 2020-08-20T15:20:26.029800Z

something like (reduce (fn [coll' pred] (filter pred coll')) coll pred-list)

maleghast 2020-08-20T15:22:46.031500Z

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 πŸ™‚

maleghast 2020-08-20T15:46:45.032200Z

@mccraigmccraig - Worked, first time - thanks SO MUCH!

maleghast 2020-08-20T15:47:08.032800Z

@alex.lynham - Thanks also for engaging and helping me realise I was not explaining well

mccraigmccraig 2020-08-20T15:48:02.033400Z

yw