<?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: Adam Davis</title>
    <description>The latest articles on DEV Community by Adam Davis (@brewinstallbuzzwords).</description>
    <link>https://dev.to/brewinstallbuzzwords</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%2F269662%2F2f3fa706-59fc-47b0-8bab-dc8a7aa3332c.png</url>
      <title>DEV Community: Adam Davis</title>
      <link>https://dev.to/brewinstallbuzzwords</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brewinstallbuzzwords"/>
    <language>en</language>
    <item>
      <title>How to Use Head and Tail in Elixir</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Wed, 25 Jan 2023 13:30:00 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/how-to-use-head-and-tail-in-elixir-1dl2</link>
      <guid>https://dev.to/brewinstallbuzzwords/how-to-use-head-and-tail-in-elixir-1dl2</guid>
      <description>&lt;p&gt;Head and tail are a common programming concept used to represent a  &lt;a href="https://en.wikipedia.org/wiki/Linked_list" rel="noopener noreferrer"&gt;linked list&lt;/a&gt;. While not unique to Elixir lang, it is crucial for understanding the language’s representation of lists.&lt;/p&gt;

&lt;h2&gt;
  
  
  What do head and tail refer to?
&lt;/h2&gt;

&lt;p&gt;The head of a list is the first element, and the tail is the list containing all the remaining elements.&lt;/p&gt;

&lt;p&gt;For example in the list &lt;code&gt;[1, 2, 3, 4]&lt;/code&gt;, the head would be &lt;code&gt;1&lt;/code&gt; and the tail would be the list &lt;code&gt;[2, 3, 4]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgs2xnpajj3cco9nuglds.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgs2xnpajj3cco9nuglds.png" alt="Head and tail of a linked list"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Head and tail are a recursive representation
&lt;/h3&gt;

&lt;p&gt;If you noticed in the example above, we started with a list, but the tail of that list is also a list that has its own head and tail. This is also true of the tail’s tail, and so on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frk8m3t6pbowxgltefs2k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frk8m3t6pbowxgltefs2k.png" alt="Recursive definition of head of tail"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What are the head and tail of a list with only one element?
&lt;/h3&gt;

&lt;p&gt;The head of a single element list would be the only element in the list, and the tail would be an empty list.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are the head and tail of an empty list?
&lt;/h3&gt;

&lt;p&gt;An empty list does not have a head or tail.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I get the head and tail of a list in Elixir?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Method 1: hd and tl
&lt;/h3&gt;

&lt;p&gt;The simplest way to get the head or tail in Elixir is with &lt;code&gt;hd/1&lt;/code&gt; or &lt;code&gt;tl/1&lt;/code&gt; respectively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hd&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tl&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we call &lt;code&gt;hd/1&lt;/code&gt; or &lt;code&gt;tl/1&lt;/code&gt; with an empty list, you’ll be given an &lt;code&gt;ArgumentError&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hd&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ArgumentError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt; &lt;span class="n"&gt;were&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;given&lt;/span&gt; &lt;span class="ss"&gt;arguments:&lt;/span&gt;

  &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="err"&gt;1&lt;/span&gt;&lt;span class="n"&gt;st&lt;/span&gt; &lt;span class="ss"&gt;argument:&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;nonempty&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;

    &lt;span class="ss"&gt;:erlang&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hd&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;
&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tl&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ArgumentError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt; &lt;span class="n"&gt;were&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;given&lt;/span&gt; &lt;span class="ss"&gt;arguments:&lt;/span&gt;

  &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="err"&gt;1&lt;/span&gt;&lt;span class="n"&gt;st&lt;/span&gt; &lt;span class="ss"&gt;argument:&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;nonempty&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;

    &lt;span class="ss"&gt;:erlang&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tl&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Method 2: Pattern matching
&lt;/h3&gt;

&lt;p&gt;To get both head and tail at once, we can use the &lt;code&gt;|&lt;/code&gt; operator for pattern matching:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if we only need one of the values, we can still use pattern matching. Just use an underscore to ignore the other value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we try to pattern match head and tail on an empty list, we’ll be given a &lt;code&gt;MatchError&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MatchError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="n"&gt;hand&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="ss"&gt;value:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to pattern match with head and tail in an Elixir function header
&lt;/h2&gt;

&lt;p&gt;The pattern matching shown above can also be used in a function header to verify that a list is non-empty, while also extracting whichever values you need.&lt;/p&gt;

&lt;p&gt;We can test this out by making a function that pattern matches on head and tail, and an overloaded function of the same name that accepts any argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;pattern_match_test&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;_head&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;_tail&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="s2"&gt;"pattern match succeeded"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;pattern_match_test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="s2"&gt;"pattern match failed"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that if we call &lt;code&gt;pattern_match_test/1&lt;/code&gt; with a non-empty list we get the result from the pattern match function, but other types of input will fail the pattern match.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pattern_match_test&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="s2"&gt;"pattern match succeeded"&lt;/span&gt;

&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pattern_match_test&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;
&lt;span class="s2"&gt;"pattern match failed"&lt;/span&gt;

&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pattern_match_test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"asdf"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="s2"&gt;"pattern match failed"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to use head and tail to add an element to a list in Elixir
&lt;/h2&gt;

&lt;p&gt;Elixir’s head and tail syntax can also be used to add an element to a list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above example shows how we can prepend an element to a list. However, trying to append an element to the end can produce some unexpected results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because the syntax only really supports prepending a head element instead of appending a tail. This is to encourage more efficient operations, because with a linked list is is much faster to add an element to the front of the list instead of the end.&lt;/p&gt;

&lt;p&gt;In order to prepend an element, the new node needs to be created and then pointed at the existing list, which becomes the tail.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F12vrnked0h1j44r3k8fr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F12vrnked0h1j44r3k8fr.png" alt="Add element to front of linked list"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, to add an element to the end of a list, we must traverse every element and then point the existing last node of the list to the new element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvo9pkctkwp95kr3mtn3w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvo9pkctkwp95kr3mtn3w.png" alt="Add element to the end of a linked list"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It likely wouldn’t cause any issues to add an element to the end of a list once or twice, but if we had a large operation where we appended elements thousands of times the inefficiency would compound.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real world example: Let’s implement filter!
&lt;/h2&gt;

&lt;p&gt;To put everything together that we’ve learned in this post, we’re going to create an implementation of &lt;code&gt;filter/2&lt;/code&gt; using head and tail.&lt;/p&gt;

&lt;p&gt;As with most recursive functions, I like to start with defining a base case. We’ll continue the recursive calls as long as the head is not nil. Once it is nil, we’ll return the reverse order of the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;my_filter&lt;/span&gt; &lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;when&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="c1"&gt;# TODO: implement&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we need to define what happens in the recursive case when there is an element in the head of the list. If the function &lt;code&gt;f&lt;/code&gt; produces a value of &lt;code&gt;true&lt;/code&gt; when given the head of the list, when it should be pushed onto the accumulator. Otherwise, we should keep the accumulator as-is. In both cases, we need to make a recursive call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;when&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we need to define the user interface. Because the user doesn’t need to know about the existence of the accumulator, we make a separate function header that does not include the ability to set the accumulator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To provide a neater interface, we should make every function private except the one we want the user to call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;when&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s test it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_filter&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  It works!
&lt;/h2&gt;

&lt;p&gt;Now that you’ve read this post and implemented &lt;code&gt;filter&lt;/code&gt;, you should have a firm grasp of how to use head and tail in Elixir.&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>functional</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How do you feel about cover letters?</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Wed, 12 Oct 2022 14:45:45 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/how-do-you-feel-about-cover-letters-1ooj</link>
      <guid>https://dev.to/brewinstallbuzzwords/how-do-you-feel-about-cover-letters-1ooj</guid>
      <description>&lt;p&gt;How do you feel about job applications that ask for a cover letter? Do you skip them if they’re not required?&lt;/p&gt;

&lt;p&gt;It seems to me that requiring a cover letter could create an adverse incentive where more qualified employees who have more options would not want to spend the time to write one when there’s other applications that don’t have that same requirement.&lt;/p&gt;

&lt;p&gt;Do you personally write cover letters when an application requires one? Or would that requirement make you pass over that role?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>watercooler</category>
      <category>career</category>
    </item>
    <item>
      <title>How to include static assets in an Elixir application</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Wed, 11 May 2022 11:20:19 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/how-to-include-static-assets-in-an-elixir-application-2acc</link>
      <guid>https://dev.to/brewinstallbuzzwords/how-to-include-static-assets-in-an-elixir-application-2acc</guid>
      <description>&lt;h2&gt;
  
  
  tl;dr
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Put your static assets in the &lt;code&gt;/priv&lt;/code&gt; directory.&lt;/li&gt;
&lt;li&gt;Identify the atom for your app in &lt;code&gt;mix.exs&lt;/code&gt;. In this example, we’ll use &lt;code&gt;:my_app&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Get the file path by using &lt;code&gt;Application.app_dir/2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Read the file according to your project’s needs. One way is with &lt;code&gt;File.stream!/3&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;get_my_file_stream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;app_dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:my_app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/priv/my_file.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stream!&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Diving deeper
&lt;/h2&gt;

&lt;p&gt;If the above info solved your problem, great! You can go ahead and close this page.&lt;/p&gt;

&lt;p&gt;But if you want to learn more about this topic, keep reading.&lt;/p&gt;

&lt;h3&gt;
  
  
  The legacy of OTP
&lt;/h3&gt;

&lt;p&gt;The Elixir programming language is built on top of Erlang/OTP. If you don’t know about Erlang/OTP, check out &lt;a href="https://www.erlang.org/faq/introduction.html"&gt;this page from the Erlang FAQ&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Within Erlang/OTP applications, there is a standard directory structure, which looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;─ ${application}
    ├── doc
    │   ├── internal
    │   ├── examples
    │   └── src
    ├── include
    ├── priv
    ├── src
    │   └── ${application}.app.src
    └── test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The proper directory for storing static assets in these applications is &lt;code&gt;/priv&lt;/code&gt;, and Elixir inherits this structure.&lt;/p&gt;

&lt;p&gt;If you’d like to see more details on Erlang/OTP applications, &lt;a href="https://www.erlang.org/doc/design_principles/applications.html#directory-structure-guidelines-for-a-development-environment"&gt;check out this page in the OTP Design Principles User’s Guide&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Leveraging the Erlang :code module
&lt;/h3&gt;

&lt;p&gt;Although we used &lt;code&gt;Application.app_dir&lt;/code&gt; above, that’s not the only way to get the file path.&lt;/p&gt;

&lt;p&gt;Another approach would be to use the Erlang &lt;code&gt;:code&lt;/code&gt; module, which you can call directly from your Elixir code (docs &lt;a href="https://www.erlang.org/doc/man/code.html"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;:code&lt;/code&gt; module has a function called &lt;code&gt;priv_dir/1&lt;/code&gt; which will return the path to the priv directory for the given application (docs &lt;a href="https://www.erlang.org/doc/man/code.html#priv_dir-1"&gt;here&lt;/a&gt;). Then, you can get the complete path for your file using &lt;code&gt;Path.join/2&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;get_my_file_stream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="ss"&gt;:code&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prive_dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:my_app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Path&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"my_file.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stream!&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  More content
&lt;/h2&gt;

&lt;p&gt;If you liked this, you might also like some of my other posts. If you want to be notified of my new posts, &lt;a href="https://dev.to/brewinstallbuzzwords"&gt;follow me on Dev&lt;/a&gt; or subscribe to my &lt;a href="https://mailchi.mp/89dc077154c7/brewinstallbuzzwords"&gt;brief monthly newsletter&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/elixir-if-block-source-code/"&gt;Reading the Elixir source code to learn how if blocks work&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/my-first-hex-package/"&gt;I published my first Elixir package to hex!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/whats-the-best-question-youve-been-asked-in-a-job-interview-4a1j"&gt;What's the best questions you've been asked in a job interview?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/what-was-the-first-program-you-wrote-1gjp"&gt;What was the first program you wrote?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/js-arrays-are-objects/"&gt;The weird quirk of JavaScript arrays (that you should never use)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/does-elixir-have-for-loops/"&gt;Does Elixir have for loops?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/learn-elixir-with-me/"&gt;Learn Elixir with me!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/bread-ratio-calculator/"&gt;Project Tours: Bread Ratio Calculator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/emoji-skin-tone/"&gt;Changing Emoji Skin Tones Programmatically&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/i-made-my-first-svg-animation-29j3"&gt;I made my first svg animation!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/tips-for-first-npm-package/"&gt;5 tips for publishing your first npm package&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/hugo-beginner-mistakes/"&gt;4 Hugo Beginner Mistakes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>How do different types of meetings affect the way you work?</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Wed, 30 Mar 2022 14:02:48 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/how-do-different-types-of-meetings-affect-the-way-you-work-2k6e</link>
      <guid>https://dev.to/brewinstallbuzzwords/how-do-different-types-of-meetings-affect-the-way-you-work-2k6e</guid>
      <description>&lt;p&gt;As I’ve worked with different teams and companies, I’ve noticed that the way meetings are scheduled can have an impact on my productivity. Here’s some different setups I’ve experienced:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Daily standups&lt;/li&gt;
&lt;li&gt;Weekly standups&lt;/li&gt;
&lt;li&gt;Weekly customer meetings&lt;/li&gt;
&lt;li&gt;As few meetings as possible&lt;/li&gt;
&lt;li&gt;And more!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What approaches to meetings have you experienced? Which have allowed to perform at your best?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>productivity</category>
      <category>watercooler</category>
      <category>career</category>
    </item>
    <item>
      <title>How do you take notes while you code?</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Wed, 23 Mar 2022 13:48:48 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/how-do-you-take-notes-while-you-code-5844</link>
      <guid>https://dev.to/brewinstallbuzzwords/how-do-you-take-notes-while-you-code-5844</guid>
      <description>&lt;p&gt;Do you like to take notes when you're writing code?&lt;/p&gt;

&lt;p&gt;Do you organize them in some way, or do you only keep notes about your current tasks?&lt;/p&gt;

&lt;p&gt;What pieces of information do you think are helpful to keep track of?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>productivity</category>
      <category>watercooler</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What do you wish you knew when you worked you first entry-level tech job?</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Wed, 16 Mar 2022 14:33:07 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/what-do-you-wish-you-knew-when-you-worked-you-first-entry-level-tech-job-3jhd</link>
      <guid>https://dev.to/brewinstallbuzzwords/what-do-you-wish-you-knew-when-you-worked-you-first-entry-level-tech-job-3jhd</guid>
      <description>&lt;p&gt;No matter how prepared (or not) you felt for your first job in tech, there’s inevitably some things you wish you had done differently.&lt;/p&gt;

&lt;p&gt;If you’re in an entry-level job now or hope to be in one soon, what’s something about the industry you want to know more about?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>career</category>
      <category>watercooler</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Diving into the macros that make Phoenix controllers work</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Wed, 16 Mar 2022 14:31:12 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/diving-into-the-macros-that-make-phoenix-controllers-work-4k0h</link>
      <guid>https://dev.to/brewinstallbuzzwords/diving-into-the-macros-that-make-phoenix-controllers-work-4k0h</guid>
      <description>&lt;p&gt;You’ve made a Phoenix controller before, but do you know how it actually works? Let’s explore some code together.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make a new project
&lt;/h2&gt;

&lt;p&gt;First, we’re going to make a new phoenix project. I’m including the &lt;code&gt;--no-ecto&lt;/code&gt; flag because I don’t plan on using any database or changeset functionality in this post.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;mix&lt;/span&gt; &lt;span class="n"&gt;phx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;controller_dissection&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;ecto&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All the code you need to see will be included in the text of this post. But feel free to follow along by making the project on your machine or by following along with &lt;a href="https://github.com/brew-install-buzzwords/controller_dissection"&gt;this GitHub repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The new project should look something like this (not every file is shown in this tree):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── README.md
├── _build
├── assets
├── config
├── deps
├── lib
│   ├── controller_dissection
│   │   ├── application.ex
│   │   └── mailer.ex
│   ├── controller_dissection.ex
│   ├── controller_dissection_web
│   │   ├── controllers
│   │   │   └── page_controller.ex
│   │   ├── endpoint.ex
│   │   ├── gettext.ex
│   │   ├── router.ex
│   │   ├── telemetry.ex
│   │   ├── templates
│   │   │   ├── layout
│   │   │   │   ├── app.html.heex
│   │   │   │   ├── live.html.heex
│   │   │   │   └── root.html.heex
│   │   │   └── page
│   │   │       └── index.html.heex
│   │   └── views
│   │       ├── error_helpers.ex
│   │       ├── error_view.ex
│   │       ├── layout_view.ex
│   │       └── page_view.ex
│   └── controller_dissection_web.ex
├── mix.exs
├── mix.lock
├── priv
└── test

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We haven’t done anything special yet, this is just the default structure for a Phoenix project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Down the rabbit hole
&lt;/h2&gt;

&lt;p&gt;In this post we’re interested in the controllers. To find them, we need to look in &lt;code&gt;lib/controller_dissectino_web/controllers&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You’ll only have one controller by default, and it’s in &lt;code&gt;page_controller.ex&lt;/code&gt;. Here’s what it should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PageController&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:controller&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"index.html"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line near the top should look pretty familiar if you’ve made a Phoenix controller before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:controller&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what does it actually do?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;use/2&lt;/code&gt; is a &lt;a href="https://hexdocs.pm/elixir/Kernel.html#use/2"&gt;macro&lt;/a&gt; that calls the &lt;code&gt;__using__&lt;/code&gt; macro for the given module. Here, the module is &lt;code&gt;ControllerDissectionWeb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we open up &lt;code&gt;lib/controller_dissection_web.ex&lt;/code&gt; then we’ll see the following &lt;code&gt;__using__&lt;/code&gt; macro defined near the bottom of the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="nv"&gt;@doc&lt;/span&gt; &lt;span class="sd"&gt;"""
When used, dispatch to the appropriate controller/view/etc.
"""&lt;/span&gt;
&lt;span class="k"&gt;defmacro&lt;/span&gt; &lt;span class="n"&gt;__using__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;which&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;when&lt;/span&gt; &lt;span class="n"&gt;is_atom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;which&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;__MODULE__&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;which&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;__MODULE__&lt;/code&gt; &lt;a href="https://hexdocs.pm/elixir/Kernel.SpecialForms.html#__MODULE__/0"&gt;special form&lt;/a&gt; provides the atom for the current module, and the &lt;code&gt;which&lt;/code&gt; parameter would be &lt;code&gt;:controller&lt;/code&gt;, because that’s what’s being passed in with the &lt;code&gt;use&lt;/code&gt; call in &lt;code&gt;page_controller.ex&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So all that happens with this use statement is that &lt;code&gt;ControllerDissectionWeb.controller/0&lt;/code&gt; is called.&lt;/p&gt;

&lt;p&gt;If we take a look at &lt;code&gt;ControllerDissectionWeb.controller/0&lt;/code&gt; we’ll see the real meat of the Phoenix controller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Phoenix&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;namespace:&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;

    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;Plug&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Conn&lt;/span&gt;
    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Gettext&lt;/span&gt;
    &lt;span class="n"&gt;alias&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Helpers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as:&lt;/span&gt; &lt;span class="no"&gt;Routes&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function is a macro that will inject code into modules such that they will be able to carry out the defined behavior of Phoenix controllers.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;While I won’t get too into the weeds in this post, I encourage you to follow the definition of &lt;code&gt;Phoenix.Controller&lt;/code&gt; to see the Phoenix functions that get imported into your controllers.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Injecting our own code into controllers
&lt;/h2&gt;

&lt;p&gt;To test that this code is actually injected into our controllers, we can import a module here and see if we can access it within a controller. It’s generally better to put new modules in a new file, but for simplicity let’s just put it in &lt;code&gt;lib/controller_dissection_web.ex&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Within this new module, we’ll make a simple hello world function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;HelloWorld&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;hello_world&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello, world!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let’s add an import to the &lt;code&gt;HelloWorld&lt;/code&gt; module inside the &lt;code&gt;controller/0&lt;/code&gt; macro:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Phoenix&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;namespace:&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;

    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;Plug&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Conn&lt;/span&gt;
    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Gettext&lt;/span&gt;
    &lt;span class="n"&gt;alias&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Helpers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as:&lt;/span&gt; &lt;span class="no"&gt;Routes&lt;/span&gt;

    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;HelloWorld&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test out that it’s available, navigate back to &lt;code&gt;lib/controller_dissection_web/controllers/page_controller.ex&lt;/code&gt; and try calling &lt;code&gt;hello_world/0&lt;/code&gt; within the &lt;code&gt;index/2&lt;/code&gt; function. If successful, the string &lt;code&gt;"hello, world!"&lt;/code&gt; will be printed whenever the index page is loaded.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PageController&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:controller&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;hello_world&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"index.html"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that everything is in place, we’ll run the server with &lt;code&gt;mix phx.server&lt;/code&gt; and open up &lt;code&gt;localhost:4000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If all goes well, you should see this in the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="no"&gt;GET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;world!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What else can we do?
&lt;/h2&gt;

&lt;p&gt;So now that you know how this works, what can you do with this knowledge?&lt;/p&gt;

&lt;p&gt;Here’s a few things that come to mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;common utils library&lt;/li&gt;
&lt;li&gt;action handlers that you want to be present on every controller&lt;/li&gt;
&lt;li&gt;make a more specific type of controller that calls &lt;code&gt;controller/0&lt;/code&gt; but also has additional functionality&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just for fun, let’s see if we can do a trivial implementation of that last option.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extending controller functionality
&lt;/h2&gt;

&lt;p&gt;Luckily, for making our own custom type of controller, there’s some existing code in &lt;code&gt;controller_dissection_web.ex&lt;/code&gt; that we can use as a reference.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;view_helpers/0&lt;/code&gt; function defines some common functionality that is used in different types of views:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Phoenix&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="ss"&gt;root:&lt;/span&gt; &lt;span class="s2"&gt;"lib/controller_dissection_web/templates"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="ss"&gt;namespace:&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;

    &lt;span class="c1"&gt;# Import convenience functions from controllers&lt;/span&gt;
    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;Phoenix&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="ss"&gt;only:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;get_flash:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;get_flash:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;view_module:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;view_template:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="c1"&gt;# Include shared imports and aliases for views&lt;/span&gt;
    &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view_helpers&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;live_view&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Phoenix&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;LiveView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="ss"&gt;layout:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;LayoutView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"live.html"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view_helpers&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# ...&lt;/span&gt;

&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;view_helpers&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="c1"&gt;# Use all HTML functionality (forms, tags, etc)&lt;/span&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Phoenix&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;HTML&lt;/span&gt;

    &lt;span class="c1"&gt;# Import LiveView and .heex helpers (live_render, live_patch, &amp;lt;.form&amp;gt;, etc)&lt;/span&gt;
    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;Phoenix&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;LiveView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Helpers&lt;/span&gt;

    &lt;span class="c1"&gt;# Import basic rendering functionality (render, render_layout, etc)&lt;/span&gt;
    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;Phoenix&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;View&lt;/span&gt;

    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ErrorHelpers&lt;/span&gt;
    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Gettext&lt;/span&gt;
    &lt;span class="n"&gt;alias&lt;/span&gt; &lt;span class="no"&gt;ControllerDissectionWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Helpers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;as:&lt;/span&gt; &lt;span class="no"&gt;Routes&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;view/0&lt;/code&gt; and &lt;code&gt;live_view/0&lt;/code&gt; functions both provide macros that nest the &lt;code&gt;view_helpers/0&lt;/code&gt; macro within them, allowing for multiple types of views that have some shared functionality. If you take a look around that file, you can find a few other functions that also call &lt;code&gt;view_helpers/0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;All that’s needed to make this work is to pass the nested macro into &lt;code&gt;unquote/1&lt;/code&gt; and follow it up with whatever else you want to include in the macro.&lt;/p&gt;

&lt;p&gt;So we can treat &lt;code&gt;controller/0&lt;/code&gt; as the shared code for all controllers, and then make another macro function with a call to &lt;code&gt;controller/0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First, we need to make a new module with the functionality for the new controller type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;CustomControllerUtils&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;flash_hello_world&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_opts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Phoenix&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Controller&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;put_flash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:info&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"hello, world!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we can make the macro function for our custom controller type. All that’s special about our custom controller is that we’ll have a plug that puts a flash message of “hello, world!” This particular functionality is arbitrary and is only here to demonstrate that you could insert any code you like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;custom_controller&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="no"&gt;CustomControllerUtils&lt;/span&gt;

    &lt;span class="n"&gt;plug&lt;/span&gt; &lt;span class="ss"&gt;:flash_hello_world&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NlxJJE-Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnvmn23880bsmizzwiou.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NlxJJE-Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnvmn23880bsmizzwiou.png" alt="Screenshot of hello world flash message" width="880" height="697"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How would you use this?
&lt;/h2&gt;

&lt;p&gt;Have you ever made changes to the Phoenix controller macros? Did this post help you think of a good use case? I’d love to hear about it. Let me know in the comments!&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>functional</category>
      <category>phoenix</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How I built an NYT Spelling Bee solver with Elixir</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Wed, 09 Mar 2022 14:11:41 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/how-i-built-an-nyt-spelling-bee-solver-with-elixir-215h</link>
      <guid>https://dev.to/brewinstallbuzzwords/how-i-built-an-nyt-spelling-bee-solver-with-elixir-215h</guid>
      <description>&lt;p&gt;While Wordle has caught Twitter feeds by storm, there’s something I love about playing the New York Times Spelling Bee game each morning.&lt;/p&gt;

&lt;p&gt;It works by providing a set of letters, and you have to come up with as many words as you can that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only use the provided letters&lt;/li&gt;
&lt;li&gt;Use the center letter at least once&lt;/li&gt;
&lt;li&gt;Are at least 4 characters long&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s what a game might look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Oqst8NTQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9o9i9oxiewlycj60jk6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Oqst8NTQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9o9i9oxiewlycj60jk6.png" alt="Screenshot of a game of NYT Spelling Bee" width="550" height="570"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recently, I took it upon myself to build a solver for this game using Elixir. Most of the code is in this post, but if you'd like to see the whole project you can &lt;a href="https://github.com/brew-install-buzzwords/spelling_bee_solver"&gt;check it out on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining success
&lt;/h2&gt;

&lt;p&gt;Because the words in Spelling Bee are curated and intentionally exclude obscure words, we should expect that this program will suggest words that are not included in the solution.&lt;/p&gt;

&lt;p&gt;So I’ll be judging the success of this program by the percentage of the solution words that are identified, ignoring suggested words that are not part of the official solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting a list of words
&lt;/h2&gt;

&lt;p&gt;My approach was pretty simple. I needed to start with a list of English words, then filter based on the criteria of the game.&lt;/p&gt;

&lt;p&gt;To get a list of English words, I used the &lt;a href="https://www.brewinstallbuzzwords.com/posts/my-first-hex-package/"&gt;word_list package that I created&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting the project
&lt;/h2&gt;

&lt;p&gt;Once I created my new project, I imported my &lt;code&gt;word_list&lt;/code&gt; package by adding it in the &lt;code&gt;deps&lt;/code&gt; array in &lt;code&gt;mix.exs&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;deps&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:word_list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"~&amp;gt; 0.1.0"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, I installed the dependency by running &lt;code&gt;mix deps.get&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test-driven development
&lt;/h2&gt;

&lt;p&gt;Next, I wrote some basic tests so I can define some of the basic functionality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;SpellingBeeSolverTest&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;ExUnit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Case&lt;/span&gt;
  &lt;span class="n"&gt;doctest&lt;/span&gt; &lt;span class="no"&gt;SpellingBeeSolver&lt;/span&gt;

  &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="s2"&gt;"finds some valid words"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;word_stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SpellingBeeSolver&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;solve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"t"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"m"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"y"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"r"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"i"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"f"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"o"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="n"&gt;assert&lt;/span&gt; &lt;span class="s2"&gt;"mortify"&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;word_stream&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt; &lt;span class="s2"&gt;"fifty"&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;word_stream&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="s2"&gt;"all words include center letter"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;word_stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SpellingBeeSolver&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;solve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"t"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"m"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"y"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"r"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"i"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"f"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"o"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="n"&gt;assert&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;all?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word_stream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contains?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"t"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These tests make sure that at least some valid words are found, and that all of the words include the center letter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing the code
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;solve/2&lt;/code&gt; function takes the following approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get the stream of English words&lt;/li&gt;
&lt;li&gt;Apply a filter for length greater than 3, since words must be at least four letters long&lt;/li&gt;
&lt;li&gt;Apply a filter that checks that the word contains the center letter&lt;/li&gt;
&lt;li&gt;Apply a filter that checks that all letters in the word are either on the edge or the center letter&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The following is the contents of the &lt;code&gt;lib/spelling_bee_solver.ex&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;SpellingBeeSolver&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;solve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;edges&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;WordList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getStream!&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contains?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;trim:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;all?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;letter&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;letter&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;edges&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;printSolution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;edges&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;solve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;edges&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To print the solution, the inputs can instead be passed to &lt;code&gt;printSolution/2&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  But does it work?
&lt;/h2&gt;

&lt;p&gt;I tested my program against the solutions of three different days, and my program found all the solutions every time.&lt;/p&gt;

&lt;p&gt;Success! ✅&lt;/p&gt;

&lt;p&gt;There were several extra words found each time, but since the solutions are a curated set of words this is to be expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Room for improvement
&lt;/h2&gt;

&lt;p&gt;While I’m sure there’s more I could do, these are some areas that I think have room for improvement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It would probably be more convenient to pass in a string for the edges instead of an array of strings&lt;/li&gt;
&lt;li&gt;I haven’t done any input validation, so there could be some unexpected behavior if the input isn’t provided in the proper format&lt;/li&gt;
&lt;li&gt;Currently, the program must be run in iex or imported into another Elixir project in order to be functional. It would be more practical to put this logic into a cli tool or a web interface.&lt;/li&gt;
&lt;li&gt;If this were included as part of a larger project, it would be nice to track which words weren’t part of the previous solutions in order to avoid showing them in the future.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  More content
&lt;/h2&gt;

&lt;p&gt;If you liked this, you might also like some of my other posts. If you want to be notified of my new posts, &lt;a href="https://dev.to/brewinstallbuzzwords"&gt;follow me on Dev&lt;/a&gt; or subscribe to my &lt;a href="https://mailchi.mp/89dc077154c7/brewinstallbuzzwords"&gt;brief monthly newsletter&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/elixir-if-block-source-code/"&gt;Reading the Elixir source code to learn how if blocks work&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/my-first-hex-package/"&gt;I published my first Elixir package to hex!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/whats-the-best-question-youve-been-asked-in-a-job-interview-4a1j"&gt;What's the best questions you've been asked in a job interview?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/what-was-the-first-program-you-wrote-1gjp"&gt;What was the first program you wrote?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/js-arrays-are-objects/"&gt;The weird quirk of JavaScript arrays (that you should never use)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/does-elixir-have-for-loops/"&gt;Does Elixir have for loops?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/learn-elixir-with-me/"&gt;Learn Elixir with me!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/bread-ratio-calculator/"&gt;Project Tours: Bread Ratio Calculator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/emoji-skin-tone/"&gt;Changing Emoji Skin Tones Programmatically&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/i-made-my-first-svg-animation-29j3"&gt;I made my first svg animation!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/tips-for-first-npm-package/"&gt;5 tips for publishing your first npm package&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/hugo-beginner-mistakes/"&gt;4 Hugo Beginner Mistakes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>elixir</category>
      <category>functional</category>
      <category>devjournal</category>
      <category>showdev</category>
    </item>
    <item>
      <title>How to import a local module dependency in Elixir</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Thu, 17 Feb 2022 13:27:47 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/how-to-import-a-local-module-dependency-in-elixir-1d7k</link>
      <guid>https://dev.to/brewinstallbuzzwords/how-to-import-a-local-module-dependency-in-elixir-1d7k</guid>
      <description>&lt;p&gt;When you’re in the process of developing a package, it can be useful to be able to import it into another project locally without having to publish it first. By doing this, you can test it within the context of a larger project instead of relying entirely on unit tests.&lt;/p&gt;

&lt;p&gt;This guide will show you how to take an Elixir package you’ve made locally and import it into another project using the file path.&lt;/p&gt;

&lt;h2&gt;
  
  
  Name the dependency
&lt;/h2&gt;

&lt;p&gt;Open the &lt;code&gt;mix.exs&lt;/code&gt; file. Look for the project definition, which should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="ss"&gt;app:&lt;/span&gt; &lt;span class="ss"&gt;:my_dependency&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;version:&lt;/span&gt; &lt;span class="s2"&gt;"0.1.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to update the name of the &lt;code&gt;app&lt;/code&gt; property to whatever you would like to import this package as in other projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get the file path
&lt;/h2&gt;

&lt;p&gt;Copy the complete file path to the root of your package.&lt;/p&gt;

&lt;h2&gt;
  
  
  Import the package
&lt;/h2&gt;

&lt;p&gt;In the project you’d like to import the package, open your &lt;code&gt;mix.exs&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Find &lt;code&gt;deps&lt;/code&gt; and add your package to the array using the &lt;code&gt;path&lt;/code&gt; property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;deps&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:word_list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;path:&lt;/span&gt; &lt;span class="s2"&gt;"/path/to/your_dependency"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Test with iex
&lt;/h2&gt;

&lt;p&gt;Now you can test whether your project has access to the dependency. Run an &lt;code&gt;iex&lt;/code&gt; shell for your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;iex &lt;span class="nt"&gt;-S&lt;/span&gt; mix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, check that you have the ability to run functions from your dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;MyDependency&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;
&lt;span class="ss"&gt;:world&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  More content
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/my-first-hex-package/"&gt;I published my first Elixir package to hex!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/whats-the-best-question-youve-been-asked-in-a-job-interview-4a1j"&gt;What's the best questions you've been asked in a job interview?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/what-was-the-first-program-you-wrote-1gjp"&gt;What was the first program you wrote?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/js-arrays-are-objects/"&gt;The weird quirk of JavaScript arrays (that you should never use)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/does-elixir-have-for-loops/"&gt;Does Elixir have for loops?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/learn-elixir-with-me/"&gt;Learn Elixir with me!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/bread-ratio-calculator/"&gt;Project Tours: Bread Ratio Calculator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/emoji-skin-tone/"&gt;Changing Emoji Skin Tones Programmatically&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/i-made-my-first-svg-animation-29j3"&gt;I made my first svg animation!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/tips-for-first-npm-package/"&gt;5 tips for publishing your first npm package&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/hugo-beginner-mistakes/"&gt;4 Hugo Beginner Mistakes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>elixir</category>
      <category>functional</category>
      <category>beginners</category>
    </item>
    <item>
      <title>I published my first Elixir package to hex!</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Wed, 19 Jan 2022 13:30:46 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/i-published-my-first-elixir-package-to-hex-39fa</link>
      <guid>https://dev.to/brewinstallbuzzwords/i-published-my-first-elixir-package-to-hex-39fa</guid>
      <description>&lt;p&gt;As someone who primarily writes code in JavaScript, I’ve grown accustomed to npm packages existing for pretty much any use case I can come up with. But recently while working on an Elixir project, I came across a situation where an npm package I’ve used previously would be helpful, but I couldn’t find a similar package on hex.&lt;/p&gt;

&lt;p&gt;That, of course, is one of the drawbacks to using a language that isn’t as mainstream — there are going to be fewer online resources to take advantage of when you want to find a dependency or find an answer to a question.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem I needed to solve
&lt;/h2&gt;

&lt;p&gt;The use case for my project was fairly simple. I needed a way to import a very long list of English words, which I could then filter based on different criteria in my application.&lt;/p&gt;

&lt;p&gt;There is a pre-existing npm package called &lt;a href="https://www.npmjs.com/package/word-list"&gt;word-list&lt;/a&gt; that solves this, and I’ve used it before in another project I worked on. But after doing some searching, I didn’t find anything similar on hex, Elixir’s package manager.&lt;/p&gt;

&lt;h2&gt;
  
  
  My solution
&lt;/h2&gt;

&lt;p&gt;Since word-list had already compiled a list of English words, I decided to make a clone of that project in Elixir.&lt;/p&gt;

&lt;p&gt;You can find my package &lt;a href="https://www.hex.pm/packages/word_list"&gt;here&lt;/a&gt; and the source code &lt;a href="https://github.com/brew-install-buzzwords/word_list"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The code
&lt;/h2&gt;

&lt;p&gt;My solution contains a single function that imports the text file containing all the words and makes it available as a stream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;WordList&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;getStream!&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;app_dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:word_list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/priv/words.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stream!&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additionally, I added a call to &lt;code&gt;Steam.map/2&lt;/code&gt; to trim the new newline character from each word. However, since this is a Stream, that operation is performed lazily. Doing so helps avoid a long initial load time to getting the list of words, and instead pushes off the trim until a word is actually retrieved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Publishing my package
&lt;/h2&gt;

&lt;p&gt;I found that publishing to hex was pretty easy. I followed &lt;a href="https://hex.pm/docs/publish"&gt;this documentation&lt;/a&gt; and didn’t run into any issues.&lt;/p&gt;

&lt;p&gt;Once I registered an account with hex and filled out the appropriate fields in &lt;code&gt;mix.exs&lt;/code&gt;, all I had to do to publish was run &lt;code&gt;mix hex.publish&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Have you published to hex?
&lt;/h2&gt;

&lt;p&gt;If you’ve published any packages to hex before, feel free to share them in the comments. Have you ever run into any difficulties with the process? Have you learned any techniques that streamline the process?&lt;/p&gt;

&lt;h2&gt;
  
  
  More Content
&lt;/h2&gt;

&lt;p&gt;If you liked this, you might also like some of my other posts. If you want to be notified of my new posts, &lt;a href="https://dev.to/brewinstallbuzzwords"&gt;follow me on Dev&lt;/a&gt; or subscribe to my &lt;a href="https://mailchi.mp/89dc077154c7/brewinstallbuzzwords"&gt;brief monthly newsletter&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/whats-the-best-question-youve-been-asked-in-a-job-interview-4a1j"&gt;What's the best questions you've been asked in a job interview?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/what-was-the-first-program-you-wrote-1gjp"&gt;What was the first program you wrote?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/the-weird-quirk-of-javascript-arrays-that-you-should-never-use-4l3b"&gt;The weird quirk of JavaScript arrays (that you should never use)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/does-elixir-have-for-loops/"&gt;Does Elixir have for loops?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/learn-elixir-with-me/"&gt;Learn Elixir with me!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/bread-ratio-calculator/"&gt;Project Tours: Bread Ratio Calculator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/emoji-skin-tone/"&gt;Changing Emoji Skin Tones Programmatically&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/i-made-my-first-svg-animation-29j3"&gt;I made my first svg animation!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/tips-for-first-npm-package/"&gt;5 tips for publishing your first npm package&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/hugo-beginner-mistakes/"&gt;4 Hugo Beginner Mistakes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>elixir</category>
      <category>functional</category>
      <category>devjournal</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Reading the Elixir source code to learn how if blocks work</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Wed, 12 Jan 2022 12:18:34 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/reading-the-elixir-source-code-to-learn-how-if-blocks-work-58l8</link>
      <guid>https://dev.to/brewinstallbuzzwords/reading-the-elixir-source-code-to-learn-how-if-blocks-work-58l8</guid>
      <description>&lt;p&gt;As I’ve been working with Elixir, I’ve been wondering about the ways that macros can be leveraged. So once I realized that &lt;code&gt;if&lt;/code&gt; statements are implemented as macros, I figured that reading the source code would be a good way to learn from the creators of the language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the code
&lt;/h2&gt;

&lt;p&gt;In the Elixir documentation, every function has a &lt;code&gt;&amp;lt;/&amp;gt;&lt;/code&gt; button that links to its source code. If you follow that button in &lt;a href="https://hexdocs.pm/elixir/Kernel.html#if/2"&gt;the documentation for &lt;code&gt;if/2&lt;/code&gt;&lt;/a&gt;, you’ll find the code that makes &lt;code&gt;if&lt;/code&gt; statements work. I’ve pasted it below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmacro&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;clauses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;build_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;clauses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;build_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;do_clause&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;build_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;do_clause&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;build_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;do_clause&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;else_clause&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;optimize_boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;when&lt;/span&gt; &lt;span class="ss"&gt;:"Elixir.Kernel"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="ow"&gt;in&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;else_clause&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;do_clause&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;build_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;ArgumentError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"invalid or duplicate keys for if, only &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;do&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; and an optional &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;else&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; are permitted"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s break this down...&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining the macro
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmacro&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;clauses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;build_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;clauses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It starts out by defining a macro for &lt;code&gt;if/2&lt;/code&gt; that accepts the parameters &lt;code&gt;condition&lt;/code&gt; and &lt;code&gt;clauses&lt;/code&gt;. While the documentation says “this macro expects the first argument to be a condition and the second argument to be a keyword list” there is no programmatic enforcement of that at this level.&lt;/p&gt;

&lt;p&gt;Within the macro’s body, all we have is a call to &lt;code&gt;build_if/2&lt;/code&gt; that passes along the unmodified parameters of &lt;code&gt;if/2&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;build_if/2&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;build_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;do_clause&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;build_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;do_clause&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;build_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;ArgumentError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"invalid or duplicate keys for if, only &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;do&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; and an optional &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;else&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; are permitted"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a private function called by the macro and exists in two forms.&lt;/p&gt;

&lt;p&gt;The first instance of this function has pattern matching for the &lt;code&gt;clauses&lt;/code&gt; argument to only accept a value that starts with &lt;code&gt;do&lt;/code&gt;. If the pattern matching succeeds, a call is made to &lt;code&gt;build_if/3&lt;/code&gt; with a value of &lt;code&gt;nil&lt;/code&gt; for the &lt;code&gt;else&lt;/code&gt; clause.&lt;/p&gt;

&lt;p&gt;The second instance does not use pattern matching, but instead has an underscore at the beginning of the parameter names to indicate that their values will not be used. This function raises an &lt;code&gt;ArgumentError&lt;/code&gt; to show that the requisite pattern matching for the &lt;code&gt;do&lt;/code&gt; clause has not been met.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;build_if/3&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;build_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;do_clause&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;else_clause&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;optimize_boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kn"&gt;quote&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;when&lt;/span&gt; &lt;span class="ss"&gt;:"Elixir.Kernel"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="ow"&gt;in&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;else_clause&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;unquote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;do_clause&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: I’m not quite sure what the purpose of &lt;code&gt;optimize_boolean&lt;/code&gt; is, but the source code for it is &lt;a href="https://github.com/elixir-lang/elixir/blob/33f9d04851a2664aeae5f5f78eeef9e5b4c38030/lib/elixir/lib/kernel.ex#L1761"&gt;here&lt;/a&gt; if you want to take a look at it. If you know what it’s purpose is, let me know!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This function is where the actual logic of the &lt;code&gt;if&lt;/code&gt; is carried out.&lt;/p&gt;

&lt;p&gt;It makes use of &lt;code&gt;quote/2&lt;/code&gt; and &lt;code&gt;unquote/1&lt;/code&gt;. Quoting returns the internal representation of an expression, and unquoting gets an expression from an internal representation. You can read more about them &lt;a href="https://elixir-lang.org/getting-started/meta/quote-and-unquote.html#quoting"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;First, we enter a call to &lt;code&gt;quote/2&lt;/code&gt; and start a &lt;code&gt;case&lt;/code&gt; statement that unquotes the &lt;code&gt;condition&lt;/code&gt; parameter in order to evaluate it.&lt;/p&gt;

&lt;p&gt;Let’s break down the first pattern in the &lt;code&gt;case&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;:"Elixir.Kernel".in&lt;/code&gt; makes a call to &lt;code&gt;in/2&lt;/code&gt; which is actually just a macro for &lt;code&gt;Enum.member?/2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Enum.member?/2&lt;/code&gt; checks whether an element is within an enumerable&lt;/li&gt;
&lt;li&gt;So, &lt;code&gt;:"Elixir.Kernel".in(x, [false, nil])&lt;/code&gt; checks whether &lt;code&gt;x&lt;/code&gt; (the condition) is equal to either &lt;code&gt;false&lt;/code&gt; or &lt;code&gt;nil&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;From there, we see that &lt;code&gt;condition&lt;/code&gt; evaluating to either &lt;code&gt;false&lt;/code&gt; or &lt;code&gt;nil&lt;/code&gt; will result in &lt;code&gt;else_clause&lt;/code&gt; being returned.&lt;/p&gt;

&lt;p&gt;The default case returns the &lt;code&gt;do_clause&lt;/code&gt;, which is what we would expect to be returned if the condition was truthy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;We can draw a few interesting observations from reading through this code. The first is that truthiness in Elixir’s &lt;code&gt;if&lt;/code&gt; statements is defined as being neither &lt;code&gt;false&lt;/code&gt; nor &lt;code&gt;nil&lt;/code&gt;. This means that &lt;code&gt;if/2&lt;/code&gt; can be a simple way to test whether a variable has neither of those values.&lt;/p&gt;

&lt;p&gt;The other lesson is that macros can work well for implementing control flow. By passing the &lt;code&gt;do&lt;/code&gt; and &lt;code&gt;else&lt;/code&gt; clauses around as keyword lists, it prevents that code from being run unless we decide it should run.&lt;/p&gt;

&lt;h2&gt;
  
  
  More Content
&lt;/h2&gt;

&lt;p&gt;If you liked this, you might also like some of my other posts. If you want to be notified of my new posts, &lt;a href="https://dev.to/brewinstallbuzzwords"&gt;follow me on Dev&lt;/a&gt; or subscribe to my &lt;a href="https://mailchi.mp/89dc077154c7/brewinstallbuzzwords"&gt;brief monthly newsletter&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/whats-the-best-question-youve-been-asked-in-a-job-interview-4a1j"&gt;What's the best questions you've been asked in a job interview?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/what-was-the-first-program-you-wrote-1gjp"&gt;What was the first program you wrote?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/the-weird-quirk-of-javascript-arrays-that-you-should-never-use-4l3b"&gt;The weird quirk of JavaScript arrays (that you should never use)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/does-elixir-have-for-loops/"&gt;Does Elixir have for loops?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/learn-elixir-with-me/"&gt;Learn Elixir with me!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/bread-ratio-calculator/"&gt;Project Tours: Bread Ratio Calculator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/emoji-skin-tone/"&gt;Changing Emoji Skin Tones Programmatically&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/brewinstallbuzzwords/i-made-my-first-svg-animation-29j3"&gt;I made my first svg animation!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/tips-for-first-npm-package/"&gt;5 tips for publishing your first npm package&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brewinstallbuzzwords.com/posts/hugo-beginner-mistakes/"&gt;4 Hugo Beginner Mistakes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>elixir</category>
      <category>functional</category>
      <category>devjournal</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>Which VS Code shortcut do you use the most?</title>
      <dc:creator>Adam Davis</dc:creator>
      <pubDate>Wed, 12 Jan 2022 12:17:48 +0000</pubDate>
      <link>https://dev.to/brewinstallbuzzwords/which-vs-code-shortcut-do-you-use-the-most-d9i</link>
      <guid>https://dev.to/brewinstallbuzzwords/which-vs-code-shortcut-do-you-use-the-most-d9i</guid>
      <description>&lt;p&gt;If VS Code is your primary editor, what’s the shortcut you use most frequently?&lt;/p&gt;

&lt;p&gt;Do you have another shortcut you use less frequently, but you enjoy using it a lot?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>productivity</category>
      <category>watercooler</category>
    </item>
  </channel>
</rss>
