<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged map</title>
<link>https://ask.clojure.org/index.php/tag/map</link>
<description></description>
<item>
<title>Transducers and Maps</title>
<link>https://ask.clojure.org/index.php/8654/transducers-and-maps</link>
<description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I'm learning more about transducers, but coming up with some deadends. &lt;/p&gt;
&lt;p&gt;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 :-)&lt;/p&gt;
&lt;p&gt;Let' say I have a data stucture thus:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; (def trip {:tripData 
            {:segments [{:dataPoints 
                         [{:location {:lat 1 :lng 2}} 
                          {:location {:lat 3 :lng 4}}]}]}})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There could be hundreds/thousands of dataPoints with each dataPoint having a single location.&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; (def xf
   (comp
    (mapcat :dataPoints)
    (map :location)
    (map (fn [{lat :lat lng :lng}] (str lat &quot; &quot; lng)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;then evaluated via:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def lat-lng (into [] xf (-&amp;gt;&amp;gt; trip :tripData :segments)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And I get back something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[&quot;1 2&quot; &quot;3 4&quot;]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which I can then do (for the purposes of my exercise), this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; (clojure.string/join &quot;, &quot; lat-lng)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;to obtain, finally, this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;1 2, 3 4&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which is all fine and dandy :-)&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;I also discovered, I can do this too, without the use of transducers:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; (def lat-lng-2 (-&amp;gt;&amp;gt; trip
                     :tripData
                     :segments
                     (mapcat :dataPoints)
                     (map :location)
                     (map (fn [{lat :lat lng :lng}] (str lat &quot; &quot; lng)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which, given the clojure.string/join, ends up with the same result.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;I'm at a loss on how to make this efficent/better whilst learning how I can use transducers.&lt;/p&gt;
&lt;p&gt;I would appreciate some help/guidance/feedback!&lt;/p&gt;
&lt;p&gt;Thank you.&lt;/p&gt;
</description>
<category>Transducers</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8654/transducers-and-maps</guid>
<pubDate>Tue, 24 Sep 2019 20:14:24 +0000</pubDate>
</item>
</channel>
</rss>