Fork me on GitHub
#clojure-uk
<
2020-04-29
>
dharrigan05:04:44

Good Morning!

thomas07:04:18

mogge 😼

guy08:04:23

Morning!

dharrigan10:04:59

I'm having a bit of an hard time trying to massage some data into another structure, can anyone help? I'm open to using clojure or specter or whatever to get the job done πŸ™‚ Here's the original structure...

dharrigan10:04:08

{"Foobar Motor Group" [{:poi_name "Foobar Motor Group",
                        :poi_type "GARAGE",
                        :trip_location #object[org.postgresql.util.PGobject
                                               "0xbda6575"
                                               "44A40"],
                        :trip_timestamp #inst "2020-02-03T10:41:50"}
                       {:poi_name "Foobar Motor Group",
                        :poi_type "GARAGE",
                        :trip_location #object[org.postgresql.util.PGobject
                                               "0x258b4c14"
                                               "44A40"],
                        :trip_timestamp #inst "2020-02-03T10:08:19"}
                       {:poi_name "Foobar Motor Group",
                        :poi_type "GARAGE",
                        :trip_location #object[org.postgresql.util.PGobject
                                               "0xed60b3b"
                                               "44A40"],
                        :trip_timestamp #inst "2020-02-03T09:40:19"}]}                        

dharrigan10:04:15

and this is what I want to end up with:

dharrigan10:04:24

{"Foobar Motor Group" [{:poi_name "Foobar Motor Group",
                        :poi_type "GARAGE",
                        :trips [{:trip_location #object......,
                                 :trip_timestamp #inst......}
                                {:trip_location #object......,
                                 :trip_timestamp #inst......}]}]}

alexlynham11:04:15

in this example, all the things in the vector are garages, right? if they're unique, any reason why it isn't a hash rather than a vec?

dharrigan12:04:12

It's just an example, there are multitude of other types in the result set returned

dharrigan12:04:24

i.e., poi_type "ROUNDABOUT" etc...

guy12:04:24

But the :poi_name is the same right?

guy12:04:34

it looks like uve done a group-by on :poi_name

guy12:04:17

So then u want to merge together all the maps with the same name type and then add in the trips basically in a vec?

dharrigan12:04:17

yes, in effect deduping the poi's into one and then adding the trips associated with the poi into a vec

dharrigan12:04:33

I was playing with group-by to give me a start to see what I can achieve.

dharrigan12:04:49

Then i went down a rabbit hole of map, reduce-kv, etc..

Ben Hammond12:04:04

group-by would be the obvious choice

Ben Hammond12:04:27

with a couple of transforms either side

Ben Hammond12:04:47

β€’ transform to flattten data structure β€’ group-by β€’ transform group-by output to what you want

πŸ‘ 4
Ben Hammond12:04:09

but that keep the individual steps distinct and obvious

Ben Hammond12:04:29

don't try to do too much in one function - makes it difficult to maintain

Ben Hammond12:04:05

but keep it really explicit what is the 'group-by' key

Ben Hammond12:04:10

and what is the 'group-by' data

dharrigan12:04:48

I did (group-by :poi_name results) where results is similar to the example I gave at the start πŸ™‚

dharrigan12:04:57

Just to get things rolling in my head, mind πŸ™‚

Ben Hammond12:04:08

so then you have a simple (medley/map-vals transform to do to get thing in the shape thatt you want

dharrigan12:04:28

Reading up on map-vals

Ben Hammond12:04:48

nah its just a

(mapv #(select-keys % [:trip_location :trip_timestamp) ...) 
innitt

dharrigan12:04:30

trying that out πŸ™‚

guy12:04:41

Yeah i thought that at first too @U793EL04V haha

dharrigan13:04:58

it produces the shape I'm interested in

dharrigan13:04:10

it's probably immature and efficient, so would welcome improvements πŸ™‚

Ben Hammond14:04:01

usually the most important question is > how easy is it to maintain

Ben Hammond14:04:56

How quickly would the 6-months-in-the-future you understand it

dharrigan14:04:39

Probably well enough, and hopefully more experienced enough to say "that could be rewritten better!" πŸ™‚

alexlynham14:04:56

I think that snippet looks okay, I'd just format it so it's easier to visually parse and maybe rename a couple of the fns to be really explicit, but then my fn names are usually on the long side

dharrigan14:04:02

Thank you! πŸ™‚

dharrigan10:04:47

It's basically collapsing all the trips into one collection, under one "poi"

guy10:04:59

sorta smells like group by i dnno

guy10:04:04

let me take it to the repl

guy10:04:54

nvm its not that haha I've got a meeting now, ill post what ive got later after it

jasonbell10:04:28

Morning friends

πŸ‘‹ 8
alexlynham11:04:15

in this example, all the things in the vector are garages, right? if they're unique, any reason why it isn't a hash rather than a vec?

folcon11:04:02

Morn’

rhinocratic11:04:59

Cabin fever is definitely starting to get the better of me. I started a blog... https://blog.nerdwick.net/

πŸ‘€ 4
folcon13:04:41

Well that was just lovely =)…

rhinocratic14:04:48

Why thank you! πŸ™‚

4
Eamonn Sullivan19:04:13

That's a really nice looking site! I did the same thing (https://eamonnsullivan.co.uk/), out of pure boredom. Mine looks like something out of the early noughts...

πŸ‘ 8
rhinocratic19:04:03

Thanks! Yours looks great - nice and clean, and with far more content than mine! Early days though... Subscribed!

folcon20:04:29

snap @UR71VR71S, I too used the basic cryogen template ;)… http://folcon.github.io/

Conor12:04:47

Flann O'Brien, I approve

rhinocratic12:04:01

Thank you! My all-time favourite author.