Fork me on GitHub
#code-reviews
<
2019-09-10
>
dharrigan12:09:50

Is there a way to do this better?...

dharrigan12:09:38

(def xform
  (comp
   (map #(let [{:keys [id location geohash frequency]} %]
           {:id id :location (m/pgobject-location->latlng location) :geohash geohash :frequency frequency}))))

dharrigan12:09:52

given that I need to convert the location into a different format

delaguardo13:09:49

Assuming you will use xform in transducer context so you can chain map calls

(def xform
  (comp
   (map #(update % :location m/pgobject-location->latlng))
   (map #(select-keys % [:id :location :geohash :frequency]))))

๐Ÿ‘ 4
dominicm13:09:05

You want the update function, no?

dharrigan13:09:14

This is what confused me still with transducers, how to read the order, i.e., it looks like in that comp form above, stuff is being applied bottom up....(whereas, the natural way of looking at pipelines is top to bottom)...a bit confusing...

dharrigan13:09:09

Thank you! (and for the rewrite!) ๐Ÿ™‚ It's good to learn! ๐Ÿ™‚

delaguardo13:09:45

you are welcome)

delaguardo13:09:10

And if you donโ€™t need to have maps with listed keys but only with updated location: (map #(update % :location ....)) will give you a transducer ) You can check on mapโ€™s documentation - look for note about one-arity version of map

jaihindhreddy22:09:39

there's actually a subtle difference, the old version always has all four keys in the output with absent values as nil, whereas delaguardo's version omits absent keys, because of select-keys

dominicm13:09:48

We were discussing this yesterday in JUXT

dominicm13:09:12

I think Tim Baldridge's video on this is good

dharrigan13:09:30

link me! ๐Ÿ™‚