<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Elixir - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Elixir - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 24 May 2026 16:30:38 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/elixir/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ An Animated Introduction to Elixir ]]>
                </title>
                <description>
                    <![CDATA[ Elixir is a dynamic, functional programming language designed for building scalable and maintainable applications. It leverages the battle-tested Erlang VM, known for running low-latency, distributed, and fault-tolerant systems. Elixir is based on an... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/an-animated-introduction-to-elixir/</link>
                <guid isPermaLink="false">682f44e3dcb21c615cf1a12d</guid>
                
                    <category>
                        <![CDATA[ Elixir ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Functional Programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Mark Mahoney ]]>
                </dc:creator>
                <pubDate>Thu, 22 May 2025 15:38:11 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1747923637406/e7ad8796-6269-4260-b500-226e445140ce.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p><a target="_blank" href="https://elixir-lang.org/">Elixir</a> is a dynamic, functional programming language designed for building scalable and maintainable applications. It leverages the battle-tested Erlang VM, known for running low-latency, distributed, and fault-tolerant systems.</p>
<p>Elixir is based on another language called <a target="_blank" href="https://www.erlang.org/">Erlang</a>. Erlang was developed by Ericsson in the 1980s for telecom applications requiring extreme reliability and availability. It includes built-in support for concurrency, distribution, and fault-tolerance. Elixir, created by José Valim, brings a more approachable and expressive syntax to the Erlang VM. It lowers the barrier to entry for using Erlang's powerful features.</p>
<p>In Elixir, functions are the primary building blocks of programs, similar to how classes and methods are the core units in object-oriented languages. But instead of modeling behavior through stateful objects, functional languages like Elixir treat computation as a series of pure functions that take input and produce output without side effects.</p>
<p>This paradigm offers several benefits:</p>
<ul>
<li><p>Immutability: Data is immutable by default. Once a variable is bound, it can't be changed. This avoids hard to track bugs caused by side effects.</p>
</li>
<li><p>Functions as first-class citizens: Functions can be assigned to variables, passed as arguments, and returned from other functions. This enables powerful abstractions and code reuse.</p>
</li>
<li><p>Pattern matching: Elixir uses pattern matching to bind variables, unpack data structures, and control program flow. This leads to concise and declarative code.</p>
</li>
<li><p>Recursion: Looping is typically achieved through recursion. Elixir optimizes recursive calls to avoid stack overflow issues.</p>
</li>
</ul>
<p>While functional programming requires a shift in thinking, it can lead to more predictable and maintainable systems. Elixir makes this paradigm friendly and accessible.</p>
<p>One of Elixir's standout features is its concurrency model. It uses lightweight processes to achieve massive scalability:</p>
<ul>
<li><p>Processes are isolated and share no memory, communicating only via message passing.</p>
</li>
<li><p>The Erlang VM can run millions of processes concurrently on a single machine.</p>
</li>
<li><p>Fault-tolerance is achieved by supervising and restarting failed processes.</p>
</li>
</ul>
<p>This architecture enables building distributed, real-time systems that efficiently use modern multi-core hardware.</p>
<h2 id="heading-an-animated-introduction-to-elixir"><strong>An Animated Introduction to Elixir</strong></h2>
<p>To make Elixir's functional and concurrent nature more approachable, I developed an interactive tutorial called "An Animated Introduction to Elixir". It uses annotated code playbacks to walk through key language features step-by-step. From basic syntax to advanced topics like concurrency, each concept is explained through code and accompanying visuals.</p>
<p>You can access the free 'book' of code playbacks here: <a target="_blank" href="https://playbackpress.com/books/exbook">https://playbackpress.com/books/exbook</a>.</p>
<p>For more info about code playbacks, you can watch a short demo:</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/uYbHqCNjVDM" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p> </p>
<p>Part 1 of the book focuses on core Elixir – syntax basics, pattern matching, functions and modules, key data structures like tuples, maps, lists, functional concepts like closures, recursion, enumeration, and efficient immutability.</p>
<ul>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/1">1.1 Hello Elixir!!!</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/2">1.2 Numbers and the Match Operator</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/3">1.3 Functions and More Matching</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/4">1.4 Modules and More Matching with SimpleMath</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/5">1.5 Closures</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/6">1.6 Ranges and the Enum Module</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/7">1.7 Tuples</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/8">1.8 Maps</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/9">1.9 SimpleDateFormatter Module with Maps</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/10">1.10 Lists, Matching, and Recursion</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/11">1.11 Poker Probabilities</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/1/12">1.12 Recursion in Elixir</a></p>
</li>
</ul>
<p>Part 2 explores Elixir's concurrency model – working with processes, message passing between processes, dividing work across processes, and real-world examples and benchmarking. The concepts are applied to practical problems like estimating poker probabilities and generating calendars.</p>
<ul>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/2/1">2.1 Adding Tests to the Mix</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/2/2">2.2 Process Basics</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/2/3">2.3 Prime Sieve</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/2/4">2.4 Calendar with Processes</a></p>
</li>
<li><p><a target="_blank" href="https://playbackpress.com/books/exbook/chapter/2/5">2.5 Poker with Processes</a></p>
</li>
</ul>
<h2 id="heading-why-learn-elixir">Why Learn Elixir?</h2>
<p>Learning Elixir is beneficial for programmers for several compelling reasons. Elixir's functional paradigm and immutable data structures promote writing cleaner, more predictable, and maintainable code.</p>
<p>Its actor-based concurrency model, built on the robust Erlang VM, enables developing highly scalable, fault-tolerant, and distributed systems that can efficiently leverage multi-core processors and handle massive numbers of simultaneous users. Also, Elixir has a friendly, expressive syntax that lowers the barrier to entry for using these powerful features.</p>
<p>Finally, Elixir has a rapidly growing, vibrant community and ecosystem. For example, the Elixir ecosystem includes powerful web frameworks like <a target="_blank" href="https://www.phoenixframework.org/">Phoenix</a> for building scalable web applications, <a target="_blank" href="https://nerves-project.org/">Nerves</a> for creating embedded software for devices, and <a target="_blank" href="https://hexdocs.pm/ecto/Ecto.html">Ecto</a> for writing database queries and interacting with different databases.</p>
<p>If you have any questions or feedback, I'd love to hear it. Comments and feedback are welcome anytime: <a target="_blank" href="mailto:mark@playbackpress.com">mark@playbackpress.com</a>.</p>
<p>If you'd like to support my work and help keep Playback Press free for all, consider donating using <a target="_blank" href="https://github.com/sponsors/markm208">GitHub Sponsors</a>. I use all of the donations for hosting costs. Your support helps me continue creating educational content like this. Thank you!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Discover the Power of Functional Programming with Elixir ]]>
                </title>
                <description>
                    <![CDATA[ Transform your development approach and embrace the power of Elixir and functional programming with our new beginner's course. It is designed to help you build scalable, maintainable applications like never before. We just published a new course on t... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/functional-programming-with-elixir/</link>
                <guid isPermaLink="false">66b20288a8b92c932923644f</guid>
                
                    <category>
                        <![CDATA[ Elixir ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Wed, 10 May 2023 15:59:36 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/05/elixir.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Transform your development approach and embrace the power of Elixir and functional programming with our new beginner's course. It is designed to help you build scalable, maintainable applications like never before.</p>
<p>We just published a new course on the freeCodeCamp channel that will teach you how to build your own Elixir applications and apply functional programming concepts in your projects.</p>
<p>Octallium developed this course. He is an experienced developer and has shared other popular courses for the freeCodeCamp channel.</p>
<p>Elixir, a dynamic, functional programming language built on the Erlang Virtual Machine (BEAM), offers remarkable concurrency, fault-tolerance, and distributed computing capabilities. Its functional programming paradigm makes it an ideal choice for scalable and maintainable applications. This course will help you gain a solid understanding of Elixir and its underlying principles, setting you on the path to becoming a proficient developer.</p>
<p>The course covers a wide range of topics, starting with an introduction to Elixir and functional programming. You'll learn about recursion and its importance in functional programming, as well as how to install Elixir and set up Visual Studio Code for a seamless development experience.</p>
<p>As you progress, you'll explore pattern matching and the powerful LiveBook, which allows you to create interactive and collaborative notebooks in Elixir. You'll also gain an in-depth understanding of immutability and the Actor Model, both of which are fundamental to Elixir's design.</p>
<p>Diving into Elixir's rich data types, you'll learn about atoms, strings, charlists, processes, lists, tuples, keyword lists, maps, and structs. You'll also discover how to effectively control the flow of your Elixir applications, and create Mix projects to organize your code.</p>
<p>The course emphasizes the importance of recursion, and you'll get hands-on experience with head and tail recursion, as well as practical examples like summing digits, calculating factorials, and reversing numbers. You'll also delve into list operations, including summing, reversing, mapping, concatenating, and flat mapping.</p>
<p>To reinforce your learning, our course includes a mid-review, practical projects like the "Seven Wonders" and "Expense Manager," and a final assessment of your understanding of Elixir and functional programming.</p>
<p>So, are you prepared to unlock the full potential of Elixir and functional programming? Watch the full course <a target="_blank" href="https://www.youtube.com/watch?v=IiIgm_yaoOA">on the freeCodeCamp.org YouTube channel</a> (5-hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/IiIgm_yaoOA" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Why Elixir uses linked lists ]]>
                </title>
                <description>
                    <![CDATA[ By Edison Yap I’ve always thought data structures are cool, but you know what’s cooler? Seeing them in the wild! While going through Elixir’s docs, I saw that Elixir uses linked lists under the hood for their linear data structures. I thought this wa... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/elixir-why-linked-lists-aa6828b6b099/</link>
                <guid isPermaLink="false">66c349b84f7405e6476b01a1</guid>
                
                    <category>
                        <![CDATA[ Computer Science ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Elixir ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Functional Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 13 Feb 2019 03:12:13 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*L8v2RUaCgJ2O1YiWqQTGwQ.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Edison Yap</p>
<p>I’ve always thought data structures are cool, but you know what’s cooler? Seeing them in the wild!</p>
<p>While going through Elixir’s docs, I saw that Elixir uses linked lists under the hood for their linear data structures. I thought this was cool, but something struck me; I understood arrays and linked lists, but I had no idea how they related to programming languages. It’s bothered me ever since, and I <em>needed</em> to find out <em>why</em> linked lists were used…hence this article!</p>
<p>So back to the article: from what I’ve found so far, there are three reasons as to why Elixir does this (and I could be totally wrong, feel free to correct me!). Let’s go through one by one:</p>
<h3 id="heading-immutable-data">Immutable Data</h3>
<p>In Elixir (most functional languages actually), data are immutable. This is an important first step to understanding why linked lists are used, so let’s explore this.</p>
<blockquote>
<p><em>Immutable means that once data is declared, it</em> cannot <em>be mutated/changed anymore.</em></p>
</blockquote>
<p>Assuming you know how arrays work under the hood (check out <a target="_blank" href="https://dev.to/edisonywh/how-arrays-work-the-way-arrays-work-3bpg">my other article</a> if you want a refresher), let’s take a look at what happens if we try to implement immutability with an array!</p>
<p>An array is defined as a continuous block of memory. The problem with this is that an array of 5 elements is still just ONE array, and when we are adding/deleting an element, we are MUTATING it. How can we use immutability with arrays then? It’s not impossible, but let’s look at why that’s not practical.</p>
<p>If we want to enforce true immutability in arrays, this means that we need to make a full copy of the old array every time we want to add/delete in the array.</p>
<p>Which means, if you have an array of size 5, if you want to add a new item to the array, your memory usage is instantly doubled (because you need to keep the old array as is, and you also need to make a new array of the same elements). And that’s just space complexity — there’s also time complexity that we need to think about!</p>
<p>A linked list doesn’t have the same constraints, as the nodes are all stored separately in memory, which means we don’t really need to worry about space/time complexity while adding/deleting nodes in the list.</p>
<p>This gives us our first reason as to why it uses a list, however that’s not the whole story. Here’s where recursive structural/tail sharing jumps in and everything starts making sense.</p>
<h3 id="heading-recursive-structure">Recursive structure</h3>
<p>Did you notice that linked lists are recursive by definition?</p>
<p>For example, <code>A -&gt; B -&gt;</code> C -&gt; D is a linked list, b<code>ut so is B</code> -&amp;<code>gt; C</code> -&gt; D, C -&gt; D and so on, and each link<em>ed</em> list is just a sub structure of another linked list!</p>
<p>Well that wasn’t very exciting on its own, but this is vital to the next piece of puzzle!</p>
<blockquote>
<p>Fun Fact: The recursive nature coupled with the fact that data has to be immutable (so you can’t have a loop counter) is why functional languages are usually associated with recursion — they kinda have to!</p>
</blockquote>
<h3 id="heading-structuraltail-sharing">Structural/Tail Sharing</h3>
<p>So, we know linked lists are recursive in nature. Combined with the immutable nature of the language, we know that the data can never change.</p>
<p>This is interesting, because now we can confidently say that <code>A -&gt; B -&gt;</code> C -&gt; D is a different l<code>ist from B</code> -&gt; C -&gt; D (even though one recursively contains the other one). Because we have that guarantee (along with the fact that a list CAN'T change), we don't have to define the same d<strong>ata twice, and we can reuse existin</strong>g linked lists! <strong>This is called Str</strong>uctural sharing.</p>
<p>Awesome isn’t it? Let’s look at an example.</p>
<p>e.g:</p>
<p>Now we have THREE different lists! <code>list</code>, <code>list_one</code> and <code>list_two</code>, but all of them share the same reference (the tail) and the only difference between them is the head pointer.</p>
<p>This means that there will be a total of 6 elements in memory. Adding to list has low memory cost, while retaining the immutability that we desire.</p>
<p>Reusable baby!</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*b31hiO4ynbDLRrXWEFF4aQ.png" alt="Image" width="700" height="400" loading="lazy"></p>
<p>If you want to read a little more, you can look into <a target="_blank" href="https://en.wikipedia.org/wiki/Trie">Trie trees</a> which have the exact same concepts of sharing datas/prefixes!</p>
<h3 id="heading-garbage-collection-amp-caching">Garbage Collection &amp; Caching?</h3>
<p>These two I’m not quite sure about, but I’ve heard that linked lists are good for GCs and that tail sharing makes a good candidate for locality of reference/caching (I don’t get how, because they aren’t stored in the same places). Would appreciate if someone wants to chime in!</p>
<h3 id="heading-closing-note">Closing Note</h3>
<p>Side note: in actuality, it’s not as much about Elixir since it compiles down to Erlang. But it’s also not much about Erlang, because all functional programming does pretty much same thing…but this is what prompted my curiosity, hence the ties to Elixir.</p>
<p>While writing this article, I found that I had to write in depth on how arrays work before I was able to dive into the Elixir part, so I’ve published that as <a target="_blank" href="https://dev.to/edisonywh/how-arrays-work-the-way-arrays-work-3bpg">another article over here</a> instead. Do read that to gain a better understanding on what the tradeoff is!</p>
<p><a target="_blank" href="https://dev.to/edisonywh/how-arrays-work-the-way-arrays-work-3bpg"><strong>How arrays work the way arrays work</strong></a><br><a target="_blank" href="https://dev.to/edisonywh/how-arrays-work-the-way-arrays-work-3bpg">_In computer science, there is the concept of linear data structure, which means that the datas are structured in a…_dev.to</a></p>
<p>I also did not really talk about Big O notation, because I felt it might add unnecessary reading <em>time</em> and <em>complexity</em> to the article, but it’s pretty vital and fundamental to computer science, so I suggest you brush up a little on it.</p>
<p>If you’re a podcast kind-of person, there’s the <a target="_blank" href="https://www.codenewbie.org/basecs">BaseCS</a> by CodeNewbie, co-hosted by Vaidehi Joshi and Saron.</p>
<p>If you want to read though, Vaidehi Joshi’s blogpost version (which is what inspired the podcast, I believe) is great too on <a target="_blank" href="https://medium.com/basecs">BaseCS Medium</a>.</p>
<p>As for video, <a target="_blank" href="https://www.youtube.com/playlist?list=PL2_aWCzGMAwI3W_JlcBbtYTwiQSsOTa6P">MyCodeSchool</a> is beyond amazing and is practically where I learned everything that I know now, highly recommended!</p>
<p>Other than that, hope you all enjoyed the article as much as I enjoyed writing it!</p>
<h3 id="heading-sources">Sources</h3>
<p><a target="_blank" href="https://elixir-lang.org/getting-started/basic-types.html#linked-lists">https://elixir-lang.org/getting-started/basic-types.html#linked-lists</a> — The piece that prompted this article</p>
<p><em>Originally posted on <a target="_blank" href="https://dev.to/edisonywh/-elixir--why-linked-lists--1e9d">dev.to</a></em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to build a distributed Game of Life in Elixir ]]>
                </title>
                <description>
                    <![CDATA[ By Artur Trzop I wrote my first game in Elixir. It is a popular thing — Conway’s Game of Life — but it gets quite interesting when you solve it in a functional language, especially when you can see how the actor model works and how actors are distrib... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-a-distributed-game-of-life-in-elixir-9152588100cd/</link>
                <guid isPermaLink="false">66c34f6939769b84d9fe96de</guid>
                
                    <category>
                        <![CDATA[ Elixir ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Erlang ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Testing ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 17 Jan 2019 17:14:44 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/0*AfpCKR3o8T1Ie-Eq.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Artur Trzop</p>
<p>I wrote my first game in Elixir. It is a popular thing — Conway’s Game of Life — but it gets quite interesting when you solve it in a functional language, especially when you can see <a target="_blank" href="https://en.wikipedia.org/wiki/Actor_model">how the actor model works</a> and how actors are distributed across servers in the network.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/3Snl82uCosun4TAqGggHfSKVUng0YPUzFheF" alt="Image" width="400" height="274" loading="lazy">
<em>Game of Life</em></p>
<p><strong>In this blog post I am going to show:</strong></p>
<ul>
<li>how to write rules for the game of life with tests in Elixir,</li>
<li>parallel tasks across lightweight processes (actors) in order to utilize all CPU cores,</li>
<li>how to distribute work across nodes so the game can be executed by many servers in the cluster,</li>
<li>how to use GenServer behaviour, TaskSupervisor and Agents in Elixir.</li>
</ul>
<p>This project and the full <a target="_blank" href="https://github.com/BeyondScheme/elixir-game_of_life">source code can be found here</a>.</p>
<h3 id="heading-demo">Demo</h3>
<p>Let’s start with watching quick demo of how the game works.</p>
<p><a target="_blank" href="https://asciinema.org/a/44233"><strong>GameOfLife</strong></a><br><a target="_blank" href="https://asciinema.org/a/44233">_Recorded by ArturT_asciinema.org</a></p>
<p>As you can see, node1 represents running game and board on the screen. The second node was also started and connected to the first one. From the second node, we added new cells to the board. Both nodes are responsible for processing the game, but only the first node is a primary with information about the current state of the game. We can connect more nodes to the cluster so game processing can happen on all of the nodes. You are going to learn in this article how to make it happen.</p>
<h3 id="heading-game-of-life-rules">Game of Life rules</h3>
<p>If you already know about <a target="_blank" href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">the game of life problem</a> just jump to <a target="_blank" href="https://beyondscheme.com/2016/distributed-game-of-life-in-elixir#create-new-application-in-elixir">the next header</a>. If not, in this section you can learn the basic concept.</p>
<p>The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:</p>
<ul>
<li>Any live cell with fewer than two live neighbours dies as if caused by under-population.</li>
<li>Any live cell with two or three live neighbours lives on to the next generation.</li>
<li>Any live cell with more than three live neighbours dies, as if by over-population.</li>
<li>Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.</li>
</ul>
<p>The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed — births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.</p>
<h3 id="heading-create-new-application-in-elixir">Create new application in Elixir</h3>
<p>First things first, so we are going to create a new Elixir OTP application with supervision tree. We will use supervisor for our game server, you will learn more about it a bit later.</p>
<pre><code>$ mix <span class="hljs-keyword">new</span> --sup game_of_life
</code></pre><p>A <code>--sup</code> option is given to generate an OTP application skeleton including a supervision tree. Normally an app is generated without a supervisor and without the app callback.</p>
<p>In <code>lib/game_of_life.ex</code> file you will find an example of how to add child worker to supervisor.</p>
<pre><code># lib/game_of_life.exdefmodule GameOfLife <span class="hljs-keyword">do</span>  use Application  # See http:<span class="hljs-comment">//elixir-lang.org/docs/stable/elixir/Application.html  # for more information on OTP Applications  def start(_type, _args) do    import Supervisor.Spec, warn: false    children = [      # Define workers and child supervisors to be supervised      # worker(GameOfLife.Worker, [arg1, arg2, arg3]),    ]    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html    # for other strategies and supported options    opts = [strategy: :one_for_one, name: GameOfLife.Supervisor]    Supervisor.start_link(children, opts)  endend</span>
</code></pre><h3 id="heading-represent-the-board-in-game-of-life">Represent the board in Game of Life</h3>
<p>We need to represent the alive cells on the board in our game. A single cell can be a tuple <code>{x, y}</code> with coordinates in the 2-dimensional board.</p>
<p>All alive cells on the board will be on the list <code>alive_cells</code>.</p>
<pre><code>alive_cells = [{<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">-1</span>,<span class="hljs-number">-2</span>}]
</code></pre><p>Here is an example of how this board with alive cells looks like:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/j2utgQIL1-t1HTIR9smp5jEtsfDIbsOkS1ZE" alt="Image" width="600" height="450" loading="lazy">
<em>Board with alive cells</em></p>
<p>and here are proper x &amp; y coordinates:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/FNNnkZazIb8gyyR0vg5F9R4kiXVKAUXDUx9D" alt="Image" width="600" height="450" loading="lazy">
<em>Coordinates of alive cells</em></p>
<p>Now when we have the idea of how we are going to store our alive cells we can jump to write some code.</p>
<h3 id="heading-game-of-life-rules-with-tests">Game of Life rules with tests</h3>
<p>We can create <code>GameOfLife.Cell</code> module with function <code>keep_alive?/2</code> responsible for determining if a particular alive cell <code>{x, y}</code> should be still alive on the next generation or not.</p>
<p>Here is the function with expected arguments:</p>
<pre><code># lib/game_of_life/cell.exdefmodule GameOfLife.Cell <span class="hljs-keyword">do</span>  def keep_alive?(alive_cells, {x, y} = _alive_cell) <span class="hljs-keyword">do</span>    # TODO  endend
</code></pre><p>Let’s write some tests to cover the first of the requirements of the Game of Life:</p>
<blockquote>
<p>Any live cell with fewer than two live neighbours dies, as if caused by under-population.</p>
</blockquote>
<p>We wrote tests to ensure <code>GameOfLife.Cell.keep_alive?/2</code> function returns false in a case when the alive cell has no neighbours or has just one.</p>
<pre><code># test/game_of_life/cell_test.exsdefmodule GameOfLife.CellTest <span class="hljs-keyword">do</span>  use ExUnit.Case, <span class="hljs-attr">async</span>: <span class="hljs-literal">true</span>  test <span class="hljs-string">"alive cell with no neighbours dies"</span> <span class="hljs-keyword">do</span>    alive_cell = {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}    alive_cells = [alive_cell]    refute GameOfLife.Cell.keep_alive?(alive_cells, alive_cell)  end  test <span class="hljs-string">"alive cell with 1 neighbour dies"</span> <span class="hljs-keyword">do</span>    alive_cell = {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}    alive_cells = [alive_cell, {<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}]    refute GameOfLife.Cell.keep_alive?(alive_cells, alive_cell)  endend
</code></pre><p><code>GameOfLife.Cell.keep_alive?/2</code> function needs to return false just to pass our tests so let’s add more tests to cover other requirements.</p>
<blockquote>
<p>Any live cell with more than three live neighbours dies, as if by over-population.</p>
</blockquote>
<pre><code># test/game_of_life/cell_test.exstest <span class="hljs-string">"alive cell with more than 3 neighbours dies"</span> <span class="hljs-keyword">do</span>  alive_cell = {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}  alive_cells = [alive_cell, {<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">1</span>}]  refute GameOfLife.Cell.keep_alive?(alive_cells, alive_cell)end
</code></pre><blockquote>
<p><em>Any live cell with two or three live neighbours lives on to the next generation.</em></p>
</blockquote>
<pre><code># test/game_of_life/cell_test.exstest <span class="hljs-string">"alive cell with 2 neighbours lives"</span> <span class="hljs-keyword">do</span>  alive_cell = {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}  alive_cells = [alive_cell, {<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}]  assert GameOfLife.Cell.keep_alive?(alive_cells, alive_cell)endtest <span class="hljs-string">"alive cell with 3 neighbours lives"</span> <span class="hljs-keyword">do</span>  alive_cell = {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}  alive_cells = [alive_cell, {<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">1</span>}]  assert GameOfLife.Cell.keep_alive?(alive_cells, alive_cell)end
</code></pre><p>Now, we can implement our <code>GameOfLife.Cell.keep_alive?/2</code> function.</p>
<pre><code># lib/game_of_life/cell.exdefmodule GameOfLife.Cell <span class="hljs-keyword">do</span>  def keep_alive?(alive_cells, {x, y} = _alive_cell) <span class="hljs-keyword">do</span>    <span class="hljs-keyword">case</span> count_neighbours(alive_cells, x, y, <span class="hljs-number">0</span>) <span class="hljs-keyword">do</span>      <span class="hljs-number">2</span> -&amp;gt; <span class="hljs-literal">true</span>      <span class="hljs-number">3</span> -&gt; <span class="hljs-literal">true</span>      _ -&gt; <span class="hljs-literal">false</span>    end  end  defp count_neighbours([head_cell | tail_cells], x, y, count) <span class="hljs-keyword">do</span>    increment = <span class="hljs-keyword">case</span> head_cell <span class="hljs-keyword">do</span>      {hx, hy} when hx == x - <span class="hljs-number">1</span> and hy == y - <span class="hljs-number">1</span> -&gt; <span class="hljs-number">1</span>      {hx, hy} when hx == x     and hy == y - <span class="hljs-number">1</span> -&gt; <span class="hljs-number">1</span>      {hx, hy} when hx == x + <span class="hljs-number">1</span> and hy == y - <span class="hljs-number">1</span> -&gt; <span class="hljs-number">1</span>      {hx, hy} when hx == x - <span class="hljs-number">1</span> and hy == y     -&amp;gt; <span class="hljs-number">1</span>      {hx, hy} when hx == x + <span class="hljs-number">1</span> and hy == y     -&gt;; <span class="hljs-number">1</span>      {hx, hy} when hx == x - <span class="hljs-number">1</span> and hy == y + <span class="hljs-number">1</span> -&gt;; <span class="hljs-number">1</span>      {hx, hy} when hx == x     and hy == y + <span class="hljs-number">1</span> -&amp;gt; <span class="hljs-number">1</span>      {hx, hy} when hx == x + <span class="hljs-number">1</span> and hy == y + <span class="hljs-number">1</span> -&gt; <span class="hljs-number">1</span>      _not_neighbour -&gt; <span class="hljs-number">0</span>    end    count_neighbours(tail_cells, x, y, count + increment)  end  defp count_neighbours([], _x, _y, count), <span class="hljs-attr">do</span>: countend
</code></pre><p>As you can see, we implemented the private function <code>count_neighbours/4</code> responsible for counting neighbours. It will be helpful to meet the next rule.</p>
<p>There is one more requirement we forgot about:</p>
<blockquote>
<p>Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.</p>
</blockquote>
<p>We are going to write a new function <code>GameOfLife.Cell.become_alive?/2</code> expecting the coordinates of the dead cell and returning if the dead cell should become alive or not.</p>
<pre><code># lib/game_of_life/cell.exdefmodule GameOfLife.Cell <span class="hljs-keyword">do</span>  def become_alive?(alive_cells, {x, y} = _dead_cell) <span class="hljs-keyword">do</span>    <span class="hljs-number">3</span> == count_neighbours(alive_cells, x, y, <span class="hljs-number">0</span>)  endend
</code></pre><p>And here is the test for that:</p>
<pre><code># test/game_of_life/cell_test.exstest <span class="hljs-string">"dead cell with three live neighbours becomes a live cell"</span> <span class="hljs-keyword">do</span>  alive_cells = [{<span class="hljs-number">2</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">1</span>}]  dead_cell = {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}  assert GameOfLife.Cell.become_alive?(alive_cells, dead_cell)endtest <span class="hljs-string">"dead cell with two live neighbours stays dead"</span> <span class="hljs-keyword">do</span>  alive_cells = [{<span class="hljs-number">2</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}]  dead_cell = {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}  refute GameOfLife.Cell.become_alive?(alive_cells, dead_cell)end
</code></pre><p>There is one more thing which might be helpful for us. We have the list of alive cells but we don’t know much about the dead cells. The number of dead cells is infinite so we need to cut down the number of dead cells for which we want to check if they should become alive. The simple way would be to check only dead cells with alive neighbours. Hence the <code>GameOfLife.Cell.dead_neighbours/1</code>function.</p>
<p>Let’s write some tests first:</p>
<pre><code># test/game_of_life/cell_test.exstest <span class="hljs-string">"find dead cells (neighbours of alive cell)"</span> <span class="hljs-keyword">do</span>  alive_cells = [{<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}]  dead_neighbours = GameOfLife.Cell.dead_neighbours(alive_cells) |&amp;gt; Enum.sort  expected_dead_neighbours = [    {<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">0</span>},    {<span class="hljs-number">0</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">1</span>},    {<span class="hljs-number">0</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">2</span>}  ] |&gt;; Enum.sort  assert dead_neighbours == expected_dead_neighboursendtest <span class="hljs-string">"find dead cells (neighbours of alive cells)"</span> <span class="hljs-keyword">do</span>  alive_cells = [{<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">1</span>}]  dead_neighbours = GameOfLife.Cell.dead_neighbours(alive_cells) |&amp;gt; Enum.sort  expected_dead_neighbours = [    {<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">3</span>, <span class="hljs-number">0</span>},    {<span class="hljs-number">0</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">3</span>, <span class="hljs-number">1</span>},    {<span class="hljs-number">0</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">3</span>, <span class="hljs-number">2</span>}  ] |&gt; Enum.sort  assert dead_neighbours == expected_dead_neighboursend
</code></pre><p>and here is the implemented function:</p>
<pre><code># lib/game_of_life/cell.exdef dead_neighbours(alive_cells) <span class="hljs-keyword">do</span>  neighbours = neighbours(alive_cells, [])  (neighbours |&amp;gt; Enum.uniq) -- alive_cellsenddefp neighbours([{x, y} | cells], neighbours) <span class="hljs-keyword">do</span>  neighbours(cells, neighbours ++ [    {x - <span class="hljs-number">1</span>, y - <span class="hljs-number">1</span>}, {x    , y - <span class="hljs-number">1</span>}, {x + <span class="hljs-number">1</span>, y - <span class="hljs-number">1</span>},    {x - <span class="hljs-number">1</span>, y    }, {x + <span class="hljs-number">1</span>, y    },    {x - <span class="hljs-number">1</span>, y + <span class="hljs-number">1</span>}, {x    , y + <span class="hljs-number">1</span>}, {x + <span class="hljs-number">1</span>, y + <span class="hljs-number">1</span>}  ])enddefp neighbours([], neighbours), <span class="hljs-attr">do</span>: neighbours
</code></pre><p>Basically, these are all rules implemented in the single module <code>GameOfLife.Cell</code>. You can see the whole <a target="_blank" href="https://github.com/BeyondScheme/elixir-game_of_life/blob/master/lib/game_of_life/cell.ex">module file</a> with <a target="_blank" href="https://github.com/BeyondScheme/elixir-game_of_life/blob/master/test/game_of_life/cell_test.exs">tests on GitHub</a>.</p>
<h3 id="heading-the-architecture-of-distributed-game-of-life">The architecture of distributed Game of Life</h3>
<p><img src="https://cdn-media-1.freecodecamp.org/images/BIrpLZBjGimv4-HlBQQt9UslBgNwam3Pr0bC" alt="Image" width="600" height="450" loading="lazy">
<em>The architecture of distributed Game of Life in Elixir</em></p>
<p>Our main supervisor is <code>GameOfLife.Supervisor</code> which I mentioned at the beginning of the article. Below you can see how we defined its children like <code>Task.Supervisor</code>, workers for <code>BoardServer</code> and <code>GamePrinter</code>.</p>
<pre><code># lib/game_of_life.exdefmodule GameOfLife <span class="hljs-keyword">do</span>  use Application  # See http:<span class="hljs-comment">//elixir-lang.org/docs/stable/elixir/Application.html  # for more information on OTP Applications  def start(_type, _args) do    import Supervisor.Spec, warn: false    init_alive_cells = []    children = [      # Define workers and child supervisors to be supervised      # worker(GameOfLife.Worker, [arg1, arg2, arg3]),      supervisor(Task.Supervisor, [[name: GameOfLife.TaskSupervisor]]),      worker(GameOfLife.BoardServer, [init_alive_cells]),      # We will uncomment this line later      # worker(GameOfLife.GamePrinter, []),    ]    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html    # for other strategies and supported options    opts = [strategy: :one_for_one, name: GameOfLife.Supervisor]    Supervisor.start_link(children, opts)  endend</span>
</code></pre><p>Let me describe what each component on the image is responsible for.</p>
<p><code>Task.Supervisor</code> is an Elixir module defining a new supervisor which can be used to dynamically supervise tasks. We are going to use it to spin off tasks like determining if the particular cell should live or die. Those tasks can be run across nodes connected into the cluster.</p>
<p>In the above code, we gave the name <code>GameOfLife.TaskSupervisor</code> for our supervisor. We will use this name to tell the <code>Task.Supervisor.async</code> function which Task Supervisor should handle our task. You can read more about <a target="_blank" href="http://elixir-lang.org/docs/stable/elixir/Task.Supervisor.html">Task.Supervisor here</a>.</p>
<p><code>GameOfLife.BoardServer</code> is our module implemented as <a target="_blank" href="http://elixir-lang.org/docs/stable/elixir/GenServer.html">GenServer behaviour</a>. It is responsible for holding the state of the game. By that I mean it keeps the list of alive cells on the board along with generation counter and TRef. TRef is a timer reference returned by the <a target="_blank" href="http://erlang.org/doc/man/timer.html">Erlang timer module</a> and the <a target="_blank" href="http://erlang.org/doc/man/timer.html#apply_interval-4">apply_interval</a> function.</p>
<p>We want to start the game and generate a new list of alive cells for the next generation with a specified time interval. With each new generation, we will update the generation counter. The other interesting thing is that <code>GameOfLife.BoardServer</code> is running only on a single node. Once another node is connected to the cluster where is already running <code>GameOfLife.BoardServer</code> then <code>GameOfLife.BoardServer</code> won’t be started just like that on the newly connected node.</p>
<p>Instead on the new node <code>GameOfLife.BoardServer</code> will keep the only reference to the PID of the process existing on the first node. We want to have the single source of truth about the state of our game in one primary <code>GameOfLife.BoardServer</code> process existing on the first node started in the cluster.</p>
<p><code>GameOfLife.GamePrinter</code> is a simple module using <a target="_blank" href="http://elixir-lang.org/docs/stable/elixir/Agent.html">Agent</a> in order to keep TRef (time reference) so we can print the board to STDOUT with the specified interval. We will use <a target="_blank" href="http://erlang.org/doc/man/timer.html#apply_interval-4">Erlang timer module</a> to print the board on the screen every second.</p>
<p>You may wonder what’s the difference between GenServer and Agent.</p>
<p>A GenServer is a process like any other Elixir process and it can be used to keep state, execute code asynchronously, and so on. The advantage of using a generic server process (GenServer) is that it will have a standard set of interface functions and include functionality for tracing and error reporting. It also fits into a supervision tree as this is what we did in the <code>GameOfLife</code> module.</p>
<p>On the other hand, Agent is a much simpler solution than GenServer. Agents are a simple abstraction around state. Often in Elixir there is a need to share or store state that must be accessed from different processes or by the same process at different points in time. The Agent module provides a basic server implementation that allows state to be retrieved and updated via a simple API. This is what we are going to do in <code>GameOfLife.GamePrinter</code> as we only need to keep time reference to our timer interval.</p>
<h3 id="heading-create-node-manager-for-task-supervisor">Create node manager for task supervisor</h3>
<p>Let’s start with something simple just to see if we can distribute work across nodes in the cluster. We assume each new process created by task supervisor will be assigned randomly to one of the connected nodes. Each node should be equally overloaded with the assumption that each task is pretty similar and all nodes are machines with the same configuration and overload.</p>
<pre><code># lib/game_of_life/node_manager.exdefmodule GameOfLife.NodeManager <span class="hljs-keyword">do</span>  def all_nodes <span class="hljs-keyword">do</span>    [Node.self | Node.list]  end  def random_node <span class="hljs-keyword">do</span>    all_nodes |&amp;gt; Enum.random  endend
</code></pre><p>Our node manager has <code>random_node/0</code> function which returns the name of a random node connected to the cluster. Basically, that’s it. A simple solution should be enough for now.</p>
<h3 id="heading-create-board-helper-functions">Create board helper functions</h3>
<p>We need some helper functions for operations we can do on the board like adding and removing cells. Let’s start with tests for the module <code>GameOfLife.Board</code> and function <code>add_cells/2</code>.</p>
<pre><code># test/game_of_life/board_test.exsdefmodule GameOfLife.BoardTest <span class="hljs-keyword">do</span>  use ExUnit.Case, <span class="hljs-attr">async</span>: <span class="hljs-literal">true</span>  test <span class="hljs-string">"add new cells to alive cells without duplicates"</span> <span class="hljs-keyword">do</span>    alive_cells = [{<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">2</span>}]    new_cells = [{<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}]    actual_alive_cells = GameOfLife.Board.add_cells(alive_cells, new_cells)                          |&amp;gt; Enum.sort    expected_alive_cells = [{<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">2</span>}]    assert actual_alive_cells == expected_alive_cells  endend
</code></pre><p>We need to ensure we won’t allow adding the same cell twice to the board so we test that there are no duplicates. Here is the implementation for <code>add_cells/2</code> function:</p>
<pre><code># lib/game_of_life/board.exdefmodule GameOfLife.Board <span class="hljs-keyword">do</span>  def add_cells(alive_cells, new_cells) <span class="hljs-keyword">do</span>    alive_cells ++ new_cells    |&amp;gt; Enum.uniq  endend
</code></pre><p>Another thing is removing cells from the list of alive cells:</p>
<pre><code># test/game_of_life/board_test.exstest <span class="hljs-string">"remove cells which must be killed from alive cells"</span> <span class="hljs-keyword">do</span>  alive_cells = [{<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">4</span>, <span class="hljs-number">-2</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">1</span>}]  kill_cells = [{<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">2</span>}]  actual_alive_cells = GameOfLife.Board.remove_cells(alive_cells, kill_cells)  expected_alive_cells = [{<span class="hljs-number">4</span>, <span class="hljs-number">-2</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">1</span>}]  assert actual_alive_cells == expected_alive_cellsend
</code></pre><p>Implementation is super simple:</p>
<pre><code># lib/game_of_life/board.exdef remove_cells(alive_cells, kill_cells) <span class="hljs-keyword">do</span>  alive_cells -- kill_cellsend
</code></pre><p>Let’s create something more advanced. We should determine which cells should still live in the next generation after the tick. Here is a test for the <code>GameOfLife.Board.keep_alive_tick/1</code> function:</p>
<pre><code># test/game_of_life/board_test.exstest <span class="hljs-string">"alive cell with 2 neighbours lives on to the next generation"</span> <span class="hljs-keyword">do</span>  alive_cells = [{<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">0</span>}]  expected_alive_cells = [{<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}]  assert GameOfLife.Board.keep_alive_tick(alive_cells) == expected_alive_cellsend
</code></pre><p>The function <code>keep_alive_tick</code> does a few things like creating a new task with <code>Task.Supervisor</code> for each alive cell. Tasks will be created across available nodes in the cluster. We calculate if alive cells should stay alive or be removed. The <code>keep_alive_or_nilify/2</code> function returns if the cell should live or <code>nil</code> otherwise.</p>
<p>We wait with <code>Task.await/1</code> until all tasks across all nodes finished their work. Tasks are working in parallel but we need to wait for results from each task. We remove from the list the <code>nil</code> values so at the end we end up with only alive cells for the next generation.</p>
<pre><code># lib/game_of_life/board.ex@doc <span class="hljs-string">"Returns cells that should still live on the next generation"</span>def keep_alive_tick(alive_cells) <span class="hljs-keyword">do</span>  alive_cells  |&amp;gt; Enum.map(&amp;(Task.Supervisor.async(                {GameOfLife.TaskSupervisor, GameOfLife.NodeManager.random_node},                GameOfLife.Board, :keep_alive_or_nilify, [alive_cells, &amp;<span class="hljs-number">1</span>])))  |&gt;; Enum.map(&amp;Task.await/<span class="hljs-number">1</span>)  |&gt; remove_nil_cellsenddef keep_alive_or_nilify(alive_cells, cell) <span class="hljs-keyword">do</span>  <span class="hljs-keyword">if</span> GameOfLife.Cell.keep_alive?(alive_cells, cell), <span class="hljs-attr">do</span>: cell, <span class="hljs-attr">else</span>: nilenddefp remove_nil_cells(cells) <span class="hljs-keyword">do</span>  cells  |&gt; Enum.filter(fn cell -&amp;gt; cell != nil end)end
</code></pre><p>There is one more case we should handle which is a situation when dead cells should become alive. <code>GameOfLife.Board.become_alive_tick/1</code> function will be responsible for that.</p>
<pre><code># test/game_of_life/board_test.exstest <span class="hljs-string">"dead cell with three live neighbours becomes a live cell"</span> <span class="hljs-keyword">do</span>  alive_cells = [{<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}]  born_cells = GameOfLife.Board.become_alive_tick(alive_cells)  expected_born_cells = [{<span class="hljs-number">1</span>, <span class="hljs-number">-1</span>}, {<span class="hljs-number">0</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">1</span>}]  assert born_cells == expected_born_cellsend
</code></pre><p>This is how our function looks like:</p>
<pre><code># lib/game_of_life/board.ex@doc <span class="hljs-string">"Returns new born cells on the next generation"</span>def become_alive_tick(alive_cells) <span class="hljs-keyword">do</span>  GameOfLife.Cell.dead_neighbours(alive_cells)  |&amp;gt; Enum.map(&amp;(Task.Supervisor.async(                {GameOfLife.TaskSupervisor, GameOfLife.NodeManager.random_node},                GameOfLife.Board, :become_alive_or_nilify, [alive_cells, &amp;<span class="hljs-number">1</span>])))  |&gt;; Enum.map(&amp;Task.await/<span class="hljs-number">1</span>)  |&gt; remove_nil_cellsenddef become_alive_or_nilify(alive_cells, dead_cell) <span class="hljs-keyword">do</span>  <span class="hljs-keyword">if</span> GameOfLife.Cell.become_alive?(alive_cells, dead_cell), <span class="hljs-attr">do</span>: dead_cell, <span class="hljs-attr">else</span>: nilend
</code></pre><p>It works similarly to <code>GameOfLife.Board.keep_alive_tick/1</code>. First, we are looking for the dead neighbours of alive cells. Then for each dead cell we create a new process across the nodes in the cluster to determine if the dead cell should become alive in the next generation.</p>
<p>You can see the full source code of <a target="_blank" href="https://github.com/BeyondScheme/elixir-game_of_life/blob/master/lib/game_of_life/board.ex">GameOfLife.Board module</a> and <a target="_blank" href="https://github.com/BeyondScheme/elixir-game_of_life/blob/master/test/game_of_life/board_test.exs">tests on github</a>.</p>
<h3 id="heading-create-boardserver">Create BoardServer</h3>
<p>Let’s create <code>GameOfLife.BoardServer</code> generic server behaviour. We define a public interface for the server.</p>
<pre><code># lib/game_of_life/board_server.exdefmodule GameOfLife.BoardServer <span class="hljs-keyword">do</span>  use GenServer  <span class="hljs-built_in">require</span> Logger  @name {:<span class="hljs-built_in">global</span>, __MODULE__}  @game_speed <span class="hljs-number">1000</span> # miliseconds  # Client  def start_link(alive_cells) <span class="hljs-keyword">do</span>    <span class="hljs-keyword">case</span> GenServer.start_link(__MODULE__, {alive_cells, nil, <span class="hljs-number">0</span>}, <span class="hljs-attr">name</span>: @name) <span class="hljs-keyword">do</span>      {:ok, pid} -&amp;gt;        Logger.info <span class="hljs-string">"Started #{__MODULE__} master"</span>        {:ok, pid}      {:error, {:already_started, pid}} -&gt;        Logger.info <span class="hljs-string">"Started #{__MODULE__} slave"</span>        {:ok, pid}    end  end  def alive_cells <span class="hljs-keyword">do</span>    GenServer.call(@name, :alive_cells)  end  def generation_counter <span class="hljs-keyword">do</span>    GenServer.call(@name, :generation_counter)  end  def state <span class="hljs-keyword">do</span>    GenServer.call(@name, :state)  end  @doc <span class="hljs-string">""</span><span class="hljs-string">"  Clears board and adds only new cells.  Generation counter is reset.  "</span><span class="hljs-string">""</span>  def set_alive_cells(cells) <span class="hljs-keyword">do</span>    GenServer.call(@name, {:set_alive_cells, cells})  end  def add_cells(cells) <span class="hljs-keyword">do</span>    GenServer.call(@name, {:add_cells, cells})  end  def tick <span class="hljs-keyword">do</span>    GenServer.cast(@name, :tick)  end  def start_game(speed \\ @game_speed) <span class="hljs-keyword">do</span>    GenServer.call(@name, {:start_game, speed})  end  def stop_game <span class="hljs-keyword">do</span>    GenServer.call(@name, :stop_game)  end  def change_speed(speed) <span class="hljs-keyword">do</span>    stop_game    start_game(speed)  endend
</code></pre><p>As you can see, we use <code>GenServer</code> behaviour in our module. The module requires also Logger as we would like to print some info to the STDOUT.</p>
<p>In the <code>start_link/1</code> function we start a new <code>GenServer</code>. When our generic server starts, it is as a first process in the cluster. Then it becomes the primary process. In the case when there is already a running process with a globally registered name <code>{:global,__MODULE__}</code>, we log info that our process will be a replica process with a reference to the existing PID on another node in the cluster.</p>
<p>We store the global name for our server in the attribute <code>@name</code>. We use another attribute <code>@game_speed</code> for default game speed which is 1000 milliseconds.</p>
<p>In our public interface, we have the <code>alive_cells/1</code> function which returns the list of alive cells. Basically, it is the current state of the game (alive cells on the board). This function calls <code>GenServer</code> with the registered <code>@name</code> and requests <code>:alive_cells</code>. We need to implement the <code>handle_call/3</code> function for this type of request (<code>:alive_cells</code>).</p>
<p>There is another public function <code>generation_counter/1</code> which returns how many generations were already processed by the board server.</p>
<p>The <code>state/1</code> function returns state that is held by our generic server. The state is represented as the tuple with 3 values like alive cells, TRef (time reference - we want to regenerate board every second) and generation counter. TRef is a very internal thing for the board server so we won’t return this to the outside world. That’s why we will return just alive cells and the generation counter. You will see it later in the implementation for <code>handle_call(:state, _from, state)</code>.</p>
<p>You can use the <code>set_alive_cells/1</code> function in the case when you want to override the current list of alive cells with a new list.</p>
<p>The <code>add_cells/1</code> function will be very useful as we want to be able to add new cells or figures to the board. For instance, we may want to add a blinker pattern to the existing game. You will learn more about patterns later.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/SGbjJ1XyyVP1F7u7KDrJ2mT74d6J3KBxuhw1" alt="Image" width="82" height="82" loading="lazy">
<em>blinker pattern</em></p>
<p>We can manually force the game to calculate the next generation of cells with the <code>tick/1</code> function.</p>
<p>The <code>start_game/1</code> function is responsible for starting a new timer which calls every second a <code>tick/1</code> function. Thanks to that our game will update the list of alive cells within a specified interval which is <code>@game_speed</code>.</p>
<p>The last 2 functions are <code>stop_game/1</code> and <code>change_speed/1</code> which just restart the game and start a new one with the provided speed.</p>
<p>Now you can see how the above functions are working. They are calling server callbacks.</p>
<pre><code># lib/game_of_life/board_server.exdefmodule GameOfLife.BoardServer <span class="hljs-keyword">do</span>  use GenServer  # ...  # Server (callbacks)  def handle_call(:alive_cells, _from, {alive_cells, _tref, _generation_counter} = state) <span class="hljs-keyword">do</span>    {:reply, alive_cells, state}  end  def handle_call(:generation_counter, _from, {_alive_cells, _tref, generation_counter} = state) <span class="hljs-keyword">do</span>    {:reply, generation_counter, state}  end  def handle_call(:state, _from, {alive_cells, _tref, generation_counter} = state) <span class="hljs-keyword">do</span>    {:reply, {alive_cells, generation_counter}, state}  end  def handle_call({:set_alive_cells, cells}, _from, {_alive_cells, tref, _generation_counter}) <span class="hljs-keyword">do</span>    {:reply, cells, {cells, tref, <span class="hljs-number">0</span>}}  end  def handle_call({:add_cells, cells}, _from, {alive_cells, tref, generation_counter}) <span class="hljs-keyword">do</span>    alive_cells = GameOfLife.Board.add_cells(alive_cells, cells)    {:reply, alive_cells, {alive_cells, tref, generation_counter}}  end  def handle_call({:start_game, speed}, _from, {alive_cells, nil = _tref, generation_counter}) <span class="hljs-keyword">do</span>    {:ok, tref} = :timer.apply_interval(speed, __MODULE__, :tick, [])    {:reply, :game_started, {alive_cells, tref, generation_counter}}  end  def handle_call({:start_game, _speed}, _from, {_alive_cells, _tref, _generation_counter} = state) <span class="hljs-keyword">do</span>    {:reply, :game_already_running, state}  end  def handle_call(:stop_game, _from, {_alive_cells, nil = _tref, _generation_counter} = state) <span class="hljs-keyword">do</span>    {:reply, :game_not_running, state}  end  def handle_call(:stop_game, _from, {alive_cells, tref, generation_counter}) <span class="hljs-keyword">do</span>    {:ok, :cancel} = :timer.cancel(tref)    {:reply, :game_stoped, {alive_cells, nil, generation_counter}}  end  def handle_cast(:tick, {alive_cells, tref, generation_counter}) <span class="hljs-keyword">do</span>    keep_alive_task = Task.Supervisor.async(                      {GameOfLife.TaskSupervisor, GameOfLife.NodeManager.random_node},                      GameOfLife.Board, :keep_alive_tick, [alive_cells])    become_alive_task = Task.Supervisor.async(                        {GameOfLife.TaskSupervisor, GameOfLife.NodeManager.random_node},                        GameOfLife.Board, :become_alive_tick, [alive_cells])    keep_alive_cells = Task.await(keep_alive_task)    born_cells = Task.await(become_alive_task)    alive_cells = keep_alive_cells ++ born_cells    {:noreply, {alive_cells, tref, generation_counter + <span class="hljs-number">1</span>}}  endend
</code></pre><p>Oh, we forgot about tests. In this case, we can use <a target="_blank" href="http://elixir-lang.org/docs/stable/ex_unit/ExUnit.DocTest.html">DocTest</a>. It allows us to generate tests from the code examples existing in a module/function/macro’s documentation.</p>
<p>Our test file is super short:</p>
<pre><code># test/game_of_life/board_server_test.exsdefmodule GameOfLife.BoardServerTest <span class="hljs-keyword">do</span>  use ExUnit.Case  doctest GameOfLife.BoardServerend
</code></pre><p>Let’s add <code>@moduledoc</code> to <code>GameOfLife.BoardServer</code>.</p>
<pre><code># lib/game_of_life/board_server.exdefmodule GameOfLife.BoardServer <span class="hljs-keyword">do</span>  use GenServer  <span class="hljs-built_in">require</span> Logger  @moduledoc <span class="hljs-string">""</span><span class="hljs-string">"  ## Example      iex&gt; GameOfLife.BoardServer.start_game      :game_started      iex&gt; GameOfLife.BoardServer.start_game      :game_already_running      iex&gt; GameOfLife.BoardServer.stop_game      :game_stoped      iex&gt; GameOfLife.BoardServer.stop_game      :game_not_running      iex&gt; GameOfLife.BoardServer.change_speed(500)      :game_started      iex&gt; GameOfLife.BoardServer.stop_game      :game_stoped      iex&gt; GameOfLife.BoardServer.set_alive_cells([{0, 0}])      [{0, 0}]      iex&gt; GameOfLife.BoardServer.alive_cells      [{0, 0}]      iex&gt; GameOfLife.BoardServer.add_cells([{0, 1}])      [{0, 0}, {0, 1}]      iex&gt; GameOfLife.BoardServer.alive_cells      [{0, 0}, {0, 1}]      iex&gt; GameOfLife.BoardServer.state      {[{0, 0}, {0, 1}], 0}      iex&gt; GameOfLife.BoardServer.generation_counter      0      iex&gt; GameOfLife.BoardServer.tick      :ok      iex&gt; GameOfLife.BoardServer.generation_counter      1      iex&gt; GameOfLife.BoardServer.state      {[], 1}  "</span><span class="hljs-string">""</span>end
</code></pre><p>As you can see we have grouped 3 examples in the <code>@moduledoc</code> attribute and they are separated by a new line. When you run tests you will see 3 separate tests.</p>
<pre><code>$ mix test test/game_of_life/board_server_test.exsCompiled lib/game_of_life/board_server.ex20:<span class="hljs-number">54</span>:<span class="hljs-number">30.637</span> [info]  Started Elixir.GameOfLife.BoardServer master...Finished <span class="hljs-keyword">in</span> <span class="hljs-number">0.1</span> seconds (<span class="hljs-number">0.1</span>s on load, <span class="hljs-number">0.00</span>s on tests)<span class="hljs-number">3</span> tests, <span class="hljs-number">0</span> failuresRandomized <span class="hljs-keyword">with</span> seed <span class="hljs-number">791637</span>
</code></pre><p>In <code>GameOfLife.BoardServer</code> you probably noticed 2 interesting things. First is <code>GameOfLife.Board</code> which is called in:</p>
<pre><code># lib/game_of_life/board_server.exdef handle_call({:add_cells, cells}, _from, {alive_cells, tref, generation_counter}) <span class="hljs-keyword">do</span>  alive_cells = GameOfLife.Board.add_cells(alive_cells, cells)  {:reply, alive_cells, {alive_cells, tref, generation_counter}}end
</code></pre><p>As you saw before we added some useful functions to <code>GameOfLife.Board</code> module which helps us to do operations on the list of alive cells.</p>
<p>Another interesting thing is how we use <code>Task.Supervisor</code> in:</p>
<pre><code># lib/game_of_life/board_server.exdef handle_cast(:tick, {alive_cells, tref, generation_counter}) <span class="hljs-keyword">do</span>    keep_alive_task = Task.Supervisor.async(                      {GameOfLife.TaskSupervisor, GameOfLife.NodeManager.random_node},                      GameOfLife.Board, :keep_alive_tick, [alive_cells])    become_alive_task = Task.Supervisor.async(                        {GameOfLife.TaskSupervisor, GameOfLife.NodeManager.random_node},                        GameOfLife.Board, :become_alive_tick, [alive_cells])    keep_alive_cells = Task.await(keep_alive_task)    born_cells = Task.await(become_alive_task)    alive_cells = keep_alive_cells ++ born_cells    {:noreply, {alive_cells, tref, generation_counter + <span class="hljs-number">1</span>}}  end
</code></pre><p>What we are doing here is spinning off a new async process to run the <code>GameOfLife.keep_alive_tick/1</code> function with the argument <code>alive_cells</code>.</p>
<pre><code># lib/game_of_life/board_server.exkeep_alive_task = Task.Supervisor.async(                  {GameOfLife.TaskSupervisor, GameOfLife.NodeManager.random_node},                  GameOfLife.Board, :keep_alive_tick, [alive_cells])
</code></pre><p>The tuple <code>{GameOfLife.TaskSupervisor, GameOfLife.NodeManager.random_node}</code> tells <code>Task.Supervisor</code> that we want to use task supervisor with the name <code>GameOfLife.TaskSupervisor</code> and we want to run the process on the node returned by the <code>GameOfLife.NodeManager.random_node</code> function.</p>
<h3 id="heading-create-game-printer-and-console-presenter">Create game printer and console presenter</h3>
<p><code>GameOfLife.GamePrinter</code> module is running as a worker under the supervision of the <code>GameOfLife</code> supervisor. <code>GameOfLife.GamePrinter</code> is using <code>Agent</code> to store <code>TRef</code> for timer reference as we want to print the board to the STDOUT with the specified interval.</p>
<p>You have already seen the example of using <code>Agent</code> so this shouldn’t be new for you. Basically, we wrote the public interface to start and stop printing the board to the screen. For tests we used <a target="_blank" href="http://elixir-lang.org/docs/stable/ex_unit/ExUnit.DocTest.html">DocTest</a>.</p>
<pre><code># lib/game_of_life/game_printer.exdefmodule GameOfLife.GamePrinter <span class="hljs-keyword">do</span>  @moduledoc <span class="hljs-string">""</span><span class="hljs-string">"  ## Example      iex&gt; GameOfLife.GamePrinter.start_printing_board      :printing_started      iex&gt; GameOfLife.GamePrinter.start_printing_board      :already_printing      iex&gt; GameOfLife.GamePrinter.stop_printing_board      :printing_stopped      iex&gt; GameOfLife.GamePrinter.stop_printing_board      :already_stopped  "</span><span class="hljs-string">""</span>  @print_speed <span class="hljs-number">1000</span>  def start_link <span class="hljs-keyword">do</span>    Agent.start_link(fn -&gt; nil end, <span class="hljs-attr">name</span>: __MODULE__)  end  def start_printing_board <span class="hljs-keyword">do</span>    Agent.get_and_update(__MODULE__, __MODULE__, :do_start_printing_board, [])  end  def do_start_printing_board(nil = _tref) <span class="hljs-keyword">do</span>    {:ok, tref} = :timer.apply_interval(@print_speed, __MODULE__, :print_board, [])    {:printing_started, tref}  end  def do_start_printing_board(tref), <span class="hljs-attr">do</span>: {:already_printing, tref}  def print_board <span class="hljs-keyword">do</span>    {alive_cells, generation_counter} = GameOfLife.BoardServer.state    alive_counter = alive_cells |&gt; Enum.count    GameOfLife.Presenters.Console.print(alive_cells, generation_counter, alive_counter)  end  def stop_printing_board <span class="hljs-keyword">do</span>    Agent.get_and_update(__MODULE__, __MODULE__, :do_stop_printing_board, [])  end  def do_stop_printing_board(nil = _tref), <span class="hljs-attr">do</span>: {:already_stopped, nil}  def do_stop_printing_board(tref) <span class="hljs-keyword">do</span>    {:ok, :cancel} = :timer.cancel(tref)    {:printing_stopped, nil}  endend
</code></pre><p><code>GameOfLife.Presenters.Console</code> is responsible for printing the board nicely with X &amp; Y axes, the number of alive cells, and the generation counter. Let’s start with tests. We are going to capture STDOUT and compare if data printed to the screen are looking as we expect.</p>
<pre><code># test/game_of_life/presenters/console_test.exsdefmodule GameOfLife.Presenters.ConsoleTest <span class="hljs-keyword">do</span>  use ExUnit.Case  # allows to capture stuff sent to stdout  <span class="hljs-keyword">import</span> ExUnit.CaptureIO  test <span class="hljs-string">"print cells on the console output"</span> <span class="hljs-keyword">do</span>    cell_outside_of_board = {<span class="hljs-number">-1</span>, <span class="hljs-number">-1</span>}    cells = [{<span class="hljs-number">0</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">2</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">0</span>, <span class="hljs-number">2</span>}, cell_outside_of_board]    result = capture_io fn -&amp;gt;      GameOfLife.Presenters.Console.print(cells, <span class="hljs-number">123</span>, <span class="hljs-number">6</span>, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>)    end    assert result == (    <span class="hljs-string">"    2| O,,\n"</span> &lt;&gt;    <span class="hljs-string">"    1| ,O,\n"</span> &lt;&gt;    <span class="hljs-string">"    0| OOO\n"</span> &lt;&gt;    <span class="hljs-string">"     | _ _ \n"</span> &lt;;&amp;gt;    <span class="hljs-string">"    /  0    \n"</span> &amp;lt;&gt;    <span class="hljs-string">"Generation: 123\n"</span> &amp;lt;&amp;gt;    <span class="hljs-string">"Alive cells: 6\n"</span>    )  endend
</code></pre><p>Here is the implementation of our print function:</p>
<pre><code># lib/game_of_life/presenters/<span class="hljs-built_in">console</span>.exdefmodule GameOfLife.Presenters.Console <span class="hljs-keyword">do</span>  @doc <span class="hljs-string">""</span><span class="hljs-string">"  Print cells to the console output.  Board is visible only for specified size for x and y.  Start x and y are in top left corner of the board.  `x_padding` Must be a prime number. Every x divided by the prime number  will be visible on x axis.  `y_padding` Any number. Padding for numbers on y axis.  "</span><span class="hljs-string">""</span>  def print(cells, generation_counter, alive_counter, start_x \\ <span class="hljs-number">-10</span>, start_y \\ <span class="hljs-number">15</span>, x_size \\ <span class="hljs-number">60</span>,            y_size \\ <span class="hljs-number">20</span>, x_padding \\ <span class="hljs-number">5</span>, y_padding \\ <span class="hljs-number">5</span>) <span class="hljs-keyword">do</span>    end_x = start_x + x_size    end_y = start_y - y_size    x_range = start_x..end_x    y_range = start_y..end_y    <span class="hljs-keyword">for</span> y &amp;lt;- y_range, x &lt;- x_range <span class="hljs-keyword">do</span>      # draw y axis      <span class="hljs-keyword">if</span> x == start_x <span class="hljs-keyword">do</span>        (y        |&gt;; Integer.to_string        |&gt; <span class="hljs-built_in">String</span>.rjust(y_padding)) &lt;&amp;gt; <span class="hljs-string">"| "</span>        |&amp;gt; IO.write      end      IO.write(<span class="hljs-keyword">if</span> Enum.member?(cells, {x, y}), <span class="hljs-attr">do</span>: <span class="hljs-string">"O"</span>, <span class="hljs-attr">else</span>: <span class="hljs-string">","</span>)      <span class="hljs-keyword">if</span> x == end_x, <span class="hljs-attr">do</span>: IO.puts <span class="hljs-string">""</span>    end    # draw x axis    IO.write <span class="hljs-built_in">String</span>.rjust(<span class="hljs-string">"| "</span>, y_padding + <span class="hljs-number">2</span>)    x_length = (round((end_x-start_x)/<span class="hljs-number">2</span>))    <span class="hljs-keyword">for</span> x &amp;lt;- <span class="hljs-number">0.</span>.x_length, <span class="hljs-attr">do</span>: IO.write <span class="hljs-string">"_ "</span>    IO.puts <span class="hljs-string">""</span>    IO.write <span class="hljs-built_in">String</span>.rjust(<span class="hljs-string">"/  "</span>, y_padding + <span class="hljs-number">2</span>)    <span class="hljs-keyword">for</span> x &lt;- x_range <span class="hljs-keyword">do</span>      <span class="hljs-keyword">if</span> rem(x, x_padding) == <span class="hljs-number">0</span> <span class="hljs-keyword">do</span>        x        |&amp;gt; Integer.to_string        |&gt; <span class="hljs-built_in">String</span>.ljust(x_padding)        |&amp;gt; IO.write      end    end    IO.puts <span class="hljs-string">""</span>    IO.puts <span class="hljs-string">"Generation: #{generation_counter}"</span>    IO.puts <span class="hljs-string">"Alive cells: #{alive_counter}"</span>  endend
</code></pre><p>The board with the bigger visible part looks like this:</p>
<pre><code>   <span class="hljs-number">15</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,   <span class="hljs-number">14</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,   <span class="hljs-number">13</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,   <span class="hljs-number">12</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,   <span class="hljs-number">11</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,   <span class="hljs-number">10</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    <span class="hljs-number">9</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    <span class="hljs-number">8</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    <span class="hljs-number">7</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    <span class="hljs-number">6</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    <span class="hljs-number">5</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    <span class="hljs-number">4</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    <span class="hljs-number">3</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    <span class="hljs-number">2</span>| ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    <span class="hljs-number">1</span>| ,,,,,,,,,,OO,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,    <span class="hljs-number">0</span>| ,,,,,,,,,,OO,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,   <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">-10</span>  <span class="hljs-number">-5</span>   <span class="hljs-number">0</span>    <span class="hljs-number">5</span>    <span class="hljs-number">10</span>   <span class="hljs-number">15</span>   <span class="hljs-number">20</span>   <span class="hljs-number">25</span>   <span class="hljs-number">30</span>   <span class="hljs-number">35</span>   <span class="hljs-number">40</span>   <span class="hljs-number">45</span>   <span class="hljs-number">50</span>Generation: <span class="hljs-number">18</span>Alive cells: <span class="hljs-number">4</span>
</code></pre><p>Last step is to uncomment the <code>GameOfLife.GamePrinter</code> worker in:</p>
<pre><code># lib/game_of_life.exdefmodule GameOfLife <span class="hljs-keyword">do</span>  use Application  # See http:<span class="hljs-comment">//elixir-lang.org/docs/stable/elixir/Application.html  # for more information on OTP Applications  def start(_type, _args) do    import Supervisor.Spec, warn: false    init_alive_cells = []    children = [      # Define workers and child supervisors to be supervised      # worker(GameOfLife.Worker, [arg1, arg2, arg3]),      supervisor(Task.Supervisor, [[name: GameOfLife.TaskSupervisor]]),      worker(GameOfLife.BoardServer, [init_alive_cells]),      # This line is uncommented now      worker(GameOfLife.GamePrinter, []),    ]    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html    # for other strategies and supported options    opts = [strategy: :one_for_one, name: GameOfLife.Supervisor]    Supervisor.start_link(children, opts)  endend</span>
</code></pre><h3 id="heading-add-figure-patterns-and-place-them-on-the-board">Add figure patterns and place them on the board</h3>
<p>To play our game of life, it would be great to have an easy way to add figures to the board. There are many commonly known patterns like still lifes, oscillators, and spaceships. You can <a target="_blank" href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life#Examples_of_patterns">learn more about them here</a>.</p>
<p>One interesting pattern is the gun. The Gosper Glider Gun is a very popular pattern. Here it is how it looks:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/wxl-EjJX2Bn1aQ82Mp6p0hhFLMUnSjwpMyHy" alt="Image" width="610" height="178" loading="lazy">
<em>Gosper Glider Gun</em></p>
<p>When you run game the pattern behaves as you see. The gun is shooting.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/ROo3D4AEbeI2atC5gYBi3Snfk1-TiAsDmMdk" alt="Image" width="250" height="180" loading="lazy">
<em>Gosper Glider Gun pattern live cycle</em></p>
<p>Let’s write this pattern down. Imagine you want to put the pattern in a rectangle. Left bottom corner of the rectangle is at <code>{0,0}</code> position.</p>
<pre><code># lib/game_of_life/patterns/guns.exdefmodule GameOfLife.Patterns.Guns <span class="hljs-keyword">do</span>  @moduledoc <span class="hljs-string">""</span><span class="hljs-string">"  https://en.wikipedia.org/wiki/Gun_(cellular_automaton)  "</span><span class="hljs-string">""</span>  @doc <span class="hljs-string">""</span><span class="hljs-string">"  https://en.wikipedia.org/wiki/File:Game_of_life_glider_gun.svg  "</span><span class="hljs-string">""</span>  def gosper_glider <span class="hljs-keyword">do</span>    [      {<span class="hljs-number">24</span>, <span class="hljs-number">8</span>},      {<span class="hljs-number">22</span>, <span class="hljs-number">7</span>}, {<span class="hljs-number">24</span>, <span class="hljs-number">7</span>},      {<span class="hljs-number">12</span>, <span class="hljs-number">6</span>}, {<span class="hljs-number">13</span>, <span class="hljs-number">6</span>}, {<span class="hljs-number">20</span>, <span class="hljs-number">6</span>}, {<span class="hljs-number">21</span>, <span class="hljs-number">6</span>}, {<span class="hljs-number">34</span>, <span class="hljs-number">6</span>}, {<span class="hljs-number">35</span>, <span class="hljs-number">6</span>},      {<span class="hljs-number">11</span>, <span class="hljs-number">5</span>}, {<span class="hljs-number">15</span>, <span class="hljs-number">5</span>}, {<span class="hljs-number">20</span>, <span class="hljs-number">5</span>}, {<span class="hljs-number">21</span>, <span class="hljs-number">5</span>}, {<span class="hljs-number">34</span>, <span class="hljs-number">5</span>}, {<span class="hljs-number">35</span>, <span class="hljs-number">5</span>},      {<span class="hljs-number">0</span>, <span class="hljs-number">4</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">4</span>}, {<span class="hljs-number">10</span>, <span class="hljs-number">4</span>}, {<span class="hljs-number">16</span>, <span class="hljs-number">4</span>}, {<span class="hljs-number">20</span>, <span class="hljs-number">4</span>}, {<span class="hljs-number">21</span>, <span class="hljs-number">4</span>},      {<span class="hljs-number">0</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">10</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">14</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">16</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">17</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">22</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">24</span>, <span class="hljs-number">3</span>},      {<span class="hljs-number">10</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">16</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">24</span>, <span class="hljs-number">2</span>},      {<span class="hljs-number">11</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">15</span>, <span class="hljs-number">1</span>},      {<span class="hljs-number">12</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">13</span>, <span class="hljs-number">0</span>},    ]  endend
</code></pre><p>It would be also useful if we could place the pattern on the board in the position specified by us. Let’s write a pattern converter.</p>
<pre><code># lib/game_of_life/pattern_converter.exdefmodule GameOfLife.PatternConverter <span class="hljs-keyword">do</span>  @doc <span class="hljs-string">""</span><span class="hljs-string">"  ## Example      iex&gt; GameOfLife.PatternConverter.transit([{0, 0}, {1, 3}], -1, 2)      [{-1, 2}, {0, 5}]  "</span><span class="hljs-string">""</span>  def transit([{x, y} | cells], x_padding, y_padding) <span class="hljs-keyword">do</span>    [{x + x_padding, y + y_padding} | transit(cells, x_padding, y_padding)]  end  def transit([], _x_padding, _y_padding), <span class="hljs-attr">do</span>: []end
</code></pre><p>This is the way you can add the Gosper glider pattern to the board with the specified position.</p>
<pre><code>GameOfLife.Patterns.Guns.gosper_glider|&amp;gt; GameOfLife.PatternConverter.transit(<span class="hljs-number">-2</span>, <span class="hljs-number">-3</span>)|&gt; GameOfLife.BoardServer.add_cells
</code></pre><p>You can find <a target="_blank" href="https://github.com/BeyondScheme/elixir-game_of_life/tree/master/lib/game_of_life/patterns">more patterns in modules here</a>.</p>
<h3 id="heading-run-game-across-multiple-nodes">Run game across multiple nodes</h3>
<p>Now it is time to run our game. The full <a target="_blank" href="https://github.com/BeyondScheme/elixir-game_of_life">source code can be found here</a>.</p>
<p>Let’s run the first node where the <code>GameOfLife.BoardServer</code> will be running.</p>
<pre><code>$ iex --sname node1 -S mixErlang/OTP <span class="hljs-number">18</span> [erts<span class="hljs-number">-7.3</span>] [source] [<span class="hljs-number">64</span>-bit] [smp:<span class="hljs-number">4</span>:<span class="hljs-number">4</span>] [<span class="hljs-keyword">async</span>-threads:<span class="hljs-number">10</span>] [hipe] [kernel-poll:<span class="hljs-literal">false</span>] [dtrace]Interactive Elixir (<span class="hljs-number">1.2</span><span class="hljs-number">.4</span>) - press Ctrl+C to exit (type h() ENTER <span class="hljs-keyword">for</span> help)<span class="hljs-number">16</span>:<span class="hljs-number">54</span>:<span class="hljs-number">08.554</span> [info]  Started Elixir.GameOfLife.BoardServer masteriex(node1@Artur)<span class="hljs-number">1</span>&gt; GameOfLife.BoardServer.start_game:game_startediex(node1@Artur)<span class="hljs-number">2</span>&gt; GameOfLife.GamePrinter.start_printing_board:printing_started
</code></pre><p>In another terminal window, you can start the second node. We will connect it with the first node.</p>
<pre><code>$ iex --sname node2 -S mixErlang/OTP <span class="hljs-number">18</span> [erts<span class="hljs-number">-7.3</span>] [source] [<span class="hljs-number">64</span>-bit] [smp:<span class="hljs-number">4</span>:<span class="hljs-number">4</span>] [<span class="hljs-keyword">async</span>-threads:<span class="hljs-number">10</span>] [hipe] [kernel-poll:<span class="hljs-literal">false</span>] [dtrace]Interactive Elixir (<span class="hljs-number">1.2</span><span class="hljs-number">.4</span>) - press Ctrl+C to exit (type h() ENTER <span class="hljs-keyword">for</span> help)<span class="hljs-number">16</span>:<span class="hljs-number">55</span>:<span class="hljs-number">17.395</span> [info]  Started Elixir.GameOfLife.BoardServer masteriex(node2@Artur)<span class="hljs-number">1</span>&gt; Node.connect :node1@Arturtrue16:<span class="hljs-number">55</span>:<span class="hljs-number">17.691</span> [info]  Started Elixir.GameOfLife.BoardServer slaveiex(node2@Artur)<span class="hljs-number">2</span>&gt; Node.list[:node1@Artur]iex(node2@Artur)<span class="hljs-number">3</span>&gt; Node.self:node2@Arturiex(node2@Artur)<span class="hljs-number">4</span>&gt; GameOfLife.Patterns.Guns.gosper_glider |&gt; GameOfLife.BoardServer.add_cells[{<span class="hljs-number">24</span>, <span class="hljs-number">8</span>}, {<span class="hljs-number">22</span>, <span class="hljs-number">7</span>}, {<span class="hljs-number">24</span>, <span class="hljs-number">7</span>}, {<span class="hljs-number">12</span>, <span class="hljs-number">6</span>}, {<span class="hljs-number">13</span>, <span class="hljs-number">6</span>}, {<span class="hljs-number">20</span>, <span class="hljs-number">6</span>}, {<span class="hljs-number">21</span>, <span class="hljs-number">6</span>}, {<span class="hljs-number">34</span>, <span class="hljs-number">6</span>}, {<span class="hljs-number">35</span>, <span class="hljs-number">6</span>}, {<span class="hljs-number">11</span>, <span class="hljs-number">5</span>}, {<span class="hljs-number">15</span>, <span class="hljs-number">5</span>}, {<span class="hljs-number">20</span>, <span class="hljs-number">5</span>}, {<span class="hljs-number">21</span>, <span class="hljs-number">5</span>}, {<span class="hljs-number">34</span>, <span class="hljs-number">5</span>}, {<span class="hljs-number">35</span>, <span class="hljs-number">5</span>}, {<span class="hljs-number">0</span>, <span class="hljs-number">4</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">4</span>}, {<span class="hljs-number">10</span>, <span class="hljs-number">4</span>}, {<span class="hljs-number">16</span>, <span class="hljs-number">4</span>}, {<span class="hljs-number">20</span>, <span class="hljs-number">4</span>}, {<span class="hljs-number">21</span>, <span class="hljs-number">4</span>}, {<span class="hljs-number">0</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">1</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">10</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">14</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">16</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">17</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">22</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">24</span>, <span class="hljs-number">3</span>}, {<span class="hljs-number">10</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">16</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">24</span>, <span class="hljs-number">2</span>}, {<span class="hljs-number">11</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">15</span>, <span class="hljs-number">1</span>}, {<span class="hljs-number">12</span>, <span class="hljs-number">0</span>}, {<span class="hljs-number">13</span>, <span class="hljs-number">0</span>}]
</code></pre><p>Both nodes are executing a calculation to determine a new state for living cells. You can run the game also across different servers in the network like this:</p>
<pre><code># start node1$ iex --name node1@<span class="hljs-number">192.168</span><span class="hljs-number">.0</span><span class="hljs-number">.101</span> --cookie <span class="hljs-string">"token_for_cluster"</span> -S mix# start node2 on another server$ iex --name node2@<span class="hljs-number">192.168</span><span class="hljs-number">.0</span><span class="hljs-number">.102</span> --cookie <span class="hljs-string">"token_for_cluster"</span> -S mixiex&gt; Node.connect :<span class="hljs-string">"node1@192.168.0.101"</span><span class="hljs-literal">true</span>
</code></pre><p>You already saw how the game works in the demo at the beginning of the article. You can try it on your own machine, just clone the <a target="_blank" href="https://github.com/BeyondScheme/elixir-game_of_life">repository</a>.</p>
<h3 id="heading-summary">Summary</h3>
<p>Finally, we managed to get to the end. It was a pretty long road but we have a working game, distributed across nodes. We learned how to write GenServer, use Agents, split processes across nodes with TaskSupervisor and connect nodes into the cluster. You also saw examples of tests in Elixir and how to use DocTest.</p>
<p>Hope you found something interesting in the article. Please share your thoughts in the comments.</p>
<p>Nowadays I work on CI parallelisation problem to run tests fast on CI servers. I built <a target="_blank" href="https://knapsackpro.com?utm_source=medium&amp;utm_medium=blog&amp;utm_campaign=game_of_life_elixir&amp;utm_content=knapsackpro"><strong>Knapsack Pro which splits your Ruby and JavaScript tests on parallel CI nodes to save time on testing</strong></a>. You can check one of my articles about <a target="_blank" href="https://medium.com/@arturtrzop/cypress-e2e-tests-split-with-ci-parallelisation-and-auto-balancing-ci-nodes-time-8c6c8b0a37f1">Cypress in JavaScript test suite parallelisation</a> or <a target="_blank" href="https://medium.com/datadriveninvestor/heroku-ci-parallel-test-runs-done-with-optimal-test-suite-split-how-to-run-parallel-dynos-79903d9a0ec6">Heroku CI parallelisation</a>. Let me know how slow or fast your tests are in your Elixir projects — just leave a comment. :) I’d like to add support for CI parallelisation testing into Elixir as well.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ My intro to Elixir: how learning another programming language can make you a better developer ]]>
                </title>
                <description>
                    <![CDATA[ By Nikolas O'Donnell I attended ElixirConf EU in Warsaw earlier this year. It was actually my first ever programming conference. My colleague was giving a talk about Elixir and Phoenix called ‘Phoenix and the World of Tomorrow’. Now, my background is... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/my-intro-to-elixir-how-learning-another-programming-language-can-make-you-a-better-developer-d967e568191c/</link>
                <guid isPermaLink="false">66c35bc37944e470c16db3ba</guid>
                
                    <category>
                        <![CDATA[ Elixir ]]>
                    </category>
                
                    <category>
                        <![CDATA[ learning ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 04 Dec 2018 00:33:19 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*-roj-FWdYdOaPvpF9qo-hw.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Nikolas O'Donnell</p>
<p>I attended ElixirConf EU in Warsaw earlier this year. It was actually my first ever programming conference. My colleague was giving a talk about Elixir and Phoenix called ‘Phoenix and the World of Tomorrow’.</p>
<p>Now, my background is in JavaScript but my company is Elixir obsessed. Having taken a sip of the company kool-aid and seen what it can do — I’m pretty well converted.</p>
<p>JavaScript will always be my first language, and holds a special place in my heart. I’m still using, learning and very much part of the vibrant and growing JavaScript and React community.</p>
<p>Although I’ve been familiarizing myself with Elixir for work, I have come to see a great deal of value in learning another programming language.</p>
<p>It’s a similar experience with learning a new spoken language. You’re pushed outside of your comfort zone. Having to understand and reason in another way, even from another perspective.</p>
<p>Further, you often have to rebuild from first principles — which in turn can have the added benefits of breaking preconceived assumptions and limitations.</p>
<p>This is a healthy thing to do and ultimately we should be language, library, and framework agnostic.</p>
<p>Our job is not actually to write code, and certainly not to write a specific ‘language’ of code.</p>
<p>Rather, it is to solve problems for our companies, clients, and customers.</p>
<p>Having other languages, frameworks and coding paradigms at your disposal when solving a problem increases your chances of solving it in a better way. In addition, it makes you a more well-rounded programmer and valuable team member.</p>
<h4 id="heading-exploring-elixir-further">Exploring Elixir further</h4>
<p>Elixir is a relatively new meta-programming language created by Jose Valim and launched in 2012.</p>
<p>The ‘meta’ part is not just me trying to be ‘hip’, ‘happening’ and ‘down with the cool kids’. It gives an extra piece of information to what Elixir is.</p>
<p>To explain more about Elixir, I guess I have to first talk a little bit about Erlang. This is because Elixir is built on top of Erlang (hence the “meta” part). It runs on the Erlang virtual machine, called the BEAM because of some acronym that I’d have to <a target="_blank" href="https://www.freecodecamp.org/news/my-intro-to-elixir-how-learning-another-programming-language-can-make-you-a-better-developer-d967e568191c/undefined">DuckDuckGo</a> to find out.</p>
<p>Erlang was created by Joe Armstrong, Robert Virding, and Mike Williams, while they were working for Ericsson in the mid 1980’s.</p>
<p>Ericsson works within the telecommunications space. They had the problem of making software that was robust, fault tolerant, and asynchronous — so calls didn’t drop out!</p>
<p>Charged with this mission, these engineers created Erlang. The Danish engineer <a target="_blank" href="https://en.wikipedia.org/wiki/Agner_Krarup_Erlang">Agner Krarup Erlang</a> is often cited as the namesake… though it’s also a pretty convenient choice for an <strong>Er</strong>icsson <strong>Lang</strong>uage (I’m on to you ಠ_ಠ).</p>
<p>Back to Elixir. Being a functional language, it is super nice to keep things all ordered, organized and readable.</p>
<p>This function does that specific task. This module does this set of functions. Neither really need to know what the other is doing. This modular design pattern makes it easier to keep a clean codebase.</p>
<p>It is actually considered a multi-paradigm language as it is functional, concurrent, distributed and process-orientated. Cool story — but what does any of that even mean?</p>
<ul>
<li><strong>Functional programming</strong> uses functions (ideally ‘pure functions’ where the inputs and outputs are clearly declared) with no hidden values coming in or out to build the program. The goal is to remove side effects or unintended outputs from the code.</li>
<li><strong>Concurrency</strong> lets a program execute multiple computations at the same time. It doesn’t have to wait for one thing to finish before starting another. This is referred to as ‘blocking’ because the execution of the next item is blocked from running until the previous item is completed.</li>
<li><strong>Distributed</strong> describes how information is exchanged. In distributed systems, problems are broken down into smaller tasks. These are completed through exchanging messages. As these messages can be talking to each other across machines/networks, it is distributed.</li>
<li><strong>Process-orientated</strong> also reflects how problems are broken down into smaller tasks or processes and aims to keep separate the data structures from the processes that interact with them. The reason for wanting to do this is that it allows for programmers to be more assured of getting the result they expect.</li>
</ul>
<p>This is what Elixir code looks like:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*I2Z7z9DnrvgDAf2gb1WY6w.gif" alt="Image" width="1024" height="800" loading="lazy">
_[https://marketplace.visualstudio.com/items?itemName=mjmcloug.vscode-elixir](https://marketplace.visualstudio.com/items?itemName=mjmcloug.vscode-elixir" rel="noopener" target="<em>blank" title=")</em></p>
<p>Clean right, and quite readable — take note of the following pattern:</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> &lt;<span class="hljs-title">Module_name</span></span>&gt; <span class="hljs-keyword">do</span>

 <span class="hljs-function"><span class="hljs-keyword">def</span> &lt;<span class="hljs-title">something</span></span>&gt; <span class="hljs-keyword">do</span>

  &lt;the things to <span class="hljs-keyword">do</span>&gt;
  |&gt; {you can <span class="hljs-keyword">use</span> the pipe operator (|&gt;) to parse..}
  |&gt; {the result from a function..}
  |&gt; {as the fist argument to the <span class="hljs-keyword">next</span> function..}
  |&gt; {creating a <span class="hljs-string">'pipeline'</span> <span class="hljs-keyword">with</span> a final output..} 
  |&gt; {of the entire cross function calls!}

 <span class="hljs-keyword">end</span>

<span class="hljs-keyword">end</span>
</code></pre>
<h4 id="heading-but-where-is-all-the-extra-syntax">But where is all the extra syntax?</h4>
<p>Well, being a new language, it has the fortune gained from hind sight. The language borrows some of the best aspects of other languages. As mentioned, it is built on Erlang and is actually compatible — meaning you can use Erlang syntax in Elixir code.</p>
<p>It also leverages the clean syntax and code structure of Ruby — its creator having come from a Ruby background.</p>
<h4 id="heading-phoenix">Phoenix</h4>
<p>Phoenix is a web framework built for Elixir by Chris McCord. You can think of this as a way to bootstrap out a project. It is modular (thanks to Elixir). It is also super fast (thanks to Erlang), and ultimately very powerful.</p>
<p>You can use it as the API layer between your database and your front end. You can also easily use the HTML and CSS templates that come with Phoenix. You can use Brunch JS to inject these parts into your website/app.</p>
<p>Alternatively, you could also use a front-end framework like Ember or React to do the same — making it a ‘best of both worlds’ approach.</p>
<p>This is the talk my colleague Ley gave at ElixirConf EU that I mentioned earlier. It is well worth a watch, as it looks at the role Phoenix can play in the next billion users accessing the internet on &lt; 3G devices:</p>
<p>So if you’re intrigued, why not take a sip of Elixir? I think you might just get hooked.</p>
<p>Though in any case, take a sip of something new. Move out of your programming comfort zone and challenge yourself to explore another language, perspective and way of thinking. What’s the worst that could happen...?</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*x49mx2qULeZcmfooM7cCOA.gif" alt="Image" width="500" height="211" loading="lazy">
_[https://gfycat.com/gifs/detail/fewalarmingcaiman](https://gfycat.com/gifs/detail/fewalarmingcaiman" rel="noopener" target="<em>blank" title=")</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to build a simple, extensible blog with Elixir and Phoenix ]]>
                </title>
                <description>
                    <![CDATA[ By Raman Sah In this post, we’ll discuss how to build a boilerplate Phoenix web app with user authentication and an admin panel, along with image upload in Elixir. TodoMVC has become a de facto tool to compare various JavaScript-based MV* frameworks.... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/simple-extensible-blog-built-with-elixir-and-phoenix-61d4dfafabb1/</link>
                <guid isPermaLink="false">66c35ed6258ebfc3dc8f1f86</guid>
                
                    <category>
                        <![CDATA[ Elixir ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Phoenix framework ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 28 Sep 2018 18:06:58 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/0*qT3nBMIsmRiQa4nc" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Raman Sah</p>
<p>In this post, we’ll discuss how to build a boilerplate Phoenix web app with user authentication and an admin panel, along with image upload in Elixir.</p>
<p><a target="_blank" href="http://todomvc.com/">TodoMVC</a> has become a de facto tool to compare various JavaScript-based MV* frameworks. Along the same lines, I feel that a blog application can be a tiebreaker in choosing a new backend or API framework.</p>
<p>So let’s get started and build one in Phoenix. We’ll follow the default setup, that is Phoenix hooked up with Ecto running on PostgreSQL.</p>
<p>Here are the final screens to give you an idea of what the app will look like at the end.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*x59piiG96eAfObns-aPzvQ.png" alt="Image" width="800" height="454" loading="lazy"></p>
<p>The landing page will show all the published blogs in a card layout. A card can be clicked to view that particular post.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*mxWIdSAc-p9elJI3x2yQKw.png" alt="Image" width="800" height="454" loading="lazy"></p>
<p>We will have a dashboard that will show the statistics in brief. Access to this page requires admin user login.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*xwuFgK253CAjphO0IVu8Ow.png" alt="Image" width="800" height="454" loading="lazy"></p>
<p>There will be a separate section that has an overview of all the posts. Here you can publish / modify / delete posts.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*fkzXp5iJjFJPzyk-bu1NFg.png" alt="Image" width="800" height="454" loading="lazy"></p>
<p>This is the post editor layout featuring a markdown editor along with a file picker for the featured image.</p>
<blockquote>
<p><em>Note: The full working code is hosted on <a target="_blank" href="https://github.com/ramansah/cms">GitHub</a>. There are numerous files in the project which cannot be shared in a single blog. So I have explained the specific ones which I assume are critical.</em></p>
</blockquote>
<p>Let’s keep the project’s name as CMS for now. So we’ll start with creating a new project with <code>mix phx.new cms</code>. Run <code>mix deps.get</code> to install dependencies.</p>
<p>Generate a migration file for users and posts, respectively.</p>
<pre><code># User migration file
</code></pre><pre><code>mix phx.gen.schema Auth.User users name:string email:string password_hash:string is_admin:boolean
</code></pre><pre><code># Posts migration file
</code></pre><pre><code>mix phx.gen.schema Content.Post posts title:string body:text published:boolean cover:string user_id:integer slug:string
</code></pre><p>Two tables have to be created in the database which represent users and posts. I’ve kept it rather simple, keeping only the required fields and expanding when the need arises.</p>
<p>Subsequently, we can define changesets and additional methods in the user and post schema as well.</p>
<p><strong>user.ex</strong></p>
<p><strong>post.ex</strong></p>
<pre><code>@derive {Phoenix.Param, <span class="hljs-attr">key</span>: :slug}
</code></pre><p>Since we want the posts to have a readable and SEO friendly URL structure, we inform route helpers to reference <code>slug</code> instead of <code>id</code> in the URL namespace.</p>
<p>The routes are described here:</p>
<p>Resources which are specific to the admin section are clubbed together and assigned a pipeline which forces authentication.</p>
<p>Meanwhile, global routes are treated with passive authentication. User details are fetched if a session is present but the pages are still accessible. Login and home pages belong here.</p>
<p>Executing <code>mix phx.routes</code> gives me this output:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*C0G1-utBGFbtv8332dWFfA.png" alt="Image" width="800" height="239" loading="lazy"></p>
<p>The view is divided into three logical sections:</p>
<ol>
<li>Navigation bar</li>
<li>Sidebar</li>
<li>Main Content</li>
</ol>
<p>While the navigation bar is always visible, the sidebar appears only if an admin user is logged in. Browsing content will be inside the admin context. The links in the sidebar will grow as and when the app evolves.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*UkNS-kHZ4Dpo9lgMpzqSdA.png" alt="Image" width="553" height="531" loading="lazy"></p>
<p>The Admin.Post controller follows the typical CRUD architecture and includes an action to toggle the published state of a given post.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*xwuFgK253CAjphO0IVu8Ow.png" alt="Image" width="800" height="454" loading="lazy"></p>
<p>A lot of controls reside in the index page of admin’s post section. Here, posts can be deleted, published and modified.</p>
<p><strong>templates/admin/post/index.html.eex</strong></p>
<p>To keep the template uncluttered, we can define convenience view helpers like formatting time etc. separately.</p>
<p><strong>views/admin/post_view.ex</strong></p>
<p>Arc along with arc_ecto provides out of the box file upload capabilities. Since a post features a cover image, we have to define an arc configuration in our app.</p>
<p>Each post in our blog requires two versions of cover images — original which is visible inside specific post view and a thumb version with a smaller footprint to populate the cards. For now, let’s go with 250x250 resolution for the thumb version.</p>
<p>Coming back to the app’s landing page, it will house the cards for all the published posts. And each post will be accessible through the slug formed.</p>
<pre><code>controllers/page_controller.ex
</code></pre><p>This project explores Phoenix — how a Phoenix app is structured and how to dismantle a Phoenix-based project. I hope you’ve learned something and enjoyed it!</p>
<p>The full working app is on Github : <a target="_blank" href="https://github.com/ramansah/votex">https://github.com/ramansah/</a>cms. Feel free to clone ? and do clap if you find this blog useful ?</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to write a super fast link shortener with Elixir, Phoenix, and Mnesia ]]>
                </title>
                <description>
                    <![CDATA[ By Ben Church Let's start this month’s tutorial with two statements that are going to get me in trouble: Elixir is the most productive language out there. Bit.ly charges way much for their paid plan Elixir, the Phoenix framework, and the Erlang VM ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-write-a-super-fast-link-shortener-with-elixir-phoenix-and-mnesia-70ffa1564b3c/</link>
                <guid isPermaLink="false">66c35631cf1314a450f0d693</guid>
                
                    <category>
                        <![CDATA[ Elixir ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ software development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 18 Sep 2018 21:49:29 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*_5mxMoPtToWHgTEyslbYnA.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Ben Church</p>
<p>Let's start this month’s tutorial with two statements that are going to get me in trouble:</p>
<ol>
<li>Elixir is the most productive language out there.</li>
<li>Bit.ly charges way much for their paid plan</li>
</ol>
<p>Elixir, the Phoenix framework, and the Erlang VM allows us to make production ready systems fast, easily, and with very few moving parts. At this point, you can see where we’re going with this. Let’s use what comes out of the box with Elixir to create a link shortener and prevent a $500 monthly bill.</p>
<p>Let’s dive in!</p>
<h3 id="heading-initial-setup">Initial Setup</h3>
<h4 id="heading-before-you-start">Before you start</h4>
<p>Please make sure you have the following:</p>
<ol>
<li><a target="_blank" href="https://elixir-lang.org/install.html">Elixir</a></li>
<li><a target="_blank" href="https://hexdocs.pm/phoenix/installation.html">Phoenix</a></li>
</ol>
<h4 id="heading-create-a-new-phoenix-project">Create a new Phoenix Project</h4>
<p>The first thing to do as always is to have Phoenix create the skeleton of the project. For this demo, we won't have a “frontend” so we can tell the initializer to leave those out.</p>
<p>In your terminal type:</p>
<pre><code>mix phx.new shorten_api --no-html --no-brunch
</code></pre><h4 id="heading-update-your-dependencies">Update your dependencies</h4>
<p>Next, we're going to add a few dependencies to <code>mix.exs</code>. Go ahead and update the <code>deps/0</code> function in that file to look like this:</p>
<h3 id="heading-logic">Logic!</h3>
<p>Ok basic set up out of the way. That means we’re into setting up the logic that will:</p>
<ol>
<li>Allow us to save URLs</li>
<li>Reference these URLs by a unique HashId (ex. <code>abc123</code>)</li>
<li>Navigate to <code>/abc123</code> and it redirects to the URL that it references</li>
</ol>
<p>First up, creating a way to store these links.</p>
<h4 id="heading-creating-a-way-to-store-these-links">Creating a way to store these links</h4>
<p>Let's use Phoenix’s built-in generators to do this for us. In your terminal run:</p>
<p><code>mix phx.gen.json Links Link links hash:string:unique url:string:unique</code></p>
<p>This will create the</p>
<ol>
<li><code>Links</code> context</li>
<li><code>Link</code> model</li>
<li><code>Link</code> controller</li>
<li>Database migration</li>
</ol>
<p>That’s honestly the end of that step. It will ask you to put a few lines in your <code>router.ex</code> file but for now, you can skip that if you want as we will touch on it later. Let's move onto how we can modify what was created above to automatically create the id’s we will use to reference these links.</p>
<h4 id="heading-autogenerate-the-hashid-on-link-creation">Autogenerate the HashId on Link creation</h4>
<p>By default in these systems, models are given an <code>id</code> column in the database that is a number, unique and auto increments: 1, 2, 3 and so on. In our system we want the <code>id</code> to be a:</p>
<ol>
<li>Short</li>
<li>Unique</li>
<li>String</li>
<li>That auto-generates.</li>
</ol>
<p><em>Ecto makes this really easy.</em></p>
<p>The first thing to do is make a custom Ecto Type which will handle all of this for us. Create a new file <code>shorten_api/ecto/hash_id.ex</code> and populate it as follows:</p>
<p>What we did above is essentially create a new type that can be used the same way we define a field as a <code>String</code> or an <code>Integer</code>. Now we can define a field as a <code>HashId</code>.</p>
<blockquote>
<p><em>You can learn more about this in the <a target="_blank" href="https://hexdocs.pm/ecto/Ecto.Type.html">Ecto documentation</a>.</em></p>
</blockquote>
<p>So let's do just that and update <code>shorten_api/links/link.ex</code> to use a <code>HashId</code> as it’s a primary key instead of an <code>Integer</code>:</p>
<h4 id="heading-update-the-migration">Update the migration</h4>
<p>Now that the <code>HashId</code> is setup in our code, we want to update the migration to set up the database to reflect what's happening in our model file. You should have a file in your project that ends with <code>_create_links.exs</code> . Find it, open it and modify it to resemble the below code:</p>
<p>Alright, that's the majority of our plumbing steps, now we’re going to jump into the core logic of this whole project.</p>
<h4 id="heading-redirect-from-an-id-to-a-url">Redirect from an Id to a URL</h4>
<p>First, we need a function in our controller that</p>
<ol>
<li>Takes an <code>Id</code> of a <code>Link</code></li>
<li>Looks up the <code>Link</code></li>
<li>Redirects to the <code>URL</code> attached to that <code>Link</code></li>
</ol>
<p>To do this, let's add a new function to our link controller found here: <code>shorten_api_web/controllers/link_controller.ex</code></p>
<h4 id="heading-hook-it-all-up-to-our-router">Hook it all up to our router</h4>
<p>Now that we have this new controller function, the only thing left is to hook it up. Update the <code>router.ex</code> file to reflect the following:</p>
<blockquote>
<p>Note: we will also be adding the routes to <code>mix phx.gen</code> suggested earlier</p>
</blockquote>
<h3 id="heading-tada">TADA! ?</h3>
<p>At this point, you should be able to run the project with <code>mix phx.server</code> and have everything function as expected! However, we’re not stopping here.</p>
<h3 id="heading-secret-sauce">Secret sauce</h3>
<p>Because link shorteners sit between a user and the actual content, it is crucial that these systems are fast. While Elixir is already fast, the main lag time in this process comes from our database. It takes time to look up the Link attached to an id.</p>
<p>To speed this up, link shorteners will often opt to use an in-memory datastore like Redis as opposed to an on-disk database like Postgres (which Phoenix sets us up with by default). <strong>Thankfully because Elixir is built on top of the Erlang VM, we already have an in-memory datastore built in: Mnesia!</strong></p>
<p>In the next section, we’re going to alter our configuration to use Mnesia instead of Postgres.</p>
<h4 id="heading-swapping-from-postgres-to-mnesia">Swapping from Postgres to Mnesia</h4>
<p>This process is actually very simple. First update <code>config.exs</code> as shown:</p>
<h4 id="heading-create-your-mnesia-database">Create your Mnesia database</h4>
<p>Then create the location where Mnesia will back data up to and initialize the database through Ecto:</p>
<pre><code>mkdir priv/datamkdir priv/data/mnesiamix ecto.createmix ecto.migrate
</code></pre><p><strong>Boom done!</strong> You’re now using an In-Memory database to hold our Link information. Wasn’t that easy?</p>
<h3 id="heading-kick-it-off">Kick it off</h3>
<p>You now can</p>
<ol>
<li>start the project (again for many of you):</li>
</ol>
<p><code>mix phx.server</code></p>
<ol start="2">
<li>Create a shortened link via <code>curl</code>:</li>
</ol>
<pre><code>curl --request POST \  --url http:<span class="hljs-comment">//localhost:4000/api/links/ \  --header 'content-type: application/json' \  --data '{ "link": {  "url": "https://twitter.com/bnchrch" }}'</span>
</code></pre><ol start="3">
<li>Take the <code>hash</code> returned in the response</li>
</ol>
<p><code>{“data”:{“url”:”[https://twitter.com/bnchrch](https://twitter.com/bnchrch)","hash":"7cJY_ckq"}}</code></p>
<ol start="4">
<li>And be redirected appropriately when you go to <code>localhost:4000/{hash}</code>:</li>
</ol>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*5jbpF4xVkeo2K7UIQKJWXg.gif" alt="Image" width="640" height="437" loading="lazy"></p>
<h3 id="heading-wrap-up">Wrap up</h3>
<p>Amazing how easy it was with all these tools to create a fast, easy to maintain and simple to extend link shortener. A lot of the credit here can go to the BEAM (Erlang VM), Jose Valim (creator of Elixir), and Chris McCord (creator of Phoenix). The rest of the praise goes to how simple the idea of a link shortener is, in no way justifying a $500 per month introductory price tag. Still looking at you Bit.ly.</p>
<h4 id="heading-this-is-open-source-you-can-find-it-here-on-githubhttpsgithubcombechurchshortenapitutorial">?‍ This is open source! you can f<a target="_blank" href="https://github.com/bechurch/shorten_api_tutorial">ind it here on Github</a></h4>
<h4 id="heading-i-only-write-about-programming-and-remote-work-if-you-follow-me-on-twitterhttpswwwtwittercombnchrch-i-wont-waste-your-time">❤️ I only write about programming and remote work. If you <a target="_blank" href="https://www.twitter.com/bnchrch">follow me on Twitter</a> I won’t waste your time.</h4>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Elixir: A Big-Picture Programming Language ]]>
                </title>
                <description>
                    <![CDATA[ By CityBase Elixir makes programmers better at their work, and it makes their work better About a year ago, I decided to pursue the chance to work with Elixir full time as lead engineer at CityBase. Since I began using the programming language in 201... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/elixir-a-big-picture-programming-language-755dcef2fa6a/</link>
                <guid isPermaLink="false">66c349b54f7405e6476b019f</guid>
                
                    <category>
                        <![CDATA[ Elixir ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Erlang ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Productivity ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 22 Jan 2018 16:40:02 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*tLIXa6jWWjxfB-6AYjm2Hg.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By CityBase</p>
<h4 id="heading-elixir-makes-programmers-better-at-their-work-and-it-makes-their-work-better">Elixir makes programmers better at their work, and it makes their work better</h4>
<p>About a year ago, I decided to pursue the chance to work with Elixir full time as lead engineer at CityBase. Since I began using the programming language in 2014, my goal has been to use, grow, and learn more from Elixir.</p>
<p>CityBase attracted me for other reasons, too. I’ve always been curious about making complex systems work better. With government technology, we have the challenge to make some of the most complex systems work more effectively for people on a massive scale, to massive impact.</p>
<p>These objectives — growing Elixir and coding for large-scale impact — are in my opinion the same. I really believe that today, reliability, concurrency, and fault tolerance are foundational qualities that most applications should be built on. Elixir can help with that based on a battle-proven list of applications and concepts.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/UtGCPFkJSMeNlcix48URAs-F-7-YIhJyx8mf" alt="Image" width="800" height="599" loading="lazy">
_(Photo: [Unsplash](https://unsplash.com/photos/w7ZyuGYNpRQ?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" rel="noopener" target="_blank" title=""&gt;Kevin, &lt;a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" rel="noopener" target="<em>blank" title="))</em></p>
<h3 id="heading-a-new-language-with-tested-roots">A New Language with Tested Roots</h3>
<p>Elixir is a new language created in 2011. It’s built with the principles of Erlang, a system developed in 1986 for the telecom industry. Erlang is the reason your phone is never temporarily shut down for maintenance. It’s responsible for hardware flexibility and scalability, so you can replace a phone and have your account work the same, and add new phone lines without affecting performance.</p>
<p>Even though Elixir is relatively young, it relies on Erlang’s proven virtual machine (VM), called BEAM, and its principles of high availability, adaptability, and scalability. These features are exceptionally important in govtech applications, where the fundamental services delivered by technology must be available to everyone.</p>
<h3 id="heading-the-programmers-programing-language">The Programmer’s Programing Language</h3>
<p>Engineers like me are excited to work with Elixir because it helps us be better at our jobs. To code with Elixir, developers need to be in tune with overall business objectives, and code with the future flexibility in mind.</p>
<p>It includes tools that encourage programmers to plan around what can go wrong, and focus on getting as close as possible to the ideal outcome for the end user.</p>
<p><strong>Having programmers who understand the desired big-picture outcomes can be a game changer.</strong></p>
<p>If you ask someone to write code that performs a certain function, they’ll write that code. But if you ask them to write code that leads to an experience or solves a problem, they may think of a solution you never considered — and foresee problems you didn’t know existed.</p>
<p>Elixir encourages this kind of big-picture thinking in its DNA. A shared characteristic of Elixir and Erlang is that they are holistic programming languages. You could easily use Elixir to develop just one service, but it is optimized for developing large systems of many services.</p>
<h3 id="heading-a-language-thats-fault-tolerant">A Language That’s Fault-Tolerant</h3>
<p>Like death and taxes, another certainty in life is that things will go wrong.</p>
<p>Elixir has native fault tolerance for the two major types of programming errors.</p>
<h4 id="heading-error-type-1"><strong>Error Type 1</strong></h4>
<p>The rarest issues are usually discovered in production and are by definition harder to test for. For instance, connectivity (when a service goes down or is taking longer than expected) between a third-party service or a system resource, such as a database.</p>
<p>To be fault-tolerant to these issues, your system must always be available to customers by using at least two servers. This is to address hardware issues, network problems, or other errors that live outside your program.</p>
<p>Elixir runs on Erlang’s BEAM VM, which is configured as a mini OS on top of the server OS. The VM is responsible for the communication between servers and nodes. A node will be notified when another node is down, and the system will act in response.</p>
<h4 id="heading-error-type-2"><strong>Error Type 2</strong></h4>
<p>Issues associated with data are easier to test and can be reproduced locally. For example, if a function that does any math calculation receives a string instead of a number, that function will fail.</p>
<p>For a program to be fault-tolerant here, your system must be able to “heal” itself during errors stemming from logic bugs, wrong input data, and other internal failures.</p>
<p>As Elixir is a compiled language, any mistakes in the code prevent the application from starting. This ensures that running applications at least have a valid starting state.</p>
<p>For this to work smoothly, the Erlang VM uses something called the supervision principle, which goes like this:</p>
<ul>
<li>Processes are structured based on the idea that there are both “worker” and “supervisor” modules built into a given program.</li>
<li>Workers perform computations, and supervisors monitor workers.</li>
<li>If something goes wrong, a supervisor can restart a worker to its initial valid state.</li>
</ul>
<p>The supervision principle is associated with process isolation, when one module can run in one isolated process. This ensures that errors in one module will not affect other parts of the application, and you can restart that module in isolation as well.</p>
<h3 id="heading-a-language-thats-modular-and-built-to-scale">A Language That’s Modular and Built to Scale</h3>
<p>Elixir is a modular language, meaning that you can modify self-contained parts without worrying about impacting other, unrelated parts. Microservices function concurrently. These code-based actions all play roles in the greater program you’ve created, but tasks are distributed so that they are not dependent on one another to work. This reinforces the benefits of failing fast — you take one faulty player out, and the game still continues.</p>
<p>This also becomes crucial as a codebase grows: in non-modular systems, it’s not always clear when one part impacts others — or which other parts it might impact. This means that even the smallest change requires that you test everything to ensure that the change didn’t break anything. This makes for a daunting amount of work, which means projects move slowly and require lots of people.</p>
<p>It also means that training new developers is difficult and time-consuming, as they must familiarize themselves with a complex code legacy in order to effectively expand it.</p>
<p>With Elixir, <strong>developers focus on the future, and ensure they’re coding for new or evolving goals</strong>, rather than a previous vision that’s baked into overly complex code.</p>
<p>Beyond its modularity, Elixir is also highly scalable. The language enables you to start building an application running only one or two servers and to add more as needed. Together, the servers function as part of a cluster in a distributed system to achieve high availability and scalability.</p>
<p>Within that cluster, servers communicate using an Erlang-based protocol, rather than having to implement or use any application protocol like HTTP, and pick a data serialization/deserialization option like JSON or Protocol Buffers. That means you don’t need to implement anything to pass data between services in different servers/nodes. This is huge in terms of complexity of logic for communication.</p>
<h3 id="heading-a-language-that-has-no-niche">A Language That Has No Niche</h3>
<p>Obviously, I’m a fan of Elixir. It’s got a lot going for it, from its proven infrastructure to its scalability. But maybe the coolest thing about this language is that it’s industry agnostic. Already, it’s been adopted by a range of companies, for a variety of purposes: WhatsApp, Bleacher Report, Netflix, Pinterest, Postmates, and a handful of .gov sites all use Elixir or Erlang.</p>
<p>This is a plus for several reasons: first, it means that the language is likely to continue to grow in popularity as companies recognize the benefits it can offer. That, in turn, means that developers will continue to learn it, which means there’s not likely to be a shortage of developers who know Elixir. Companies of all sizes should be able to find developers at all levels to work with Elixir.</p>
<p>Given the power this language has to improve user experience, minimize downtime, and make life easier for dev teams, these are indicators everyone should be cheering — especially those of us in engineering and programming fields.</p>
<p>For those of us in govtech, Elixir is especially promising, as it embodies the kind of resilience and long-term focus essential to making governments function better for everyone.</p>
<p>• • •</p>
<p>By <a target="_blank" href="http://pedroassumpcao.ghost.io/">Pedro Assumpcao</a>, Lead Software Engineer at CityBase</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Authenticate your Elixir/Phoenix APIs using Guardian ]]>
                </title>
                <description>
                    <![CDATA[ By Nirmalya Ghosh Authentication is always a tricky subject. People tend to use so many types of authentication in their apps. Authentication using an email address and a password with an option confirm password field is the most common. But every ti... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/authentication-using-elixir-phoenix-f9c162b2c398/</link>
                <guid isPermaLink="false">66c345139972b7c5c7624e05</guid>
                
                    <category>
                        <![CDATA[ api ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Elixir ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Phoenix framework ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sun, 19 Mar 2017 19:13:09 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*kK_wSy_F4xp77Uj8aSFkeQ.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Nirmalya Ghosh</p>
<p>Authentication is always a tricky subject. People tend to use so many types of authentication in their apps. Authentication using an email address and a password with an option confirm password field is the most common. But every time you see a registration form, you feel bored thinking that you’ll have to type in so much just to register! What’s the fun in that!</p>
<p>So, for this app, I’ll be doing authentication using your Google account. It’s pretty straightforward. You just need to click on a button, give the app the necessary permissions to access you basic Google profile and you’re set! Cool, isn’t it?</p>
<p>We will be using <a target="_blank" href="https://github.com/ueberauth/ueberauth">Ueberauth</a>, <a target="_blank" href="https://github.com/ueberauth/ueberauth_google">Ueberauth Google</a> and <a target="_blank" href="https://github.com/ueberauth/guardian">Guardian</a> for authenticating our user. Ueberauth and Ueberauth Google will help authenticate the user with their Google credentials. Guardian will help us in generating a JSON Web Token for logged in users. That token is necessary and needs to be in the header of each request for any route which needs authentication.</p>
<p>Guardian will check for that token in the requests’ header and if the token is valid, the authenticated routes will be available to the user. I’ll explain these things in details.</p>
<p>If you hadn’t already installed <a target="_blank" href="http://www.phoenixframework.org">Phoenix</a> and its necessary dependencies, you can head over to the <a target="_blank" href="http://www.phoenixframework.org/docs/installation">Phoenix Guides</a> to install and get up and running.</p>
<p>To get started add the dependencies to our <code>mix.exs</code>.</p>
<pre><code>defp deps <span class="hljs-keyword">do</span>  [   ...      {:ueberauth, <span class="hljs-string">"~&gt; 0.4"</span>},   {:ueberauth_google, <span class="hljs-string">"~&gt; 0.2"</span>},   {:ja_serializer, <span class="hljs-string">"~&gt; 0.11.2"</span>},   {:guardian, <span class="hljs-string">"~&gt; 0.14.2"</span>}]end
</code></pre><p>After this, run <code>mix deps.get</code> to fetch the dependencies.</p>
<p>You also need to add <code>ueberauth</code> and <code>ueberauth_google</code> to our application in <code>mix.exs</code>.</p>
<pre><code>def application <span class="hljs-keyword">do</span>  [mod: {SocialAppApi, []},   <span class="hljs-attr">applications</span>: [   ...      :ueberauth, :ueberauth_google]]end
</code></pre><p>Now, you will need to add your <code>ueberauth</code>, <code>ueberauth_google</code> and <code>guardian</code> configuration to your <code>config/config.exs</code> file.</p>
<pre><code># Ueberauth Config <span class="hljs-keyword">for</span> oauthconfig :ueberauth, Ueberauth,  <span class="hljs-attr">base_path</span>: <span class="hljs-string">"/api/v1/auth"</span>,  <span class="hljs-attr">providers</span>: [    google: { Ueberauth.Strategy.Google, [] },    <span class="hljs-attr">identity</span>: { Ueberauth.Strategy.Identity, [        callback_methods: [<span class="hljs-string">"POST"</span>],        <span class="hljs-attr">uid_field</span>: :username,        <span class="hljs-attr">nickname_field</span>: :username,      ] },  ]
</code></pre><pre><code># Ueberauth Strategy Config <span class="hljs-keyword">for</span> Google oauthconfig :ueberauth, Ueberauth.Strategy.Google.OAuth,  <span class="hljs-attr">client_id</span>: System.get_env(<span class="hljs-string">"GOOGLE_CLIENT_ID"</span>),  <span class="hljs-attr">client_secret</span>: System.get_env(<span class="hljs-string">"GOOGLE_CLIENT_SECRET"</span>),  <span class="hljs-attr">redirect_uri</span>: System.get_env(<span class="hljs-string">"GOOGLE_REDIRECT_URI"</span>)
</code></pre><pre><code># Guardian configurationconfig :guardian, Guardian,  <span class="hljs-attr">allowed_algos</span>: [<span class="hljs-string">"HS512"</span>], # optional  verify_module: Guardian.JWT,  # optional  issuer: <span class="hljs-string">"SocialAppApi"</span>,  <span class="hljs-attr">ttl</span>: { <span class="hljs-number">30</span>, :days },  <span class="hljs-attr">allowed_drift</span>: <span class="hljs-number">2000</span>,  <span class="hljs-attr">verify_issuer</span>: <span class="hljs-literal">true</span>, # optional  secret_key: System.get_env(<span class="hljs-string">"GUARDIAN_SECRET"</span>) || <span class="hljs-string">"rFtDNsugNi8jNJLOfvcN4jVyS/V7Sh+9pBtc/J30W8h4MYTcbiLYf/8CEVfdgU6/"</span>,  <span class="hljs-attr">serializer</span>: SocialAppApi.GuardianSerializer
</code></pre><p>As you can see here, I’ve used <code>System.get_env() .</code> This is a way to store credentials in your app which you don’t want to be a part of your codebase. You can create a <code>.env</code> file and store all of these credentials like:</p>
<pre><code><span class="hljs-keyword">export</span> DB_NAME_PROD=<span class="hljs-string">"social_app_api_db"</span><span class="hljs-keyword">export</span> DB_PASSWORD_PROD=<span class="hljs-string">"password"</span><span class="hljs-keyword">export</span> DB_USERNAME_PROD=<span class="hljs-string">"password"</span>
</code></pre><p>After this, you need to do <code>source .env</code> and then, you can use them in your app.</p>
<p>Now, we’ll need to do a bunch of stuff with our controllers which will let the user to sign up or sign in.</p>
<p>First, create a new file <code>web/controllers/auth_controller.ex</code>.</p>
<pre><code>defmodule SocialAppApi.AuthController <span class="hljs-keyword">do</span>  use SocialAppApi.Web, :controller  plug Ueberauth
</code></pre><pre><code>  alias SocialAppApi.User  alias MyApp.UserQuery
</code></pre><pre><code>  plug :scrub_params, <span class="hljs-string">"user"</span> when action <span class="hljs-keyword">in</span> [:sign_in_user]
</code></pre><pre><code>  def request(_params) <span class="hljs-keyword">do</span>  end
</code></pre><pre><code>  def <span class="hljs-keyword">delete</span>(conn, _params) <span class="hljs-keyword">do</span>    # Sign out the user    conn    |&gt; put_status(<span class="hljs-number">200</span>)    |&gt; Guardian.Plug.sign_out(conn)  end
</code></pre><pre><code>  def callback(%{<span class="hljs-attr">assigns</span>: %{<span class="hljs-attr">ueberauth_failure</span>: _fails}} = conn, _params) <span class="hljs-keyword">do</span>    # This callback is called when the user denies the app to get the data <span class="hljs-keyword">from</span> the oauth provider    conn    |&gt; put_status(<span class="hljs-number">401</span>)    |&gt; render(SocialAppApi.ErrorView, <span class="hljs-string">"401.json-api"</span>)  end
</code></pre><pre><code>  def callback(%{<span class="hljs-attr">assigns</span>: %{<span class="hljs-attr">ueberauth_auth</span>: auth}} = conn, _params) <span class="hljs-keyword">do</span>    <span class="hljs-keyword">case</span> AuthUser.basic_info(auth) <span class="hljs-keyword">do</span>      {:ok, user} -&gt;        sign_in_user(conn, %{<span class="hljs-string">"user"</span> =&gt; user})    end
</code></pre><pre><code>  <span class="hljs-keyword">case</span> AuthUser.basic_info(auth) <span class="hljs-keyword">do</span>      {:ok, user} -&gt;        conn        |&gt; render(SocialAppApi.UserView, <span class="hljs-string">"show.json-api"</span>, %{<span class="hljs-attr">data</span>: user})      {:error} -&gt;        conn        |&gt; put_status(<span class="hljs-number">401</span>)        |&gt; render(SocialAppApi.ErrorView, <span class="hljs-string">"401.json-api"</span>)    end  end
</code></pre><pre><code>  def sign_in_user(conn, %{<span class="hljs-string">"user"</span> =&gt; user}) <span class="hljs-keyword">do</span>    <span class="hljs-keyword">try</span> <span class="hljs-keyword">do</span>      # Attempt to retrieve exactly one user <span class="hljs-keyword">from</span> the DB, whose      # email matches the one provided <span class="hljs-keyword">with</span> the login request      user = User      |&gt; where(email: ^user.email)      |&gt; Repo.one!
</code></pre><pre><code>      cond <span class="hljs-keyword">do</span>        <span class="hljs-literal">true</span> -&gt;          # Successful login          # Encode a JWT          { :ok, jwt, _ } = Guardian.encode_and_sign(user, :token)
</code></pre><pre><code>          auth_conn = Guardian.Plug.api_sign_in(conn, user)          jwt = Guardian.Plug.current_token(auth_conn)          {:ok, claims} = Guardian.Plug.claims(auth_conn)
</code></pre><pre><code>          auth_conn          |&gt; put_resp_header(<span class="hljs-string">"authorization"</span>, <span class="hljs-string">"Bearer #{jwt}"</span>)          |&gt; json(%{<span class="hljs-attr">access_token</span>: jwt}) # Return token to the client
</code></pre><pre><code>        <span class="hljs-literal">false</span> -&gt;          # Unsuccessful login          conn          |&gt; put_status(<span class="hljs-number">401</span>)          |&gt; render(SocialAppApi.ErrorView, <span class="hljs-string">"401.json-api"</span>)      end    rescue      e -&gt;        IO.inspect e # Print error to the <span class="hljs-built_in">console</span> <span class="hljs-keyword">for</span> debugging
</code></pre><pre><code>        # Successful registration        sign_up_user(conn, %{<span class="hljs-string">"user"</span> =&gt; user})    end  end
</code></pre><pre><code>  def sign_up_user(conn, %{<span class="hljs-string">"user"</span> =&gt; user}) <span class="hljs-keyword">do</span>    changeset = User.changeset %User{}, %{<span class="hljs-attr">email</span>: user.email,      <span class="hljs-attr">avatar</span>: user.avatar,      <span class="hljs-attr">first_name</span>: user.first_name,      <span class="hljs-attr">last_name</span>: user.last_name,      <span class="hljs-attr">auth_provider</span>: <span class="hljs-string">"google"</span>}
</code></pre><pre><code>    <span class="hljs-keyword">case</span> Repo.insert changeset <span class="hljs-keyword">do</span>      {:ok, user} -&gt;        # Encode a JWT        { :ok, jwt, _ } = Guardian.encode_and_sign(user, :token)
</code></pre><pre><code>        conn        |&gt; put_resp_header(<span class="hljs-string">"authorization"</span>, <span class="hljs-string">"Bearer #{jwt}"</span>)        |&gt; json(%{<span class="hljs-attr">access_token</span>: jwt}) # Return token to the client      {:error, changeset} -&gt;        conn        |&gt; put_status(<span class="hljs-number">422</span>)        |&gt; render(SocialAppApi.ErrorView, <span class="hljs-string">"422.json-api"</span>)    end  end
</code></pre><pre><code>  def unauthenticated(conn, params) <span class="hljs-keyword">do</span>    conn    |&gt; put_status(<span class="hljs-number">401</span>)    |&gt; render(SocialAppApi.ErrorView, <span class="hljs-string">"401.json-api"</span>)  end
</code></pre><pre><code>  def unauthorized(conn, params) <span class="hljs-keyword">do</span>    conn    |&gt; put_status(<span class="hljs-number">403</span>)    |&gt; render(SocialAppApi.ErrorView, <span class="hljs-string">"403.json-api"</span>)  end
</code></pre><pre><code>  def already_authenticated(conn, params) <span class="hljs-keyword">do</span>    conn    |&gt; put_status(<span class="hljs-number">200</span>)    |&gt; render(SocialAppApi.ErrorView, <span class="hljs-string">"200.json-api"</span>)  end
</code></pre><pre><code>  def no_resource(conn, params) <span class="hljs-keyword">do</span>    conn    |&gt; put_status(<span class="hljs-number">404</span>)    |&gt; render(SocialAppApi.ErrorView, <span class="hljs-string">"404.json-api"</span>)  endend
</code></pre><p>Here <code>sign_in_user</code> will sign the user in and throw an <code>access_token</code> as the response. The <code>sign_up_user</code> will sign the user up using their Google credentials and then throw an <code>access_token</code> as the response. This token is essential in the way that Guardian will check for this <code>access_token</code> in all the requests’ header. It will check if the user is currently in session or not. If yes, all the authenticated routes will be available to the user. Otherwise, he will receive a <code>401</code> response for the authenticated routes.</p>
<p>Let’s add some routes to our app. Our <code>router.ex</code> file looks like this:</p>
<pre><code>defmodule SocialAppApi.Router <span class="hljs-keyword">do</span>  use SocialAppApi.Web, :router
</code></pre><pre><code>  pipeline :api <span class="hljs-keyword">do</span>    plug :accepts, [<span class="hljs-string">"json"</span>, <span class="hljs-string">"json-api"</span>]    plug JaSerializer.Deserializer  end
</code></pre><pre><code>  pipeline :api_auth <span class="hljs-keyword">do</span>    plug :accepts, [<span class="hljs-string">"json"</span>, <span class="hljs-string">"json-api"</span>]    plug Guardian.Plug.VerifyHeader, <span class="hljs-attr">realm</span>: <span class="hljs-string">"Bearer"</span>    plug Guardian.Plug.LoadResource    plug JaSerializer.Deserializer  end
</code></pre><pre><code>  scope <span class="hljs-string">"/api/v1"</span>, SocialAppApi <span class="hljs-keyword">do</span>    pipe_through :api_auth
</code></pre><pre><code>  resources <span class="hljs-string">"/users"</span>, UserController, <span class="hljs-attr">except</span>: [:<span class="hljs-keyword">new</span>, :edit]    get <span class="hljs-string">"/user/current"</span>, UserController, :current, <span class="hljs-attr">as</span>: :current_user    <span class="hljs-keyword">delete</span> <span class="hljs-string">"/logout"</span>, AuthController, :<span class="hljs-keyword">delete</span>  end
</code></pre><pre><code>  scope <span class="hljs-string">"/api/v1/auth"</span>, SocialAppApi <span class="hljs-keyword">do</span>    pipe_through :api
</code></pre><pre><code>    get <span class="hljs-string">"/:provider"</span>, AuthController, :request    get <span class="hljs-string">"/:provider/callback"</span>, AuthController, :callback    post <span class="hljs-string">"/:provider/callback"</span>, AuthController, :callback  endend
</code></pre><p>Here, pipeline <code>api_auth</code> is the one which is authenticated. The pipeline <code>api</code> isn’t. So, we can visit <code>get “/:provider”, AuthController, :request</code> without signing in.</p>
<p>Create another file called <code>web/models/auth_user.ex</code> with the following code:</p>
<pre><code>defmodule AuthUser <span class="hljs-keyword">do</span>  alias Ueberauth.Auth
</code></pre><pre><code>  def basic_info(%Auth{} = auth) <span class="hljs-keyword">do</span>    {:ok,      %{        <span class="hljs-attr">avatar</span>: auth.info.image,        <span class="hljs-attr">email</span>: auth.info.email,        <span class="hljs-attr">first_name</span>: auth.info.first_name,        <span class="hljs-attr">last_name</span>: auth.info.last_name      }    }  endend
</code></pre><p>You will also need to create a <code>User</code> model.</p>
<pre><code>mix phoenix.gen.json User users email:string auth_provider:string first_name:string last_name:string avatar:string
</code></pre><p>This will generate your necessary model and migration.</p>
<p>Your model will look something like this:</p>
<pre><code>defmodule SocialAppApi.User <span class="hljs-keyword">do</span>  use SocialAppApi.Web, :model
</code></pre><pre><code>  schema <span class="hljs-string">"users"</span> <span class="hljs-keyword">do</span>    field :email, :string    field :auth_provider, :string    field :first_name, :string    field :last_name, :string    field :avatar, :string
</code></pre><pre><code>    timestamps()  end
</code></pre><pre><code>  def changeset(struct, params \\ %{}) <span class="hljs-keyword">do</span>    struct    |&gt; cast(params, [:email, :auth_provider, :first_name, :last_name, :avatar])    |&gt; validate_required([:email, :auth_provider, :first_name, :last_name, :avatar])    |&gt; unique_constraint(:email)  endend
</code></pre><p>Your migration file will look something like this:</p>
<pre><code>defmodule SocialAppApi.Repo.Migrations.CreateUser <span class="hljs-keyword">do</span>  use Ecto.Migration
</code></pre><pre><code>  def change <span class="hljs-keyword">do</span>    create table(:users) <span class="hljs-keyword">do</span>      add :email, :string      add :auth_provider, :string      add :first_name, :string      add :last_name, :string      add :avatar, :string
</code></pre><pre><code>      timestamps()    end
</code></pre><pre><code>    # Unique email address constraint, via DB index    create index(:users, [:email], <span class="hljs-attr">unique</span>: <span class="hljs-literal">true</span>)  endend
</code></pre><p>Now, run the migration.</p>
<pre><code>mix ecto.migrate
</code></pre><p>Also, create a <code>UserController</code> for our <code>user</code> model. That will contain the following code:</p>
<pre><code>defmodule SocialAppApi.UserController <span class="hljs-keyword">do</span>  use SocialAppApi.Web, :controller
</code></pre><pre><code>  alias SocialAppApi.User
</code></pre><pre><code>  plug Guardian.Plug.EnsureAuthenticated, <span class="hljs-attr">handler</span>:     SocialAppApi.AuthController
</code></pre><pre><code>  def index(conn, _params) <span class="hljs-keyword">do</span>    users = Repo.all(User)    render(conn, <span class="hljs-string">"index.json-api"</span>, <span class="hljs-attr">data</span>: users)  end
</code></pre><pre><code>  def current(conn, _) <span class="hljs-keyword">do</span>    user = conn    |&gt; Guardian.Plug.current_resource
</code></pre><pre><code>    conn    |&gt; render(SocialAppApi.UserView, <span class="hljs-string">"show.json-api"</span>, <span class="hljs-attr">data</span>: user)  endend
</code></pre><p>This is useful in case you want to check if the authenticated routes work or not after all your hard work.</p>
<p>Create two more views at <code>web/views/error_view.ex</code> with the following code:</p>
<pre><code>defmodule SocialAppApi.ErrorView <span class="hljs-keyword">do</span>  use SocialAppApi.Web, :view  use JaSerializer.PhoenixView
</code></pre><pre><code>  def render(<span class="hljs-string">"401.json-api"</span>, _assigns) <span class="hljs-keyword">do</span>    %{<span class="hljs-attr">title</span>: <span class="hljs-string">"Unauthorized"</span>, <span class="hljs-attr">code</span>: <span class="hljs-number">401</span>}    |&gt; JaSerializer.ErrorSerializer.format  end
</code></pre><pre><code>  def render(<span class="hljs-string">"403.json-api"</span>, _assigns) <span class="hljs-keyword">do</span>    %{<span class="hljs-attr">title</span>: <span class="hljs-string">"Forbidden"</span>, <span class="hljs-attr">code</span>: <span class="hljs-number">403</span>}    |&gt; JaSerializer.ErrorSerializer.format  end
</code></pre><pre><code>  def render(<span class="hljs-string">"404.json-api"</span>, _assigns) <span class="hljs-keyword">do</span>    %{<span class="hljs-attr">title</span>: <span class="hljs-string">"Page not found"</span>, <span class="hljs-attr">code</span>: <span class="hljs-number">404</span>}    |&gt; JaSerializer.ErrorSerializer.format  end
</code></pre><pre><code>  def render(<span class="hljs-string">"422.json-api"</span>, _assigns) <span class="hljs-keyword">do</span>    %{<span class="hljs-attr">title</span>: <span class="hljs-string">"Unprocessable entity"</span>, <span class="hljs-attr">code</span>: <span class="hljs-number">422</span>}    |&gt; JaSerializer.ErrorSerializer.format  end
</code></pre><pre><code>  def render(<span class="hljs-string">"500.json-api"</span>, _assigns) <span class="hljs-keyword">do</span>    %{<span class="hljs-attr">title</span>: <span class="hljs-string">"Internal Server Error"</span>, <span class="hljs-attr">code</span>: <span class="hljs-number">500</span>}    |&gt; JaSerializer.ErrorSerializer.format  end
</code></pre><pre><code>  # In <span class="hljs-keyword">case</span> no render clause matches or no  # template is found, <span class="hljs-keyword">let</span><span class="hljs-string">'s render it as 500  def template_not_found(_template, assigns) do    render "500.json-api", assigns  endend</span>
</code></pre><p>Also, create another view <code>web/views/user_view.ex</code> with the following code:</p>
<pre><code>defmodule SocialAppApi.UserView <span class="hljs-keyword">do</span>  use SocialAppApi.Web, :view  use JaSerializer.PhoenixView
</code></pre><pre><code>  attributes [:avatar, :email, :first_name, :last_name, :auth_provider]end
</code></pre><p>And, you are all set. Fire up your server:</p>
<pre><code>mix phoenix.server
</code></pre><p>Now, go to <a target="_blank" href="http://localhost:4000/api/v1/auth/google">http://localhost:4000/api/v1/auth/google</a> and you will be redirected to Google’s login page. Once you give the app the necessary permissions, you will get an <code>access_token</code> in the response:</p>
<pre><code>{  <span class="hljs-attr">access_token</span>: <span class="hljs-string">"eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJVc2VyOjIiLCJleHAiOjE0ODk4NjM4MzUsImlhdCI6MTQ4NzI3MTgzNSwiaXNzIjoiU29jaWFsQXBwQXBpIiwianRpIjoiODU0NzJhODAtN2Q4Ny00MjM0LWIxNmUtODgyMTBmYWZkZDJmIiwicGVtIjp7fSwic3ViIjoiVXNlcjoyIiwidHlwIjoiYWNjZXNzIn0.L2LjpsyJAjF1r99hR11WVGcQ"</span>}
</code></pre><p>Now, you can install the <a target="_blank" href="https://chrome.google.com/webstore/detail/modheader/idgpnmonknjnojddfkpgkljpfnnfcklj?hl=en">Modheader</a> extension for Chrome and any other extension through which you can set response headers. Add <code>Authorization</code> as a <code>Request Header</code> and the <code>access_token</code> with <code>Bearer &lt;access_tok</code>en&gt;.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/8s46wKgYRFAp7mdlDFIYgtInAUZzUpw3-INz" alt="Image" width="542" height="171" loading="lazy">
<em>Modheader</em></p>
<p>Go to <a target="_blank" href="http://localhost:4000/api/v1/users">http://localhost:4000/api/v1/users</a> and you will be able to see an array of users you’ve already sign up with. You can also go to <a target="_blank" href="http://localhost:4000/api/v1/user/current">http://localhost:4000/api/v1/user/current</a> to see the current user in the session.</p>
<p>If you remove that value from Modheader and go to <a target="_blank" href="http://localhost:4000/api/v1/users">http://localhost:4000/api/v1/users</a>, you will get the following response:</p>
<pre><code>{  <span class="hljs-attr">jsonapi</span>: {    <span class="hljs-attr">version</span>: <span class="hljs-string">"1.0"</span>  },  <span class="hljs-attr">errors</span>: [{    <span class="hljs-attr">title</span>: <span class="hljs-string">"Unauthorized"</span>,    <span class="hljs-attr">code</span>: <span class="hljs-number">401</span>  }]}
</code></pre><p>As I’ve mentioned earlier, you need to send the <code>access_token</code> received to view the authenticated routes. Now, you know how to do API authentication in Elixir. You can compare your code with my code on <a target="_blank" href="https://github.com/ghoshnirmalya/social_app_api/tree/5f16f6432796f3fe372e971522dd588b5db3a421">Github</a>.</p>
<p>If you have some feedback, let me know in the comments below.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How Long Should I Make My API Key? ]]>
                </title>
                <description>
                    <![CDATA[ By Sam Corcos Calculating collision probabilities of hashed values Say you built an API that generates public keys, and these keys all need to be unique and hard to guess. The most common way to do this is to use a hash function to generate a random... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-long-should-i-make-my-api-key-833ebf2dc26f/</link>
                <guid isPermaLink="false">66c34e7130aba6677fb9f9ff</guid>
                
                    <category>
                        <![CDATA[ api ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Elixir ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Erlang ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Security ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 20 Aug 2016 11:04:45 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*BbHKX6Db55smgYEUJj-qZg.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Sam Corcos</p>
<h4 id="heading-calculating-collision-probabilities-of-hashed-values">Calculating collision probabilities of hashed values</h4>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*BbHKX6Db55smgYEUJj-qZg.png" alt="Image" width="800" height="442" loading="lazy"></p>
<p>Say you built an API that generates public keys, and these keys all need to be unique and hard to guess. The most common way to do this is to use a hash function to generate a random series of numbers and letters. A typical hash looks something like the text below.</p>
<blockquote>
<p><em>AFGG2piXh0ht6dmXUxqv4nA1PU120r0yMAQhuc13i8</em></p>
</blockquote>
<p>A question that often comes up is, “How long does my hash need to be in order to ensure uniqueness?” Most people assume this is a difficult calculation. So they default to some very large number, like a 50-digit hash. The equation to approximate collision probability is actually quite simple.</p>
<h3 id="heading-how-do-i-calculate">How do I calculate?</h3>
<p>Let’s assume you’re using a good cryptographic algorithm (i.e. <a target="_blank" href="https://medium.com/@betable/tifu-by-using-math-random-f1c308c4fd9d#.1ypwox7l4">not JavaScript’s Math.random</a>). Every language has a decent crypto package for generating random hashes. With Phoenix, you can use the Erlang <strong>:crypto</strong> package.</p>
<p>There are only two pieces of information you need to do the calculation:</p>
<ol>
<li>How many possible unique hash values can you create with the given inputs? We’ll assign this to the variable <em>N.</em></li>
<li>How many values could you possibly need to generate in the lifetime of your project? We’ll assign this to the variable <em>k.</em></li>
</ol>
<p>To calculate the first value, add up all the possible characters that can go into your hash. Raise it to the power of the length of your hash.</p>
<p>So for example, if your hash value contains numbers, lowercase, and uppercase letters, that adds up to 62 total characters (10 + 26 + 26) that we can use. If we are generating a hash of only 3 characters in length, then:</p>
<p><em>N</em> = 62³ = <strong>238,328</strong> possible values</p>
<p>To calculate the second value, you need to think about what your app does and make some reasonable assumptions.</p>
<p>Let’s say your app is generating a hash to assign to each of your customers. Let’s also say that your app is very niche. The absolute-best case scenario, your app will have 1000 customers over its lifetime. Then, for safety’s sake, we’ll multiply that by 10. We assume that you may need to generate 10,000 values over the course of your app’s life.</p>
<p><em>k</em> = <strong>10,000</strong> upper bound for possible values that need to be created</p>
<p>So now we need to calculate. There are many algorithms we can use. We’ll use one of the <a target="_blank" href="https://en.wikipedia.org/wiki/Birthday_problem#Approximations">simple approximations</a>, where <em>e</em> is the mathematical constant (the base of the natural log), and <em>k</em> and <em>N</em> are the same values as above.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*jET4z0dP--QnXA3AjpvEKA.png" alt="Image" width="360" height="142" loading="lazy"></p>
<p>The base equation gives us the probability that all values are unique. Then we subtract that result from 1 to get the odds that you have a collision. If you don’t feel like writing your own equation, I’ve provided one below written in JavaScript.</p>
<p>So in the example above, <strong>calculate(N, k)</strong> yields a probability of <strong>approximately 100%</strong> that you will have a collision. So for that use case, you should use a hash of more than 3 characters in length.</p>
<p>Now, if we were to take that same example but change the length of our hash from 3 to 5, we would get an <em>N</em> that is <em>much</em> larger (exponentials are good like that).</p>
<p><em>N</em> = 62⁵ = ~<strong>900,000,000</strong></p>
<p>Assuming the same value <em>k</em>, our new probability of a collision is down to only <strong>5.4%</strong>. What if we bumped that from 5 characters to 10?</p>
<p><em>N</em> = 62¹⁰ = <strong>~800,000,000,000,000,000</strong></p>
<p>Yes, that’s ~800 quintillion unique hashes. Which bring your odds of a collision down to 0.000000000062%. This is about a <strong>1-in-50-billion chance</strong> that you have a conflict. And that’s with a hash of 10 digits — something like: BwQ1W6soXk.</p>
<p>For another example, let’s say you’re a data processing company that deals with lots of transactions. We’ll say you deal with 1 million processes per second and you need to generate a hash for each of them. Let’s also say that you think this company could run for the next 100 years.</p>
<p>_k = ~_3,000,000,000,000,000</p>
<p>That comes out to about <strong>3 quadrillion</strong> hashes that you need made that all need to be unique. That’s a lot of data! But even with this extremely large number to work with, you only need a <strong>21-digit</strong> hash to ensure a <strong>1-in-10-million chance</strong> of collision over the lifetime of the project.</p>
<p>So the next time you’re worried about the length of your hash in ensuring uniqueness, know that you don’t need one as long as you think you do. Plus, you can do the calculation yourself without much effort.</p>
<p><em>Sam Corcos is the lead developer and co-founder of <a target="_blank" href="http://sightlinemaps.com">Sightline Maps</a>, the most intuitive platform for 3D printing topographical maps, as well as <a target="_blank" href="http://learnphoenix.io">LearnPhoenix.io</a>, an intermediate-advanced tutorial site for building scalable production apps with Phoenix and React.</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Happy Little Projects: Elixir, Phoenix, Twilio, and the Spotify API ]]>
                </title>
                <description>
                    <![CDATA[ By Nathan One of the most difficult aspects of starting a programming project is coming up with a project idea in the first place. No inspiration means no programming! Luckily, I recently discovered a project idea with a nice pacing to it: a basic No... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/happy-little-projects-ef8cd157287/</link>
                <guid isPermaLink="false">66c34c19a7aea9fc97bdfb3f</guid>
                
                    <category>
                        <![CDATA[ Elixir ]]>
                    </category>
                
                    <category>
                        <![CDATA[ learning ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Phoenix framework ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 15 Jun 2016 18:32:55 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*9RWesRs9XuAaOeObRW81SQ.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Nathan</p>
<p>One of the most difficult aspects of starting a programming project is coming up with a project idea in the first place. No inspiration means no programming!</p>
<p>Luckily, I recently discovered a project idea with a nice pacing to it: a basic Node.js and Express.js application using Spotify’s API and Twilio.</p>
<p>If you’d like to follow along with the JavaScript version, check out this <a target="_blank" href="https://youtu.be/EKb8XD_V32o">video</a>. If you want to build it in Gradle and Spark, here’s Twilio’s <a target="_blank" href="https://www.twilio.com/blog/2015/09/getting-started-with-gradle-and-the-spark-framework-3.html">original post</a>.</p>
<p>For this article, we’ll build this app using Elixir and Phoenix. Let’s get started.</p>
<p>Here’s the general flow of the app:</p>
<ul>
<li>You text the title of a song to a Twilio number</li>
<li>Twilio makes an HTTP POST to a preconfigured url with some infomation including the song title and information about the requester</li>
<li>The application searches the Spotify API for the song, and parses out a preview url</li>
<li>The application dispatches a message to the Twilio API. This includes the number to call and a url to fetch some TwiML(Twilio Markup Language)</li>
<li>Twilio fetches the TwiML from our application, calls the recipient, and plays them the song preview.</li>
</ul>
<p>I’ve been through a majority of the curriculum at <a target="_blank" href="http://www.freecodecamp.com">Free Code Camp</a>. I helped design and build a large part of the core systems behind Free Code Camp, so I’m very comfortable with JavaScript. Following along with the presentation was easy, and I was quick to recreate it. The questions was, could I do it with a language and framework I didn’t have solid experience with?</p>
<p>If you’d like to know what the finished product <em>looks</em> like, text the title of a song to +1 (334) 721–2652. Don’t worry, we won’t save your phone number or song. Request all the ABBA you want!</p>
<p>NOTE: I’ve loaded some funds into this to keep it going a while. I’m hosting it on Heroku so it may take a moment to wake up and respond. If it doesn’t respond at all, the funds I added have been exhausted.</p>
<p>Here’s a short video of me interacting with my app using Google Voice.</p>
<p>I wanted to challenged myself and play with a language that I’ve been smitten with since hearing about it. <a target="_blank" href="http://elixir-lang.org/">Elixir</a> is a gorgeous language with Ruby inspired syntax. Elixir runs on the BEAM (Erlang VM), and is interoperable with Erlang. Yes, Erlang of <a target="_blank" href="http://www.wired.com/2015/09/whatsapp-serves-900-million-users-50-engineers/">WhatsApp fame</a>. I love the idea of being able to tap into that kind of power and reliability. I also <strong>love</strong> functional programming.</p>
<p>On top of Elixir, I’m also a fan of the <a target="_blank" href="http://www.phoenixframework.org/">Phoenix web framework</a>. It’s easy to get started with, and easy to get things done in. The error messages are excellent and tend to tell you exactly how to fix them. Trust me on this, I’ve seen enough of them.</p>
<p>The first task is is to generate a new Phoenix application. I called mine Philter, so I typed:</p>
<pre><code>mix phoenix.new philter --no-ecto --no-brunch
</code></pre><p>With this, we’re creating a new phoenix application called Philter, with no database layer and no JavaScript build system. We won’t be using any JavaScript in this project!</p>
<p>Follow the on-screen instructions to finish setting up the application. We’re now ready to work through our list of tasks.</p>
<h4 id="heading-twilio"><strong>Twilio</strong></h4>
<p>Twilio makes it pretty easy to setup an account. As an aside, their documentation and console are top notch. It’s one of my favorite web services to use.</p>
<p>Sign up for Twilio <a target="_blank" href="https://www.twilio.com/try-twilio">here</a>. If you want to follow along with this tutorial, you’ll have to add some funds. $5 would be more than enough to give you days of playing around. If you decide to follow along, buy a phone number and keep your browser tab open.</p>
<h4 id="heading-ngrok">Ngrok</h4>
<p>The next service you’ll want to use is <a target="_blank" href="https://ngrok.com/">ngrok</a>. This handy little service tunnels into a specified port on your computer and gives you a public url to use. The service is completely free, but I signed up for the $5/mo plan so I could have a reserved subdomain. It’s the little things, I tell ya.</p>
<p>Open a new terminal tab and install ngrok via npm. Then use ngrok to specify that you’d like to create an http tunnel to port 4000 on your computer.</p>
<pre><code># is a comment <span class="hljs-keyword">for</span> your information# ~ represents your terminal prompt~ npm i -g ngrok
</code></pre><pre><code>...~ ngrok http <span class="hljs-number">4000n</span>grok by @inconshreveable                           (Ctrl+C to quit)
</code></pre><pre><code>Tunnel Status             onlineVersion                   <span class="hljs-number">2.1</span><span class="hljs-number">.1</span>Region                    United States (us)Web Interface             http:<span class="hljs-comment">//127.0.0.1:4040Forwarding                http://someurl.ngrok.io -&gt; localhost:4000Forwarding                https://someurl.ngrok.io -&gt; localhost:4000</span>
</code></pre><p>Now switch back to your original terminal tab and start your phoenix app.</p>
<pre><code>~ iex -S mix phoenix.server      # &lt;or&gt;~ mix phoenix.server
</code></pre><p>The first option starts the server in an interactive shell that will let you interact with it. Regardless of your method of starting it, you’ll see it log out that it’s listening on your local machine on port 4000.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/zIq2uSM4GlYePld0AMckR7YAYtGlrYH3F5kk" alt="Image" width="566" height="333" loading="lazy"></p>
<p>Now open a new browser tab and visit <em>localhost:4000</em> to confirm it’s working. Then, paste in the url from the <em>Forwarding</em> http line in the ngrok terminal that I bolded above. You’ll see your app there too. Magic!</p>
<p>Go back to the tab you have your Twilio console open in, and find your phone number. Click on it, and you should see some configuration information. Under the messaging section, “When a message comes in”, enter the url from ngrok followed by “api/sms”. Ensure the HTTP method is set to POST. For reference, while building this application mine was set to <em>http://tkb.ngrok.com/api/sms</em></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/ISv5k57nBcVTQJp-xVCfpvLE33K-FDkxJ5Vb" alt="Image" width="800" height="579" loading="lazy"></p>
<p>While we have the Twilio console open, get your ACCOUNT SID and AUTH TOKEN credentials. You can find them by clicking on your account name in the top right hand corner of the window and looking at the “API Credentials” section. Create two environmental variables, TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN. I use an extension of the preferences panel on my Mac called <a target="_blank" href="https://github.com/hschmidt/EnvPane">EnvPane</a>. You can also search google and get a ton of results if you need help setting yours.</p>
<p>With all that information at hand, we’re nearly ready to tie it all together. We have one last thing to configure. We’re going to use <a target="_blank" href="https://github.com/danielberkompas/ex_twilio">ExTwilio</a>, a library to help our phoenix application talk to Twilio.</p>
<p>Open <em>config/config.exs</em> and add the following above the final <em>import</em> statement:</p>
<pre><code>config :ex_twilio,  <span class="hljs-attr">account_sid</span>: System.get_env(“TWILIO_ACCOUNT_SID”),  <span class="hljs-attr">auth_token</span>: System.get_env(“TWILIO_AUTH_TOKEN”)
</code></pre><p>Here’s where we’re telling our application to read in these two environmental variables so we can send messages and make phone calls through Twilio’s API.</p>
<p>In your favorite editor of choice (mine is <a target="_blank" href="http://spacemacs.org">Spacemacs</a>), open your Phoenix application directory. Let’s get down to business.</p>
<p>Open <em>web/router.ex</em> and get rid of any <strong>scope</strong> stuff you see. Replace it with:</p>
<pre><code>scope “/”, Philter <span class="hljs-keyword">do</span>  pipe_through :browser
</code></pre><pre><code>  post “/twiml”, TwimlController, :indexend
</code></pre><pre><code>scope “/api”, Philter <span class="hljs-keyword">do</span>  pipe_through :api
</code></pre><pre><code>  post “/sms”, SmsController, :indexend
</code></pre><p>Replace any mention of Philter with whatever name you gave your application.</p>
<p>The above code did a few things. We have a created a route that will match POSTs to <em>http://yourngrokurl/twiml</em> and route to <em>TwimlController</em>’s index function. We also did the same for the <em>http://yourngrokurl/api/sms</em> route, passing off to <em>SmsController</em>’s index function. Learn more about routing in Phoenix by checking out the excellent <a target="_blank" href="http://www.phoenixframework.org/docs/routing">documentation</a>.</p>
<p>Now create two files in the <em>web/controllers/</em> directory, _sms<em>controller.ex</em> and _twiml<em>controller.ex</em>. Make your _sms<em>controller.ex</em> look like:</p>
<pre><code>defmodule Philter.SmsController <span class="hljs-keyword">do</span>  use Philter.Web, :controller
</code></pre><pre><code>  alias Philter.Sms
</code></pre><pre><code>  def index(conn, %{<span class="hljs-string">"Body"</span> =&gt; song, <span class="hljs-string">"From"</span> =&gt; <span class="hljs-keyword">from</span>, <span class="hljs-string">"To"</span> =&gt; to}) <span class="hljs-keyword">do</span>
</code></pre><pre><code>    Task.start_link(fn -&gt; search_spotify(song, %{<span class="hljs-attr">from</span>: <span class="hljs-keyword">from</span>, <span class="hljs-attr">to</span>:     to}) end)    send_resp(conn, <span class="hljs-number">200</span>, “”)   end
</code></pre><pre><code>  defp search_spotify(song, twilio_data) <span class="hljs-keyword">do</span>    Philter.Spotify.search(song, twilio_data)  endend
</code></pre><p>To those Elixirists reading this, please keep in mind I’m still very much learning. With that disclaimer out of the way, on to a brief explanation.</p>
<p>Twilio will post the phone number of the requester in a <em>From</em> field, our Twilio number in the <em>To</em> field, and the body of their text in the <em>Body</em> field. We’re fishing those out, then spawning a task to search the Spotify API.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/-pfgkBdJYkKY--vlTT9D32JaH9mjrnyqP8PP" alt="Image" width="636" height="450" loading="lazy"></p>
<p>Spawning a task executes some long running function in a super lightweight BEAM process. If something happens to the process, like it crashes or catches on fire or is stricken by cosmic rays, our application will happily continue accepting connections and prevent moments of developer panic.</p>
<p>A full discussion of Tasks and OTP in general is far beyond the scope of this article. Please refer to the Elixir links in this post if you’d like to learn more about this amazing beast.</p>
<p>Now refer to the <a target="_blank" href="https://github.com/terakilobyte/Philter">GitHub repository</a> for this project. The files you’ll want to copy are <em>lib/philter/spotify.ex</em> and the entire <em>lib/philter/spotify/</em> directory. Ensure you go through the files, changing any mention of Philter to your own application name. In <em>spotify.ex</em>, <a target="_blank" href="https://github.com/terakilobyte/Philter/blob/master/lib/philter/spotify.ex#L55">on line 55</a>, replace “tkb” in the url to whatever your ngrok url is.</p>
<p>Now make your _twiml<em>controller.ex</em> look like the following:</p>
<pre><code>defmodule Philter.TwimlController <span class="hljs-keyword">do</span>  use Philter.Web, :controller
</code></pre><pre><code>  alias Philter.Twiml
</code></pre><pre><code>  def index(conn, %{<span class="hljs-string">"song"</span> =&gt; song) <span class="hljs-keyword">do</span>    render(conn, “index.html”, <span class="hljs-attr">song</span>: song)  endend
</code></pre><p>All we’re doing here is fishing the song out and and then passing it along as a variable to our template.</p>
<p>Open the <em>app.html.eex</em> in the <em>web/templates/</em> directory and delete everything except for:</p>
<pre><code> &lt;%= render @view_module, @view_template, assigns %&gt;
</code></pre><p>We don’t need any of that other markup!</p>
<p>Next, create a file under the <em>web/views/</em> directory called _twiml<em>view.ex</em>. We could put helper functions in here, but we don’t need any so it’s just going to live as a shell file. The contents should be:</p>
<pre><code>defmodule Philter.TwimlView <span class="hljs-keyword">do</span>  use Philter.Web, :viewend
</code></pre><p>Now create a new directory under <em>web/templates/</em> called <em>twiml/</em> and inside of it create a file called <em>index.html.eex</em>. The contents are straightforward:</p>
<pre><code> &lt;?xml version=”<span class="hljs-number">1.0</span><span class="hljs-string">" encoding=”UTF-8"</span> ?&gt; <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Response</span>&gt;</span>   <span class="hljs-tag">&lt;<span class="hljs-name">Say</span>&gt;</span>Please enjoy the clip!<span class="hljs-symbol">&amp;lt;</span>/Say&gt;   <span class="hljs-tag">&lt;<span class="hljs-name">Play</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">%=</span> @<span class="hljs-attr">song</span> %&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">Play</span>&gt;</span>   <span class="hljs-tag">&lt;<span class="hljs-name">Say</span>&gt;</span>I hope you enjoyed your song clip<span class="hljs-tag">&lt;/<span class="hljs-name">Say</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">Response</span>&gt;</span></span>
</code></pre><p>This is the response we’ll send to Twilio when they ask us for TwiML in response to our API interaction within the task I barely touched on. Feel free to play around, and reference the great <a target="_blank" href="https://www.twilio.com/docs/api/twiml">TwiML documentation</a>.</p>
<p>With a restart of Phoenix, you should be able to text your Twilio phone number and get a phone call with the song preview!</p>
<h4 id="heading-denouement">Dénouement</h4>
<p>I had a lot of fun implementing this little application with Elixir. Much like when I was learning JavaScript, I had to wrap my head around how to go about doing things, using functions correctly (even finding the correct functions to use!), and ensuring the application logic was correct.</p>
<p>There were many furrowed brows and a lot of documentation and online tutorials were referenced as I dove deeper into Elixir. I was very pleasantly surprised at how little Phoenix specific code was needed though. It’s a great framework in my opinion.</p>
<p>In the end, the final product made the temporary frustrations worth it. Getting reactions out of my wife and friends like “That’s so cool!” or “You made that?” are definitely worth the effort. It also helped cement patterns and notions I had about how things worked more clearly. Maybe after I get out of the military, I will indeed be able to transition into a career in programming.</p>
<p>I have to give a shout out to Twilio. They’ve made an excellent project. Every interaction I needed to perform was well documented, and their management console itself made it easy to find what I needed and configure actions on that end.</p>
<p>If you are interested in learning more about Elixir and Phoenix, I’d highly recommend <a target="_blank" href="https://pragprog.com/book/elixir12/programming-elixir-1-2">Programming Elixir</a>, <a target="_blank" href="https://www.manning.com/books/elixir-in-action">Elixir In Action</a>, and <a target="_blank" href="https://pragprog.com/book/phoenix/programming-phoenix">Programming Phoenix</a>. I have all 3 and they are excellent! The online documentation is well above par, and more and more tutorials and articles are popping up. In general, the Elixir community is one of the best out there.</p>
<p>We are definitely excited to add Elixir to our backend languages at FreeCodeCamp.</p>
<p>Learning to code is hard. I’d highly recommend signing up for <a target="_blank" href="http://www.freecodecamp.com">FreeCodeCamp</a> and following along the very structured and self-paced learning track we’ve established. Thousands upon thousands of people are finding success, and we have a <strong>massive</strong> community of helpful people - beginners to professionals - in our Gitter room.</p>
<p>Remember, don’t let something as ephemeral as frustration at a task today keep you from achieving your goals tomorrow. Happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
