{
    "version": "https://jsonfeed.org/version/1",
    "title": "Ryan Winchester",
    "home_page_url": "https://winchester.dev",
    "feed_url": "https://winchester.dev/rss/feed.json",
    "description": "Thoughts on programming and maybe other things",
    "icon": "https://winchester.dev/favicon.ico",
    "author": {
        "name": "Ryan Winchester"
    },
    "items": [
        {
            "id": "https://winchester.dev/posts/intro-to-elixir-for-non-ruby-programmers",
            "content_html": "<h2>Goals</h2>\n<p>I come from the land of PHP and JavaScript, with very little Ruby experience. I have seen quite a few posts around the interwebs introducing Elixir from a Rubyist&#x27;s perspective but I haven&#x27;t really seen any from a perspective I can relate to. The syntax of Elixir is kind of foreign to most people outside of Ruby-land, so I hope to help push people a little bit over the initial hump with some examples using PHP and JavaScript (ES2015) along with the Elixir examples.</p>\n<p>My goal with this post is to help introduce the syntax as well as a shallow introduction to immutability and how we use recursion instead of loops.</p>\n<p>If you have Elixir installed, you can paste or type examples into <strong>IEx</strong> (interactive elixir). Start <strong>IEx</strong> shell by just typing <code>iex</code> into your terminal.</p>\n<h2>Syntax and Semantics</h2>\n<p>So, we have to admit that the Elixir syntax is very Ruby-like although the concepts and semantics are largely inherited from Erlang.\nHowever, Elixir introduces a lot of great new things of it&#x27;s own, as well as cherry-picking some nice concepts from other functional languages.</p>\n<h2>Types</h2>\n<h3>Basic types</h3>\n<p>Please read the elixir guide on <a href=\"http://elixir-lang.org/getting-started/basic-types.html\"><strong>basic types</strong></a> for more detailed info, this is a good place to start.</p>\n<p>These common types you should already be used to:</p>\n<ul>\n<li><strong>Boolean</strong>: <code>true</code>, <code>false</code></li>\n<li><a href=\"https://hexdocs.pm/elixir/Integer.html\"><strong>Integer</strong></a>: <code>1</code></li>\n<li><a href=\"https://hexdocs.pm/elixir/Float.html\"><strong>Float</strong></a>: <code>3.14</code></li>\n<li><a href=\"https://hexdocs.pm/elixir/String.html\"><strong>String</strong></a>: <code>&quot;Hello World&quot;</code> - <em>always</em> using double quotes.</li>\n<li><a href=\"https://hexdocs.pm/elixir/List.html\"><strong>List</strong></a>: <code>[1, 2, 3]</code>- This is a singly linked list. Like an array in PHP or JavaScript, but not.</li>\n</ul>\n<p>And maybe some you aren&#x27;t used to:</p>\n<ul>\n<li><a href=\"https://hexdocs.pm/elixir/Atom.html\"><strong>Atom</strong></a>: <code>:foo</code> - A constant whose value is its name. In some other languages it might be called a symbol. Later, you will see it&#x27;s quite useful for pattern-matching.</li>\n<li><a href=\"https://hexdocs.pm/elixir/Tuple.html\"><strong>Tuple</strong></a>: <code>{1, &quot;two&quot;, :three}</code> - An ordered group of related values (any values).</li>\n</ul>\n<h3>Keyword Lists, Maps, and Structs</h3>\n<p>Please read the elixir guides on <a href=\"http://elixir-lang.org/getting-started/keywords-and-maps.html\"><strong>keyword lists &amp; maps</strong></a>, and <a href=\"http://elixir-lang.org/getting-started/structs.html\"><strong>structs</strong></a> for more detailed info.</p>\n<ul>\n<li><a href=\"https://hexdocs.pm/elixir/Keyword.html\"><strong>Keyword List</strong></a>: <code>[one: 1, two: 2, three: 3]</code> - This is similar to an associative array in PHP, or a plain object in JavaScript (except it is ordered). It is basically just syntax sugar on top of lists of two-tuples of <code>{:key, value}</code>.</li>\n<li><a href=\"https://hexdocs.pm/elixir/Map.html\"><strong>Map</strong></a>: <code>%{name: &quot;Joe&quot;, email: &quot;joe@example.com&quot;}</code> - This is also <em>similar</em> to an associative array in PHP (except it is unordered) or a plain JavaScript object.</li>\n<li><a href=\"https://hexdocs.pm/elixir/Struct.html\"><strong>Struct</strong></a>: <code>%Person{name: &quot;Joe&quot;, email: &quot;joe@example.com&quot;}</code> - It&#x27;s a map but with some compile-time guarantees and predefined properties.</li>\n</ul>\n<h2>Functions</h2>\n<p>In Elixir, functions (named functions) are always defined in modules. Modules are basically just however you decide that you want to group your functions. You should do this in a way that has meaning to you and makes sense in the context of your project. Defining a function is similar to any other language.</p>\n<p>You define and use a function as follows: <em>(I&#x27;m encapsulating the PHP and JavaScript versions in a class and object, to better compare with the Elixir module syntax)</em></p>\n<h4>PHP</h4>\n<pre><code class=\"hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class</span> <span class=\"hljs-title\">StrHelper</span>\n</span>{\n    <span class=\"hljs-keyword\">public</span> <span class=\"hljs-built_in\">static</span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function</span> <span class=\"hljs-title\">uppity</span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$str</span> = <span class=\"hljs-string\">&quot;default&quot;</span></span>)\n    </span>{\n        <span class=\"hljs-keyword\">return</span> <span class=\"hljs-title function_ invoke__\">strtoupper</span>(<span class=\"hljs-variable\">$str</span>);\n    }\n}\n\n<span class=\"hljs-title class_\">StrHelper</span>::<span class=\"hljs-title function_ invoke__\">uppity</span>(<span class=\"hljs-string\">&quot;foo&quot;</span>);  <span class=\"hljs-comment\">// &quot;FOO&quot;</span>\n</code></pre>\n<h4>JavaScript</h4>\n<pre><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let</span> strHelper = {\n  <span class=\"hljs-title function_\">uppity</span>(<span class=\"hljs-params\">str = <span class=\"hljs-string\">&quot;default&quot;</span></span>) {\n    <span class=\"hljs-keyword\">return</span> str.<span class=\"hljs-title function_\">toUpperCase</span>()\n  }\n}\n\nstrHelper.<span class=\"hljs-title function_\">uppity</span>(<span class=\"hljs-string\">&quot;foo&quot;</span>)  <span class=\"hljs-comment\">// &quot;FOO&quot;</span>\n</code></pre>\n<h4>Elixir</h4>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">defmodule</span> <span class=\"hljs-title\">StrHelper</span></span> <span class=\"hljs-keyword\">do</span>\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">uppity</span></span>(str \\\\ <span class=\"hljs-string\">&quot;default&quot;</span>) <span class=\"hljs-keyword\">do</span>\n    <span class=\"hljs-title class_\">String</span>.upcase(str)\n  <span class=\"hljs-keyword\">end</span>\n<span class=\"hljs-keyword\">end</span>\n\n<span class=\"hljs-title class_\">StrHelper</span>.uppity(<span class=\"hljs-string\">&quot;foo&quot;</span>)  <span class=\"hljs-comment\"># &quot;FOO&quot;</span>\n</code></pre>\n<h2>Anonymous functions</h2>\n<p>Anonymous functions are first class values and can be passed as arguments or returned from other functions.</p>\n<p>Here is how you define and call an anonymous function:</p>\n<h4>PHP</h4>\n<pre><code class=\"hljs language-php\"><span class=\"hljs-variable\">$uppity</span> = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function</span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$bar</span></span>) </span>{\n    <span class=\"hljs-keyword\">return</span> <span class=\"hljs-title function_ invoke__\">strtoupper</span>(<span class=\"hljs-variable\">$bar</span>);\n}\n\n<span class=\"hljs-variable\">$uppity</span>(<span class=\"hljs-string\">&quot;bar&quot;</span>);  <span class=\"hljs-comment\">// &quot;BAR&quot;</span>\n</code></pre>\n<h4>JavaScript</h4>\n<pre><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let</span> uppity = <span class=\"hljs-keyword\">function</span>(<span class=\"hljs-params\">bar</span>) {\n  <span class=\"hljs-keyword\">return</span> bar.<span class=\"hljs-title function_\">toUpperCase</span>()\n}\n\n<span class=\"hljs-title function_\">uppity</span>(<span class=\"hljs-string\">&quot;bar&quot;</span>)  <span class=\"hljs-comment\">// &quot;BAR&quot;</span>\n</code></pre>\n<h4>Elixir</h4>\n<pre><code class=\"hljs language-elixir\">uppity = <span class=\"hljs-keyword\">fn</span> bar -&gt;\n  <span class=\"hljs-title class_\">String</span>.upcase(bar)\n<span class=\"hljs-keyword\">end</span>\n\nuppity.(<span class=\"hljs-string\">&quot;bar&quot;</span>)  <span class=\"hljs-comment\"># &quot;BAR&quot;</span>\n<span class=\"hljs-comment\"># Notice the dot is required here.</span>\n</code></pre>\n<p>Why do we need a dot for anonymous functions?</p>\n<blockquote>\n<p>The main reason is that Elixir is a lisp-2 language - this means that variables and functions “occupy” different name spaces, so calling a function in the functions namespace and calling a function in the variables namespace has to work differently. Most languages are lisp-1 (which means that variables and functions are in the same namespace). Some other lisp-2 languages are Ruby, Common Lisp and Perl.</p>\n<p>– <a href=\"https://elixirforum.com/t/anonymous-functions-the-dot-and-parameters/10886/7?u=ryanwinchester\">michalmuskala</a></p>\n</blockquote>\n<blockquote>\n<p>From a practical view variables and function names have basically the same syntax so you need to differentiate between when you want the function name and when you want the value of the variable.</p>\n<p>– <a href=\"https://elixirforum.com/t/anonymous-functions-the-dot-and-parameters/10886/8?u=ryanwinchester\">rvirding</a></p>\n</blockquote>\n<h2>Anonymous function short-hand</h2>\n<p>There is a &quot;capture operator&quot; in Elixir which can be used to either capture an existing named function to turn it into an anonymous function that can be passed as an argument,\nor to define a new anonymous function.</p>\n<h4>PHP</h4>\n<p>In PHP they are called &quot;arrow functions.&quot;</p>\n<pre><code class=\"hljs language-php\"><span class=\"hljs-variable\">$uppity</span> = <span class=\"hljs-function\"><span class=\"hljs-keyword\">fn</span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$bar</span></span>) =&gt;</span> <span class=\"hljs-title function_ invoke__\">strtoupper</span>(<span class=\"hljs-variable\">$bar</span>);\n\n<span class=\"hljs-variable\">$uppity</span>(<span class=\"hljs-string\">&quot;bar&quot;</span>);  <span class=\"hljs-comment\">// &quot;BAR&quot;</span>\n</code></pre>\n<h4>JavaScript</h4>\n<p>They are also called arrow functions in JavaScript.</p>\n<pre><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let</span> <span class=\"hljs-title function_\">uppity</span> = (<span class=\"hljs-params\">bar</span>) =&gt; bar.<span class=\"hljs-title function_\">toUpperCase</span>()\n\n<span class=\"hljs-title function_\">uppity</span>(<span class=\"hljs-string\">&quot;bar&quot;</span>)  <span class=\"hljs-comment\">// &quot;BAR&quot;</span>\n</code></pre>\n<h4>Elixir</h4>\n<pre><code class=\"hljs language-elixir\">uppity = &amp;(<span class=\"hljs-title class_\">String</span>.upcase(&amp;<span class=\"hljs-number\">1</span>))\n\nuppity.(<span class=\"hljs-string\">&quot;bar&quot;</span>)  <span class=\"hljs-comment\"># &quot;BAR&quot;</span>\n</code></pre>\n<p>Since the number of arguments of the anonymous function match the number of arguments of the named function we are using, we can also write it as:</p>\n<pre><code class=\"hljs language-elixir\">uppity = &amp;<span class=\"hljs-title class_\">String</span>.upcase/<span class=\"hljs-number\">1</span>\n\nuppity.(<span class=\"hljs-string\">&quot;bar&quot;</span>)  <span class=\"hljs-comment\"># &quot;BAR&quot;</span>\n</code></pre>\n<p>Parentheses are optional in Elixir for function (and macro) calls. These are all the same thing:</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-keyword\">fn</span> s -&gt; <span class=\"hljs-title class_\">String</span>.upcase(s) <span class=\"hljs-keyword\">end</span>\n&amp;(<span class=\"hljs-title class_\">String</span>.upcase(&amp;<span class=\"hljs-number\">1</span>))\n&amp;<span class=\"hljs-title class_\">String</span>.upcase(&amp;<span class=\"hljs-number\">1</span>)\n&amp;<span class=\"hljs-title class_\">String</span>.upcase/<span class=\"hljs-number\">1</span>\n</code></pre>\n<h3>Capturing named functions</h3>\n<p>In Elixir, functions are not referred to only by their name, but also their <em><strong>arity</strong></em>. This just means they are defined by their name and the number of arguments they take.</p>\n<p>For example <code>String.upcase</code> takes one argument, so it is referenced as <code>String.upcase/1</code>. Also, <code>Enum.map</code> takes two arguments, so it is referenced as <code>Enum.map/2</code>.</p>\n<p>Putting that together, here is an example of capturing the exisiting function <code>String.upcase/1</code>, for use in <code>Enum.map/2</code>, which takes an anonymous function as it&#x27;s second argument.</p>\n<pre><code class=\"hljs language-elixir\">str = <span class=\"hljs-string\">&quot;Bananas&quot;</span>\nstr_list = <span class=\"hljs-title class_\">String</span>.graphemes(str)\n<span class=\"hljs-comment\">#=&gt; [&quot;B&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;s&quot;]</span>\n\n<span class=\"hljs-title class_\">Enum</span>.map(str_list, &amp;<span class=\"hljs-title class_\">String</span>.upcase/<span class=\"hljs-number\">1</span>)\n<span class=\"hljs-comment\">#=&gt; [&quot;B&quot;, &quot;A&quot;, &quot;N&quot;, &quot;A&quot;, &quot;N&quot;, &quot;A&quot;,&quot;S&quot;]</span>\n\n<span class=\"hljs-comment\"># Which is really the eqiuvalent of</span>\n<span class=\"hljs-title class_\">Enum</span>.map(str_list, <span class=\"hljs-keyword\">fn</span> c -&gt; <span class=\"hljs-title class_\">String</span>.upcase(c) <span class=\"hljs-keyword\">end</span>)\n<span class=\"hljs-comment\">#=&gt; [&quot;B&quot;, &quot;A&quot;, &quot;N&quot;, &quot;A&quot;, &quot;N&quot;, &quot;A&quot;,&quot;S&quot;]</span>\n</code></pre>\n<h2>Implicit returns</h2>\n<p>You may have noticed that there is no <code>return</code> statement, because Elixir has implicit returns.\nThis means that the result of the last statement is what is returned.</p>\n<p>so:</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">defmodule</span> <span class=\"hljs-title\">Foo</span></span> <span class=\"hljs-keyword\">do</span>\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">baz?</span></span>(str) <span class=\"hljs-keyword\">do</span>\n    <span class=\"hljs-keyword\">if</span> <span class=\"hljs-title class_\">String</span>.equivalent?(str, <span class=\"hljs-string\">&quot;baz&quot;</span>) <span class=\"hljs-keyword\">do</span>\n      <span class=\"hljs-string\">&quot;It&#x27;s baz!&quot;</span>\n    <span class=\"hljs-keyword\">else</span>\n      <span class=\"hljs-string\">&quot;It&#x27;s NOT baz!&quot;</span>\n    <span class=\"hljs-keyword\">end</span>\n  <span class=\"hljs-keyword\">end</span>\n<span class=\"hljs-keyword\">end</span>\n</code></pre>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-title class_\">Foo</span>.baz?(<span class=\"hljs-string\">&quot;baz&quot;</span>)  <span class=\"hljs-comment\"># &quot;It&#x27;s baz!&quot;</span>\n<span class=\"hljs-title class_\">Foo</span>.baz?(<span class=\"hljs-string\">&quot;bar&quot;</span>)  <span class=\"hljs-comment\"># &quot;It&#x27;s NOT baz!&quot;</span>\n</code></pre>\n<p>Passing <code>&quot;baz&quot;</code> above means the last statement executed is <code>&quot;It&#x27;s baz!&quot;</code>.</p>\n<p>Also, notice the function name <code>baz?</code> includes a <code>?</code> character. This is a common convention in Elixir where a function that returns a boolean will include a <code>?</code> in the name, as opposed to other languages where it would be common to name it something like <code>is_baz</code>.</p>\n<h2>Pattern matching</h2>\n<p>There is a lot to say about pattern matching, because it&#x27;s kind of a big deal in Elixir. However, I&#x27;m going to keep it shallow.</p>\n<h3>Pattern matching in function arguments</h3>\n<p>The last example could be re-written as:</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">defmodule</span> <span class=\"hljs-title\">Foo</span></span> <span class=\"hljs-keyword\">do</span>\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">baz?</span></span>(<span class=\"hljs-string\">&quot;baz&quot;</span>) <span class=\"hljs-keyword\">do</span>\n    <span class=\"hljs-string\">&quot;It&#x27;s baz!&quot;</span>\n  <span class=\"hljs-keyword\">end</span>\n\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">baz?</span></span>(str) <span class=\"hljs-keyword\">do</span>\n    <span class=\"hljs-string\">&quot;It&#x27;s NOT baz!&quot;</span>\n  <span class=\"hljs-keyword\">end</span>\n<span class=\"hljs-keyword\">end</span>\n</code></pre>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-title class_\">Foo</span>.baz?(<span class=\"hljs-string\">&quot;baz&quot;</span>)  <span class=\"hljs-comment\"># &quot;It&#x27;s baz!&quot;</span>\n<span class=\"hljs-title class_\">Foo</span>.baz?(<span class=\"hljs-string\">&quot;bar&quot;</span>)  <span class=\"hljs-comment\"># &quot;It&#x27;s NOT baz!&quot;</span>\n</code></pre>\n<p>You can define the same function multiple times, and Elixir will execute the first one whose argument pattern <em>matches</em>. So, in the example above, the first function will only ever match when <code>&quot;baz&quot;</code> is passed, and the second one will match any other argument.</p>\n<h3>Pattern matching variable assignment</h3>\n<pre><code class=\"hljs language-elixir\">a = <span class=\"hljs-number\">1</span>\n</code></pre>\n<p>This looks like typical assignment as done in other languages, but in Elixir it is actually matching the pattern of the left-hand side and right-hand side and doing the assignments based on that. Here is an example that might help clarify what this means. Pay attention to how the left-hand and right-hand side match, and how assignment is done based off of that:</p>\n<pre><code class=\"hljs language-elixir\">{a, b, c} = {<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">3</span>}\n<span class=\"hljs-comment\">#=&gt; a = 1, b = 2, c = 3</span>\n\n{i, j, <span class=\"hljs-number\">3</span>} = {<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">3</span>}\n<span class=\"hljs-comment\">#=&gt; i = 1, j = 2</span>\n\n{x, y, <span class=\"hljs-number\">9</span>} = {<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">3</span>}\n<span class=\"hljs-comment\">#=&gt; ** (MatchError) no match of right hand side value: {1, 2, 3}</span>\n</code></pre>\n<p>Some more examples, note that assignment can only be done if the variable you want to assign is on the left:</p>\n<pre><code class=\"hljs language-elixir\">x = <span class=\"hljs-number\">1</span>\n<span class=\"hljs-comment\">#=&gt; 1</span>\n\n<span class=\"hljs-number\">1</span> = x\n<span class=\"hljs-comment\">#=&gt; 1</span>\n\n<span class=\"hljs-number\">2</span> = x\n<span class=\"hljs-comment\">#=&gt; ** (MatchError) no match of right hand side value: 1</span>\n\n<span class=\"hljs-number\">2</span> = z\n<span class=\"hljs-comment\">#=&gt; ** (CompileError) undefined function z/0 </span>\n<span class=\"hljs-comment\">#      (there is no such import)</span>\n\nz = <span class=\"hljs-number\">2</span>\n<span class=\"hljs-comment\">#=&gt; 2</span>\n</code></pre>\n<h2>List building and destructuring</h2>\n<p>If you are not familiar with linked lists this section might be slightly confusing, but let&#x27;s give it a chance. Don&#x27;t worry if it doesn&#x27;t sink in right away.</p>\n<p>A list is a singly linked-list with a tail and a head, <code>[head | tail]</code>, where head is a single item, and tail is the rest of the list. e.g. Writing <code>[1, 2, 3]</code> is the same as <code>[1 | [2, 3]]</code> is the same as\n<code>[1 | [2 | [3  | []]]]</code>. A list is basically a recursive structure and the final tail is an empty list <code>[]</code>.</p>\n<p>So, you can both build lists this way or pattern-match to destructure.</p>\n<pre><code class=\"hljs language-elixir\">x = [<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">3</span>]\n\n<span class=\"hljs-comment\"># Lets assign the head to `a` and the tail to `b`.</span>\n[a | b] = x\n<span class=\"hljs-comment\">#=&gt; a = 1</span>\n<span class=\"hljs-comment\">#=&gt; b = [2, 3]</span>\n\n<span class=\"hljs-comment\"># Next, walking through the list.</span>\n[c | d] = b\n<span class=\"hljs-comment\">#=&gt; c = 2</span>\n<span class=\"hljs-comment\">#=&gt; d = [3]</span>\n\n<span class=\"hljs-comment\"># Again, and the final tail is an empty list.</span>\n[e | f] = d\n<span class=\"hljs-comment\">#=&gt; e = 3</span>\n<span class=\"hljs-comment\">#=&gt; f = []</span>\n\n<span class=\"hljs-comment\"># Now that `f` is an empty list, what do you think</span>\n<span class=\"hljs-comment\"># will happen if we try to get a head and tail?</span>\n[g | h] = f\n<span class=\"hljs-comment\">#=&gt; ** (MatchError) no match of right hand side value: []</span>\n\n<span class=\"hljs-comment\"># Elixir doesn&#x27;t care if you do the following. It just returns the</span>\n<span class=\"hljs-comment\"># result `[]`, since it matches.</span>\n[] = f\n<span class=\"hljs-comment\">#=&gt; []</span>\n\n<span class=\"hljs-comment\"># There is no head or tail in an empty list, which is why</span>\n<span class=\"hljs-comment\"># matching on an empty list would be your recursive &quot;base case&quot;</span>\n<span class=\"hljs-comment\"># when using a list.</span>\n</code></pre>\n<h2>Collections</h2>\n<p>Elixir doesn&#x27;t have loops, so iterating collections is done with recursion.</p>\n<h3>Recursion</h3>\n<p>In a recursive function, we will call the same function recursively until a condition is met.</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">defmodule</span> <span class=\"hljs-title\">Math</span></span> <span class=\"hljs-keyword\">do</span>\n  <span class=\"hljs-comment\">@doc &quot;&quot;&quot;\n  If you call the function with no arguments, we&#x27;ll start\n  automatically from `0`.\n\n  This function would be referred to as `Math.count_to_ten/0` and\n  executed as `Math.count_to_ten()`.\n  &quot;&quot;&quot;</span>\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">count_to_ten</span></span> <span class=\"hljs-keyword\">do</span>\n    count_to_ten(<span class=\"hljs-number\">0</span>)\n  <span class=\"hljs-keyword\">end</span>\n\n  <span class=\"hljs-comment\">@doc &quot;&quot;&quot;\n  You can specify which number to start from.\n\n  We can add a `guard` on the function to make sure that the\n  starting number is less than or equal to `10`. This is the\n  `when` part. It is called a guard.\n\n  This function would be referred to as `Math.count_to_ten/1`.\n  &quot;&quot;&quot;</span>\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">count_to_ten</span></span>(<span class=\"hljs-number\">10</span>), <span class=\"hljs-symbol\">do:</span> <span class=\"hljs-title class_\">IO</span>.puts(<span class=\"hljs-number\">10</span>)\n\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">count_to_ten</span></span>(from) <span class=\"hljs-keyword\">when</span> from &lt; <span class=\"hljs-number\">10</span> <span class=\"hljs-keyword\">do</span>\n    <span class=\"hljs-title class_\">IO</span>.puts(from)\n    count_to_ten(from + <span class=\"hljs-number\">1</span>)\n  <span class=\"hljs-keyword\">end</span>\n<span class=\"hljs-keyword\">end</span>\n</code></pre>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-title class_\">Math</span>.count_to_ten(<span class=\"hljs-number\">8</span>)\n<span class=\"hljs-comment\">#=&gt; 8</span>\n<span class=\"hljs-comment\">#=&gt; 9</span>\n<span class=\"hljs-comment\">#=&gt; 10</span>\n</code></pre>\n<p>Note the multiple definitions of the same function, again. The first one that matches will be called.</p>\n<p>You will see another (probably) new-to-you concept here. We can define a function without the <code>do/end</code> if we use <code>, do:</code>. This is good for short, single line functions.</p>\n<p>Okay, but what happens if you want to count from a number greater than ten?</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-title class_\">Math</span>.count_to_ten(<span class=\"hljs-number\">13</span>)\n\n** (<span class=\"hljs-title class_\">FunctionClauseError</span>) no function clause matching\n   <span class=\"hljs-keyword\">in</span> <span class=\"hljs-title class_\">Math</span>.count_to_ten/<span class=\"hljs-number\">1</span>\n\n    <span class=\"hljs-title class_\">The</span> following arguments were given to <span class=\"hljs-title class_\">Math</span>.count_to_ten/<span class=\"hljs-number\">1</span>:\n\n        <span class=\"hljs-comment\"># 1</span>\n        <span class=\"hljs-number\">13</span>\n</code></pre>\n<p>Let&#x27;s try and add the possibility of counting <strong>down</strong> to ten.</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">defmodule</span> <span class=\"hljs-title\">Math</span></span> <span class=\"hljs-keyword\">do</span>\n  <span class=\"hljs-comment\">@doc &quot;&quot;&quot;\n  If you call the function with no arguments, we&#x27;ll start\n  automatically from `0`. This function would be referred\n  to as `Math.count_to_ten/0`.\n  &quot;&quot;&quot;</span>\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">count_to_ten</span></span> <span class=\"hljs-keyword\">do</span>\n    count_to_ten(<span class=\"hljs-number\">0</span>)\n  <span class=\"hljs-keyword\">end</span>\n\n  <span class=\"hljs-comment\">@doc &quot;&quot;&quot;\n  You can specify which number to start from.\n\n  We can add a `guard` on the function to make sure that the\n  starting number is less than or equal to `10`. This is the\n  `when` part. It is called a guard.\n\n  This function would be referred to as `Math.count_to_ten/1`.\n  &quot;&quot;&quot;</span>\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">count_to_ten</span></span>(<span class=\"hljs-number\">10</span>), <span class=\"hljs-symbol\">do:</span> <span class=\"hljs-title class_\">IO</span>.puts(<span class=\"hljs-number\">10</span>)\n\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">count_to_ten</span></span>(from) <span class=\"hljs-keyword\">when</span> from &lt; <span class=\"hljs-number\">10</span> <span class=\"hljs-keyword\">do</span>\n    <span class=\"hljs-title class_\">IO</span>.puts(from)\n    count_to_ten(from + <span class=\"hljs-number\">1</span>)\n  <span class=\"hljs-keyword\">end</span>\n\n  <span class=\"hljs-comment\"># We&#x27;re adding this function that matches the case when `from`</span>\n  <span class=\"hljs-comment\"># is greater than `10`.</span>\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">count_to_ten</span></span>(from) <span class=\"hljs-keyword\">when</span> from &gt; <span class=\"hljs-number\">10</span> <span class=\"hljs-keyword\">do</span>\n    <span class=\"hljs-title class_\">IO</span>.puts(from)\n    count_to_ten(from - <span class=\"hljs-number\">1</span>)\n  <span class=\"hljs-keyword\">end</span>\n<span class=\"hljs-keyword\">end</span>\n</code></pre>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-title class_\">Math</span>.count_to_ten(<span class=\"hljs-number\">8</span>)\n<span class=\"hljs-comment\">#=&gt; 8</span>\n<span class=\"hljs-comment\">#=&gt; 9</span>\n<span class=\"hljs-comment\">#=&gt; 10</span>\n\n<span class=\"hljs-title class_\">Math</span>.count_to_ten(<span class=\"hljs-number\">12</span>)\n<span class=\"hljs-comment\">#=&gt; 12</span>\n<span class=\"hljs-comment\">#=&gt; 11</span>\n<span class=\"hljs-comment\">#=&gt; 10</span>\n</code></pre>\n<p>Since we were using guards we just needed to add another function that matched that requirement.</p>\n<p>Since you&#x27;ve seen how we can match on head and tail in a list, let&#x27;s try recursion with a list. Below, we will call the list recursively until the tail is empty, and then when <code>sum/2</code> is called again,\nthe base case <code>sum([], total)</code> will match first, and we will return the total.</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">defmodule</span> <span class=\"hljs-title\">Math</span></span> <span class=\"hljs-keyword\">do</span>\n  <span class=\"hljs-comment\">@doc &quot;&quot;&quot;\n  We will accept a list of numbers and start the recursion by\n  calling `sum/2` with the initial total `0`.\n  &quot;&quot;&quot;</span>\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">sum</span></span>(numbers) <span class=\"hljs-keyword\">when</span> is_list(numbers) <span class=\"hljs-keyword\">do</span>\n    sum(numbers, <span class=\"hljs-number\">0</span>)\n  <span class=\"hljs-keyword\">end</span>\n\n  <span class=\"hljs-comment\"># These are private functions, used to walk through the list</span>\n  <span class=\"hljs-comment\"># until we match the &quot;base case&quot; with the empty list.</span>\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">defp</span> <span class=\"hljs-title\">sum</span></span>([], total), <span class=\"hljs-symbol\">do:</span> total\n\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">defp</span> <span class=\"hljs-title\">sum</span></span>([n | rest], total) <span class=\"hljs-keyword\">do</span>\n    sum(rest, total + n)\n  <span class=\"hljs-keyword\">end</span>\n<span class=\"hljs-keyword\">end</span>\n</code></pre>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-title class_\">Math</span>.sum([<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">3</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">5</span>])\n<span class=\"hljs-comment\">#=&gt; 15</span>\n</code></pre>\n<h3>Built-in collection functions</h3>\n<p>Luckily, we have functions that should cover at least all of your basic needs in modules like <a href=\"https://hexdocs.pm/elixir/Enum.html#content\"><code>Enum</code></a>, and <a href=\"https://hexdocs.pm/elixir/List.html#content\"><code>List</code></a>.</p>\n<p>You should be familiar with a lot of the functions, especially the most common ones like <code>map</code>, <code>filter</code>, and <code>reduce</code> from your other languages. They are common in languages like PHP and JavaScript.</p>\n<p>The most notable thing about these functions is that <em>none</em> of them will mutate the original collection. (Actually, no functions in elixir will, ever.)</p>\n<p>Using a built-in <strong>map</strong> function to apply an anonymous function to each item in a collection:</p>\n<h4>JavaScript</h4>\n<pre><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let</span> items = [<span class=\"hljs-string\">&quot;foo&quot;</span>, <span class=\"hljs-string\">&quot;bar&quot;</span>, <span class=\"hljs-string\">&quot;baz&quot;</span>]\n\nitems.<span class=\"hljs-title function_\">map</span>(<span class=\"hljs-function\">(<span class=\"hljs-params\">item</span>) =&gt;</span> {\n  <span class=\"hljs-keyword\">return</span> item.<span class=\"hljs-title function_\">toUpperCase</span>()\n})\n\n<span class=\"hljs-comment\">// [&quot;FOO&quot;, &quot;BAR&quot;, &quot;BAZ&quot;]</span>\n</code></pre>\n<h4>PHP</h4>\n<pre><code class=\"hljs language-php\"><span class=\"hljs-variable\">$items</span> = [<span class=\"hljs-string\">&quot;foo&quot;</span>, <span class=\"hljs-string\">&quot;bar&quot;</span>, <span class=\"hljs-string\">&quot;baz&quot;</span>];\n\n<span class=\"hljs-title function_ invoke__\">array_map</span>(function(<span class=\"hljs-variable\">$item</span>) {\n    <span class=\"hljs-keyword\">return</span> <span class=\"hljs-title function_ invoke__\">strtoupper</span>(<span class=\"hljs-variable\">$item</span>);\n}, <span class=\"hljs-variable\">$items</span>);\n\n<span class=\"hljs-comment\">// [&quot;FOO&quot;, &quot;BAR&quot;, &quot;BAZ&quot;]</span>\n</code></pre>\n<h4>Elixir</h4>\n<pre><code class=\"hljs language-elixir\">items = [<span class=\"hljs-string\">&quot;foo&quot;</span>, <span class=\"hljs-string\">&quot;bar&quot;</span>, <span class=\"hljs-string\">&quot;baz&quot;</span>]\n\n<span class=\"hljs-title class_\">Enum</span>.map(items, <span class=\"hljs-keyword\">fn</span> item -&gt;\n  <span class=\"hljs-title class_\">String</span>.upcase(item)\n<span class=\"hljs-keyword\">end</span>)\n\n<span class=\"hljs-comment\"># [&quot;FOO&quot;, &quot;BAR&quot;, &quot;BAZ&quot;]</span>\n</code></pre>\n<h3>FizzBuzz time</h3>\n<p>Okay, now let&#x27;s combine some of what you&#x27;ve seen and add some more, and let&#x27;s do a FizzBuzz in Elixir.</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">defmodule</span> <span class=\"hljs-title\">FizzBuzz</span></span> <span class=\"hljs-keyword\">do</span>\n  <span class=\"hljs-variable\">@moduledoc</span> <span class=\"hljs-string\">&quot;&quot;&quot;\n  The classic. FizzBuzz!\n  &quot;&quot;&quot;</span>\n\n  <span class=\"hljs-comment\">@doc &quot;&quot;&quot;\n  Performs the classic FizzBuzz problem.\n\n  ## Example\n\n      iex&gt; FizzBuzz.fizz_buzz(15)\n      [&quot;1&quot;, &quot;2&quot;, &quot;Fizz&quot;, &quot;4&quot;, &quot;Buzz&quot;, &quot;Fizz&quot;, &quot;7&quot;, &quot;8&quot;, &quot;Fizz&quot;,\n       &quot;Buzz&quot;, &quot;11&quot;, &quot;Fizz&quot;, &quot;13&quot;, &quot;14&quot;, &quot;FizzBuzz&quot;]\n\n  &quot;&quot;&quot;</span>\n  <span class=\"hljs-variable\">@spec</span> fizz_buzz(pos_integer()) :: [<span class=\"hljs-title class_\">String</span>.t()]\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">fizz_buzz</span></span>(up_to) <span class=\"hljs-keyword\">do</span>\n    <span class=\"hljs-title class_\">Enum</span>.map(<span class=\"hljs-number\">1</span>..up_to, &amp;do_fizz_buzz/<span class=\"hljs-number\">1</span>)\n  <span class=\"hljs-keyword\">end</span>\n\n  <span class=\"hljs-function\"><span class=\"hljs-keyword\">defp</span> <span class=\"hljs-title\">do_fizz_buzz</span></span>(n) <span class=\"hljs-keyword\">do</span>\n    <span class=\"hljs-keyword\">case</span> {rem(n, <span class=\"hljs-number\">3</span>), rem(n, <span class=\"hljs-number\">5</span>)} <span class=\"hljs-keyword\">do</span>\n      {<span class=\"hljs-number\">0</span>, <span class=\"hljs-number\">0</span>} -&gt; <span class=\"hljs-string\">&quot;FizzBuzz&quot;</span>\n      {<span class=\"hljs-number\">0</span>, _} -&gt; <span class=\"hljs-string\">&quot;Fizz&quot;</span>\n      {_, <span class=\"hljs-number\">0</span>} -&gt; <span class=\"hljs-string\">&quot;Buzz&quot;</span>\n      {_, _} -&gt; to_string(n)\n    <span class=\"hljs-keyword\">end</span>\n  <span class=\"hljs-keyword\">end</span>\n<span class=\"hljs-keyword\">end</span>\n</code></pre>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-title class_\">FizzBuzz</span>.fizz_buzz(<span class=\"hljs-number\">15</span>)\n\n<span class=\"hljs-comment\"># Returns</span>\n[<span class=\"hljs-string\">&quot;1&quot;</span>, <span class=\"hljs-string\">&quot;2&quot;</span>, <span class=\"hljs-string\">&quot;Fizz&quot;</span>, <span class=\"hljs-string\">&quot;4&quot;</span>, <span class=\"hljs-string\">&quot;Buzz&quot;</span>, <span class=\"hljs-string\">&quot;Fizz&quot;</span>, <span class=\"hljs-string\">&quot;7&quot;</span>, <span class=\"hljs-string\">&quot;8&quot;</span>, <span class=\"hljs-string\">&quot;Fizz&quot;</span>, <span class=\"hljs-string\">&quot;Buzz&quot;</span>,\n <span class=\"hljs-string\">&quot;11&quot;</span>, <span class=\"hljs-string\">&quot;Fizz&quot;</span>, <span class=\"hljs-string\">&quot;13&quot;</span>, <span class=\"hljs-string\">&quot;14&quot;</span>, <span class=\"hljs-string\">&quot;FizzBuzz&quot;</span>]\n</code></pre>\n<ol>\n<li>You&#x27;ve seen modules before.</li>\n<li><code>@moduledoc</code> is documentation for the module. You use markdown syntax and these will format nicely in docs and in IEx help functions.</li>\n<li><code>@doc</code> is for documenting functions. Like <code>@moduledoc</code> you use markdown syntax. You can also use doctests when testing your modules and the example code will automatically be tested and asserted against the example results.</li>\n<li><code>&quot;&quot;&quot;</code> is for multiline strings.</li>\n<li>Introducing <code>@spec</code>. These are optional and define the types the function expects and returns and is used solely for documentation and static analysis tools (usually including LSP).</li>\n<li>There is also a range from <code>1</code> to <code>up_to</code>. A range is defined like <code>1..3</code> and this covers the integers <code>1</code> to <code>3</code>, inclusive. There is additional notation for <code>steps</code>, so ranges can be pretty cool, and they are also more memory efficient than a list of integers.</li>\n<li>The <code>&amp;do_fizz_buzz/1</code> is using the capture operator for anonymous function short-hand. It is equivalent to</li>\n</ol>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-keyword\">fn</span> n -&gt;\n  do_fizz_buzz(n)\n<span class=\"hljs-keyword\">end</span>\n</code></pre>\n<ol start=\"8\">\n<li>Next, you see a <code>case</code> statement which is similar to a <code>switch</code> statement in other languages, but on steroids because of pattern-matching.</li>\n<li>Finally, the <code>_</code> are how you tell Elixir that the variable is unused. So, because it&#x27;s a variable it will match any value and assign the value to that variable for the scope that it&#x27;s in, but the <code>_</code> also tells Elixir that the variable is unused and will be discarded.</li>\n</ol>\n<h3>Comprehensions</h3>\n<p>Comprehensions might have a similar feel to for loops (or for..in, or foreach) but they are not quite the same thing. Due to recursion and scope, for example, you can not iterate on a counter variable like you might be used to in non-functional languages.</p>\n<pre><code class=\"hljs language-elixir\">items = [<span class=\"hljs-string\">&quot;foo&quot;</span>, <span class=\"hljs-string\">&quot;bar&quot;</span>, <span class=\"hljs-string\">&quot;baz&quot;</span>]\n\ni = <span class=\"hljs-number\">0</span>\n\n<span class=\"hljs-keyword\">for</span> item &lt;- items <span class=\"hljs-keyword\">do</span>\n  i = i + <span class=\"hljs-number\">1</span>\n  {i, item}\n<span class=\"hljs-keyword\">end</span>\n\n<span class=\"hljs-comment\"># Returns</span>\n[\n  {<span class=\"hljs-number\">1</span>, <span class=\"hljs-string\">&quot;foo&quot;</span>},\n  {<span class=\"hljs-number\">1</span>, <span class=\"hljs-string\">&quot;bar&quot;</span>},\n  {<span class=\"hljs-number\">1</span>, <span class=\"hljs-string\">&quot;baz&quot;</span>}\n]\n\n<span class=\"hljs-comment\"># Notice `i` is not increasing?</span>\n</code></pre>\n<p>A comprehension takes one (or more) generators (e.g. <code>item &lt;- items</code>), conditionals (e.g. <code>item != &quot;foo&quot;</code>), and will return a list of the results of each execution.</p>\n<p>Here are a couple of examples to get an idea of how they can work:</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-keyword\">for</span> x &lt;- <span class=\"hljs-number\">0</span>..<span class=\"hljs-number\">10</span> <span class=\"hljs-keyword\">do</span>\n  x\n<span class=\"hljs-keyword\">end</span>\n\n<span class=\"hljs-comment\"># Returns</span>\n[<span class=\"hljs-number\">0</span>, <span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">3</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">5</span>, <span class=\"hljs-number\">6</span>, <span class=\"hljs-number\">7</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">9</span>, <span class=\"hljs-number\">10</span>]\n</code></pre>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-keyword\">for</span> x &lt;- <span class=\"hljs-number\">0</span>..<span class=\"hljs-number\">10</span>, rem(x, <span class=\"hljs-number\">2</span>) == <span class=\"hljs-number\">0</span> <span class=\"hljs-keyword\">do</span>\n  x\n<span class=\"hljs-keyword\">end</span>\n\n<span class=\"hljs-comment\"># Returns</span>\n[<span class=\"hljs-number\">0</span>, <span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">6</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">10</span>]\n</code></pre>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-keyword\">for</span> x &lt;- <span class=\"hljs-number\">0</span>..<span class=\"hljs-number\">10</span>, y &lt;- <span class=\"hljs-number\">1</span>..<span class=\"hljs-number\">3</span>, rem(x, <span class=\"hljs-number\">2</span>) == <span class=\"hljs-number\">1</span> <span class=\"hljs-keyword\">do</span>\n  {x, y}\n<span class=\"hljs-keyword\">end</span>\n\n<span class=\"hljs-comment\"># Returns</span>\n[\n  {<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">1</span>},\n  {<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">2</span>},\n  {<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">3</span>},\n  {<span class=\"hljs-number\">3</span>, <span class=\"hljs-number\">1</span>},\n  {<span class=\"hljs-number\">3</span>, <span class=\"hljs-number\">2</span>},\n  {<span class=\"hljs-number\">3</span>, <span class=\"hljs-number\">3</span>},\n  {<span class=\"hljs-number\">5</span>, <span class=\"hljs-number\">1</span>},\n  {<span class=\"hljs-number\">5</span>, <span class=\"hljs-number\">2</span>},\n  {<span class=\"hljs-number\">5</span>, <span class=\"hljs-number\">3</span>},\n  {<span class=\"hljs-number\">7</span>, <span class=\"hljs-number\">1</span>},\n  {<span class=\"hljs-number\">7</span>, <span class=\"hljs-number\">2</span>},\n  {<span class=\"hljs-number\">7</span>, <span class=\"hljs-number\">3</span>},\n  {<span class=\"hljs-number\">9</span>, <span class=\"hljs-number\">1</span>},\n  {<span class=\"hljs-number\">9</span>, <span class=\"hljs-number\">2</span>},\n  {<span class=\"hljs-number\">9</span>, <span class=\"hljs-number\">3</span>}\n]\n</code></pre>\n<p>There are other options you can use with comprehensions, and interesting ways to use them in other ways (like with binaries).</p>\n<p>I won&#x27;t really go further into this subject, since this is just an intro post. You will usually use functions, but there are\nsome cases where comprehensions are the most clear and concise way, so don&#x27;t underestimate them either.</p>\n<h2>Pipes</h2>\n<p>Pipes <code>|&gt;</code> might look odd at first, but they are actually very simple and very awesome.</p>\n<p>All that you really need to know is that they take the result of one function and pass it as the first argument into the next function. If you are familiar with unix, then you should understand the idea of a pipe operator, it&#x27;s the same idea.</p>\n<p>For example, you could refactor this:</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-title class_\">String</span>.upcase(<span class=\"hljs-string\">&quot;foo&quot;</span>)\n</code></pre>\n<p>into:</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-string\">&quot;foo&quot;</span> |&gt; <span class=\"hljs-title class_\">String</span>.upcase()\n</code></pre>\n<p>(don&#x27;t <em>actually</em> do this for a single function)</p>\n<p>The real beauty comes from the fact that you can chain pipes as much as you want. This gets rid of temporary variables and also improves readability significantly.</p>\n<p>Here is another example refactor where I will add some chained pipes. Let&#x27;s take this:</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">add_space_and_upcase</span></span>(str) <span class=\"hljs-keyword\">do</span>\n  split = <span class=\"hljs-title class_\">String</span>.codepoints(str)\n  spaced = <span class=\"hljs-title class_\">Enum</span>.join(split, <span class=\"hljs-string\">&quot; &quot;</span>)\n  <span class=\"hljs-title class_\">String</span>.upcase(spaced)\n<span class=\"hljs-keyword\">end</span>\n\nadd_space_and_upcase(<span class=\"hljs-string\">&quot;foobar&quot;</span>)  <span class=\"hljs-comment\"># &quot;F O O B A R&quot;</span>\n</code></pre>\n<p>and make it much better:</p>\n<pre><code class=\"hljs language-elixir\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title\">add_space_and_upcase</span></span>(str) <span class=\"hljs-keyword\">do</span>\n  str\n  |&gt; <span class=\"hljs-title class_\">String</span>.codepoints()\n  |&gt; <span class=\"hljs-title class_\">Enum</span>.join(<span class=\"hljs-string\">&quot; &quot;</span>)\n  |&gt; <span class=\"hljs-title class_\">String</span>.upcase()\n<span class=\"hljs-keyword\">end</span>\n\nadd_space_and_upcase(<span class=\"hljs-string\">&quot;foobar&quot;</span>)  <span class=\"hljs-comment\"># &quot;F O O B A R&quot;</span>\n</code></pre>\n<p>I think it&#x27;s clear to see how great this is.</p>\n<h2>Conclusion</h2>\n<p>Elixir clearly has some really great things going on, and we&#x27;re <em>not even</em> scratching the surface here, as I&#x27;ve mentioned nothing about processes or OTP.</p>\n<p>I hope however, that this has helped you to at least not have a mini-wtf-heartattack when you look at some elixir code and gets you interested to learn more and try it out.</p>\n<p>If you think I should include anything else, or have any critiques, please feel free to comment below.</p>",
            "url": "https://winchester.dev/posts/intro-to-elixir-for-non-ruby-programmers",
            "title": "Intro to Elixir for non Ruby programmers",
            "summary": "An introduction to Elixir from a PHP and JavaScript perspective.",
            "date_modified": "2017-05-08T00:00:00.000Z",
            "author": {
                "name": "Ryan Winchester"
            }
        }
    ]
}