<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Adam Cohn on Medium]]></title>
        <description><![CDATA[Stories by Adam Cohn on Medium]]></description>
        <link>https://medium.com/@adamcohn_78119?source=rss-34168d7cf2bc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ZIafNjCnlFc-YjQS.jpg</url>
            <title>Stories by Adam Cohn on Medium</title>
            <link>https://medium.com/@adamcohn_78119?source=rss-34168d7cf2bc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 06:24:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adamcohn_78119/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[CSV Parsing in Ruby/Rails]]></title>
            <link>https://medium.com/@adamcohn_78119/csv-parsing-in-ruby-rails-538c32cc2499?source=rss-34168d7cf2bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/538c32cc2499</guid>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Adam Cohn]]></dc:creator>
            <pubDate>Thu, 19 Jul 2018 17:59:20 GMT</pubDate>
            <atom:updated>2018-07-19T17:59:20.782Z</atom:updated>
            <content:encoded><![CDATA[<p>Is your CSV file bringing Excel to its knees? Need to seed your data from a spreadsheet or export a CSV from your database? Ruby offers a built-in CSV library that makes it easy to work with spreadsheets.</p><h3>Writing to your database</h3><p>The CSV library allows for the conversion of a spreadsheet to to an array of hashes, perfect for iterating and seeding. To get started, be sure to require ‘csv’ at the top of your file.</p><p>Next, use the library’s .open() method to load the data in Ruby and map it to a hash:</p><p>data = CSV.open(“**file**.csv”, headers: :first_row).map(&amp;:to_h)</p><p>The above converts a CSV from:</p><pre>+---------+----------+-------+-------+<br>|  name   |  county  | party | total |<br>+---------+----------+-------+-------+<br>| Clinton | Denver   | D     |   500 |<br>| Trump   | Denver   | R     |   200 |<br>+---------+----------+-------+-------+</pre><p>to:</p><pre>[{name: ‘Clinton’, county: ‘Denver’, party: ‘D’, total: 500},</pre><pre>{name: ‘Trump’ , county: ‘Denver’, party: ‘D’, total: 200} ]</pre><p>From there, you can create a method to iterate through the hashes and add them to your database:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffae4b7c57847dcb2cd2d2477b0a8bfd/href">https://medium.com/media/ffae4b7c57847dcb2cd2d2477b0a8bfd/href</a></iframe><h3>What about exports?</h3><p>Exports work similarly (but in reverse). You’ll need to create an array of hashes from your database, where the keys represent the CSV’s columns . We can use the same CSV.open() method to create a file by specifying a file name that does not exist (additionally, you can add to or edit a file by specifying an existing filename).</p><p>Here’s an example where Denver’s election results are exported from the database by candidate.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f9082b42b3173c9b76a37e98485b4e4/href">https://medium.com/media/1f9082b42b3173c9b76a37e98485b4e4/href</a></iframe><p>Let’s break it down:</p><ol><li>denver_data extracts the results in Denver County from our database</li><li>results aggregates the totals by candidate</li><li>to_export maps the results, and candidate/county data to an array of hashes</li><li>The array is then added to the CSV; first one set of keys (column names) and then the values (rows)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*G89jlPlPBCLgErThHAy0vQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=538c32cc2499" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Make an Interactive Map in 5 Minutes Using QGIS and MapBox]]></title>
            <link>https://medium.com/@adamcohn_78119/make-an-interactive-map-in-5-minutes-using-qgis-and-mapbox-b0b4cbf736af?source=rss-34168d7cf2bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0b4cbf736af</guid>
            <category><![CDATA[mapbox]]></category>
            <dc:creator><![CDATA[Adam Cohn]]></dc:creator>
            <pubDate>Thu, 19 Jul 2018 17:59:05 GMT</pubDate>
            <atom:updated>2018-07-19T17:59:05.008Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>As they huddle around the desk, Trump likes to pore over visuals — maps, charts, pictures and videos, as well as “killer graphics,” as CIA Director Mike Pompeo phrased it. (<a href="https://www.washingtonpost.com/politics/how-president-trump-consumes--or-does-not-consume--top-secret-intelligence/2017/05/29/1caaca3e-39ae-11e7-a058-ddbb23c75d82_story.html?utm_term=.d00a3cd4761a">Washington Post</a> — May 29, 2017)</blockquote><p>Maps are an effective and compelling way displaying information. While making them appears complicated (and can be), tools like MapBox and QGIS can let us render beautiful and interactive maps in just minutes. Here’s how to get started.</p><p><strong>Grab some data and load it into QGIS.</strong> In this example, we’ll use a shapefile of Washington, D.C. tree <a href="http://opendata.dc.gov/datasets/tree-inspection-in-the-last-30-days">inspections</a> over the last 30 days. (Shapefiles are a common format for storing geographic data; read more about them <a href="http://desktop.arcgis.com/en/arcmap/10.3/manage-data/shapefiles/what-is-a-shapefile.htm">here</a>.) Now fire up QGIS and import your shapefile by selecting the “Add Vector Layer” option:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*fuqCQjfr5FSecWFBBBclCQ.png" /></figure><p><strong>Convert the shapefile to GeoJSON</strong>. Web mapping tools like Leaflet and Mapbox easily read GeoJSON (a detailed <a href="https://macwright.org/2015/03/23/geojson-second-bite.html">overview</a> of the format is available here). In just a few clicks, QGIS lets us convert a shapefile to GeoJSON. Right click on your layer and select “Save As…:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*fSoHXuWGeXdJzcHZxB9NHg.png" /></figure><p>Next, give your file a name and select GeoJSON as your format:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*bd_m3RP-EwCm-M7yPT-0Rg.png" /></figure><p><strong>Upload to Mapbox and convert to Tileset</strong>. Mapbox Studio makes it especially easy to work with GeoJSONs. Create an account and upload the file as a “Dataset.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*yET3tYRQcptRju_9WZgLjA.png" /></figure><p>Success! Now, if necessary, the data can be edited. When you’re ready to make your map, click export in the top left and export the coordinates to tiles.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*-KXze0Rj8bNb6jJ3ds58ng.png" /></figure><p><strong>Create a style and link your tiles.</strong> Head back to studio and create a new style. We’ll use the pre-made “Light” template for this example. In the top-left, select “Add Layer” and select the trees Tileset. You can style the layer without code using the Studio interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*bXdGaCnP-aeCzwZpoAR8lg.png" /></figure><p><strong>Make your map in Mapbox GL JS.</strong> Mapbox is well-<a href="https://www.mapbox.com/mapbox-gl-js/example/simple-map/">documented</a>, so I won’t rehash the whole process of initializing a map, but here’s the gist:</p><p>Create an HTML file and make sure it includes a div with an id of map.</p><p>In a JavaScript file, declare your map as a variable, specify its container (our map div), and starting position.</p><p>Set the style to the map you just created. Don’t forget your API key!</p><pre>let map = new mapboxgl.Map({</pre><pre>container: ‘map’,</pre><pre>style: ‘mapbox://styles/adamcohn/cjiopq1b92cn22rpi4ar2b4al’,</pre><pre>center: [-96, 37.8],</pre><pre>zoom: 3</pre><pre>});</pre><p>Viola!</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*ZC9mLqcKeaQ28cCqtKl56g.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0b4cbf736af" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recreating Ruby Features in Javascript]]></title>
            <link>https://medium.com/@adamcohn_78119/recreating-ruby-features-in-javascript-383a024348a7?source=rss-34168d7cf2bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/383a024348a7</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Adam Cohn]]></dc:creator>
            <pubDate>Thu, 19 Jul 2018 17:58:43 GMT</pubDate>
            <atom:updated>2018-07-19T17:58:43.068Z</atom:updated>
            <content:encoded><![CDATA[<p>Switching between Ruby and Javascript can be challenging at first. Several native features of Ruby are not available right away in Javascript, but here are implementations of three public Ruby methods to ease the transition:</p><h4>1. Compact</h4><p>In Ruby, the array method .compact filters falsy values from an array. For example:</p><pre>[&quot;apple&quot;, &quot;banana&quot;, nil].compact</pre><pre># =&gt; [&quot;apple&quot;, &quot;banana&quot;]</pre><p>Javascript does not have a comparable feature but we can accomplish the same with a simple filter:</p><pre>[&quot;apple&quot;, &quot;banana&quot;, false].filter(Boolean)</pre><pre>// =&gt; [&quot;apple&quot;, &quot;banana&quot;]</pre><p>How does this work? Javascript allows for the passage of parameters to the Boolean object, which returns true or false, perfect for a filter. <em>Warning: Ruby and Javascript have different definitions of truthiness. For example, both 0 and an empty string are falsy in Javascript, but truthy in Ruby.</em></p><h4>2. Counting with Hashes</h4><p>Javascript objects and Ruby hashes are similar, allowing you to store key value pairs — a great tool for counting. In Ruby, you can give a hash a default value (like 0), helpful for incrementing. For example:</p><pre>counter = Hash.new(0)<br>array = [&quot;apple&quot;, &quot;banana&quot;, &quot;banana&quot;, &quot;orange&quot;]</pre><pre>array.each { |el| counter[el] += 1 }</pre><pre>counter</pre><pre># =&gt; { &quot;apple&quot;=&gt;1, &quot;banana&quot;=&gt;2, &quot;orange&quot;=&gt;1 }</pre><p>Javascript doesn’t allow you give an object a default value, but you can still use objects to count. For example:</p><pre>const counter = {};<br>const array = [&quot;apple&quot;, &quot;banana&quot;, &quot;banana&quot;, &quot;orange&quot;];</pre><pre>array.forEach((el) =&gt; {<br>	counter[el] ? counter[el] += 1 : counter[el] = 1;<br>});</pre><pre>counter<br>// =&gt; {apple: 1, banana: 2, orange: 1}</pre><h4>3. Uniq</h4><p>Ruby allows you to simply filter duplicate values from an array using .uniq. For example:</p><pre>[&quot;apple&quot;, &quot;banana&quot;, &quot;banana&quot;, &quot;orange&quot;].uniq</pre><pre># =&gt; [&quot;apple&quot;, &quot;banana&quot;, &quot;orange&quot;]</pre><p>This is not so easy in Javascript, but it can be accomplished using .filterwith a callback. Here’s an implementation:</p><pre>[&#39;apple&#39;, &#39;banana&#39;, &#39;banana&#39;, &#39;orange&#39;].filter((element, index, array) =&gt; array.indexOf(element) === array.lastIndexOf(element));</pre><pre>// =&gt; [&quot;apple&quot;, &quot;orange&quot;]</pre><p>Let’s break it down. Javascript’s filter can accept three arguments: each element of an array, that element’s index, and the array itself. By checking if the index of an element is the same as its last index, you can see if an element is only listed once. If this isn’t the case, the element is removed from an array.</p><p>In the above example, the first banana has an indexOf 1, but a lastIndexOf2, so it is removed from the array. The second banana then has an indexOf 1 and a lastIndexOf 1, so it is left untouched.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=383a024348a7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>