<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: John Neiberger</title>
    <description>The latest articles on DEV Community by John Neiberger (@neiby).</description>
    <link>https://dev.to/neiby</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F592181%2F70fa5033-60db-4eb6-8d4e-8e561105ec63.jpg</url>
      <title>DEV Community: John Neiberger</title>
      <link>https://dev.to/neiby</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/neiby"/>
    <language>en</language>
    <item>
      <title>A Little Elixir for Pythonistas</title>
      <dc:creator>John Neiberger</dc:creator>
      <pubDate>Sun, 06 Nov 2022 22:14:18 +0000</pubDate>
      <link>https://dev.to/neiby/a-little-elixir-for-pythonistas-27ca</link>
      <guid>https://dev.to/neiby/a-little-elixir-for-pythonistas-27ca</guid>
      <description>&lt;p&gt;I was reading Elixir in Action by Saša Jurić and came across an example that, with a little modification, demonstrated some useful similarities between Elixir and Python, which might be helpful for another Elixir noobs like me who know a little Python. This straightforward example takes a list of names and prints them with an index. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8N8ITslw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lgm622cz1w3wct0ug99h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8N8ITslw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lgm622cz1w3wct0ug99h.png" alt="Elixir list comprehension example" width="880" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It begins with a list construction that looks exactly like what you'd see in Python. The following line is an Elixir list comprehension. It begins with &lt;code&gt;for&lt;/code&gt; but otherwise is similar to list comprehensions in Python. You'd read it in English as: "For each {name, index} pair in Enum.with_index(employees), print the formatted result."&lt;/p&gt;

&lt;p&gt;&lt;code&gt;IO.puts()&lt;/code&gt; is similar to &lt;code&gt;print()&lt;/code&gt;, and the &lt;code&gt;"#{variable}"&lt;/code&gt; syntax is quite similar to Python's f-strings.&lt;/p&gt;

&lt;p&gt;This list comprehension is iterating over &lt;code&gt;Enum.with_index(employees)&lt;/code&gt;, which is analogous to Python's &lt;code&gt;enumerate()&lt;/code&gt; in that it returns a tuple of &lt;code&gt;{value, index}&lt;/code&gt; for each element of the iterable, although in Elixir the value comes first, then the index.&lt;/p&gt;

&lt;p&gt;In Python, the final list comprehension might look like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[print(f"{idx}.  {value}") for idx,value in enumerate(employees)]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I'll leave it to you to decide if either is more clear than the other. My experience so far is that Elixir comprehensions are more flexible and powerful. &lt;/p&gt;

&lt;p&gt;For example, if your comprehension generates tuples, you can use the syntax &lt;code&gt;into: %{}&lt;/code&gt; to build a map (a dictionary in Python) where the first element of the tuple is the key and the second element is the value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9J3YxN9m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xdts1dpomyt71eb66qg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9J3YxN9m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xdts1dpomyt71eb66qg.png" alt="Elixir map example" width="880" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As with dictionaries in Python, you can get the value of a map entry by key using the &lt;code&gt;map_name[key]&lt;/code&gt; syntax. &lt;/p&gt;

&lt;p&gt;Granted, you could do the same thing in Python using a dictionary comprehension:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;employee_dict = {key:value for (key, value) in enumerate(employees)}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It's really been fun learning Elixir. I may pass along more tips and tidbits down the road.&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
