Hi,
I'm learning more about transducers, but coming up with some deadends.
What I'm looking to understand, is whether they are appropriate to use (in my use-case, but also as a learning exercise) and whether what I am doing is right/efficient/maybe-there-is-a-better-way :-)
Let' say I have a data stucture thus:
(def trip {:tripData
{:segments [{:dataPoints
[{:location {:lat 1 :lng 2}}
{:location {:lat 3 :lng 4}}]}]}})
There could be hundreds/thousands of dataPoints with each dataPoint having a single location.
I want to efficiently extract out only the lat and lng into a single collection and turn it into a string. I came up with this:
(def xf
(comp
(mapcat :dataPoints)
(map :location)
(map (fn [{lat :lat lng :lng}] (str lat " " lng)))))
then evaluated via:
(def lat-lng (into [] xf (->> trip :tripData :segments)))
And I get back something like this:
["1 2" "3 4"]
Which I can then do (for the purposes of my exercise), this:
(clojure.string/join ", " lat-lng)
to obtain, finally, this:
"1 2, 3 4"
Which is all fine and dandy :-)
However, given my inexperience with transducers, I'm left wondering if there is a different/better way. For example, a way, within the comp xf, to turn the data into the string and joined at the end instead of using clojure.string/join.
I also discovered, I can do this too, without the use of transducers:
(def lat-lng-2 (->> trip
:tripData
:segments
(mapcat :dataPoints)
(map :location)
(map (fn [{lat :lat lng :lng}] (str lat " " lng)))))
Which, given the clojure.string/join, ends up with the same result.
However, it's my understanding that you can't use a map keyword (i.e., :tripData, :segments) as part of a comp as keywords are not transducers.
I'm at a loss on how to make this efficent/better whilst learning how I can use transducers.
I would appreciate some help/guidance/feedback!
Thank you.