<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Harrison Lucas on Medium]]></title>
        <description><![CDATA[Stories by Harrison Lucas on Medium]]></description>
        <link>https://medium.com/@harry_dev?source=rss-e609451a5e39------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6kZhnCa4YBT0zY1SccMdNQ.jpeg</url>
            <title>Stories by Harrison Lucas on Medium</title>
            <link>https://medium.com/@harry_dev?source=rss-e609451a5e39------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 19:29:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@harry_dev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[There’s more than code.]]></title>
            <link>https://medium.com/hackernoon/theres-more-than-code-288d0a8480d0?source=rss-e609451a5e39------2</link>
            <guid isPermaLink="false">https://medium.com/p/288d0a8480d0</guid>
            <category><![CDATA[soft-skills]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Harrison Lucas]]></dc:creator>
            <pubDate>Sat, 16 May 2020 09:59:58 GMT</pubDate>
            <atom:updated>2020-05-16T10:06:35.162Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LSxMFhbUYml7Ft2QIIz_Iw.png" /></figure><p>Let me paint you a picture.</p><p>Imagine Bob. Friendly guy, knows his stuff. Knows how to get things done.</p><p>Lone wolf type. Headphones on. Slack = Do not disturb.</p><p>Moves tickets from to do -&gt; in progress -&gt; review before you’ve even finished you’re coffee.</p><p>You give him a feature and you know it’s going to get done. And done well. Pull requests are a bit big and his Git hub profile is showing a strong gravitation towards the commit axis, but hey, it’s all worth it in the end.</p><p>Dream developer right?</p><ul><li>✅ Finishes features on time</li><li>✅ Focuses throughout the day</li><li>✅ Quality code</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/0*ziAIm6HXi50YRFxo.gif" /></figure><p>Whilst Bob seems to be doing some solid, A+ “pat on the back” work, he needs to realise there is more to being a good developer than just pumping out features.</p><p>Developers work in teams. Teams move together.</p><p>For every 700+ line PR he pumps out, he’s just taken an hour out of a team mates day.</p><p>Every PR he doesn’t review, has just fallen onto the lap of another team member. Now they can’t focus on their features.</p><p>Whilst the team is discussing an important architectural change, Bob’s got his headphones on.</p><p>Worst of all, is the opportunity cost that’s lost with the way Bob works. By himself he can contribute 2x as any other team member. That’s fantastic.</p><p>But what if Bob spent time <em>helping</em> and <em>leveling up</em> is other team members. Helping them fill in gaps in their knowledge. Showing them better patterns, abstractions. Setting up tools to help them work faster, leaving <strong>insightful</strong> feedback on their PRs.</p><p>What if Bob helped his <em>whole</em> team become just as effective as him. Suddenly, instead of 1 person contributing 2x, we have 5 people contributing 2x.</p><p>Now you don’t have a 10x developer.</p><p>You’ve got a 10x team, and that is 10x more valuable.</p><h3>Take aways</h3><p>Think of your team like a rowboat. When someone stops rowing (PR reviews, stuck without help etc.) the boat slows and goes in circles.</p><ol><li>Understand that there is more than just number of lines your write. Being apart of a team means exactly that, <em>being apart of a team</em>.</li><li>Smaller PRs are exponentially faster to review than larger ones. Reduce the burden on your team.</li><li>Review your team members PRs as soon as soon as you get a chance. Get them rowing again.</li><li>Become a force multiplier for your team. One person being 3x is less effective than 2 x 2x person.</li></ol><p>If you liked this post and want to keep up to date on new articles — you can follow me on <a href="https://twitter.com/harryblucas">twitter</a> or <a href="https://struggling.dev/there&#39;s-more-then-code">subscribe to my newsletter.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=288d0a8480d0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/theres-more-than-code-288d0a8480d0">There’s more than code.</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Explained — Function Composition]]></title>
            <link>https://medium.com/@harry_dev/explained-function-composition-541511d37108?source=rss-e609451a5e39------2</link>
            <guid isPermaLink="false">https://medium.com/p/541511d37108</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[growth]]></category>
            <dc:creator><![CDATA[Harrison Lucas]]></dc:creator>
            <pubDate>Mon, 04 May 2020 11:05:11 GMT</pubDate>
            <atom:updated>2020-05-04T11:05:11.044Z</atom:updated>
            <content:encoded><![CDATA[<h3>Explained — Function Composition</h3><p>April 28, 2020</p><p><em>This post is my first post in my </em><strong><em>explained</em></strong><em> series — where I take a concept from software engineering and explain it as succinctly and simply as possible. The goal is to have three sections for each explanation — 1. What is it? 2. Example 3. When would I use it?.</em></p><p><em>The goal is to help you increase your breadth of knowledge whilst still giving you some level of depth.</em></p><p><em>I would love your feedback if you have any on what other topics you’d like to see — you can reach me on twitter: </em><a href="https://twitter.com/harryblucas"><em>@harryblucas</em></a></p><h3>What is it?</h3><p>This is another one of those terms that sounds fancy, but really isn’t.</p><p>Function composition is process of <strong>combining two or more functions together</strong> into a <strong>single function</strong>, that produces the same result as if the functions were called independently and the result was passed from one to another.</p><p>That’s it.</p><p>To break it down — we take two (or more) functions, and pass the result of calling the first function with the supplied argument to the second function and so forth.</p><pre>const func3 = (value) =&gt; func2(func1(value))</pre><h3>An Example</h3><p>To simplify it, you can think of a function as an individual Lego block.</p><p>Function composition is then the process of combining one or more of these Lego blocks in to a larger structure.</p><p>For an example, let’s pretend we want to take a string, and count how many words start with the letter s.</p><p>First we create our individual Lego blocks (functions) that we are going to use to build the bigger block (function).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sOBqIZEkeKxY6nmP.jpg" /></figure><p>Which in code would be defined as:</p><pre>const split = (string) =&gt; string.split(&quot; &quot;)<br><br>const downCase = (list) =&gt; list.map((s) =&gt; s.toLowerCase())<br><br>const countS = (list) =&gt; list.reduce((count, word) =&gt; word[0] === &#39;s&#39; ? count + 1 : count, 0)</pre><p>Since these blocks are all the same shapes and have matching side — we can combine them together 💪</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/713/0*QWmqbXh4Aam8JJoj.jpg" /></figure><p>To do this in code:</p><pre>const wordsStartWithS = (string) =&gt; countS(downcase(split(string)))</pre><p>To read a function composed like this, you read it inside out.</p><p>However this isn’t as clear as it could be, so instead to make it a bit clearer, what a lot of functional programmers will do is introduce a compose or pipe function, which is super simple and makes the above, even clearer. To read an example of what a pipe function is you can <a href="https://struggling.dev/pipe-functions-explained">click here</a>.</p><pre>const pipe = (...fns) =&gt; (initValue) =&gt; fns.reduce((val, func) =&gt; func(val), initValue)</pre><p>Then we would use it like this:</p><pre>const wordsStartWithS = (string) =&gt; pipe(<br>  split,<br>  downcase,<br>  countS<br>)(string)</pre><p>Which is now a very readable function declaration that reads like a recipe when cooking (split then downcase then count), <strong>all without a variable assignment</strong>.</p><p>Again just to reiterate what we’ve done here is combine multiple, smaller functions, into a single larger function.</p><p>Now of course this isn’t the most robust example, but it is a start for getting your head around exactly what function composition is.</p><h3>When should I use it?</h3><p>You can use function composition to compose your entire application together. You can take your smallest functions and combine them together into larger business cases, you can then combine these functions together with other business cases to build a use case — you get the picture — it’s just functions all the way down.</p><p>The great thing about adopting more of a function composition mindset is that it forces you to break apart your functions into their smallest component pieces.</p><p>This promotes composability, reuse and is in my opinion much easier to read.</p><p>For example, if we wrote this the traditional way:</p><pre>const wordsStartWithS = (string) =&gt; string<br>  .split(&quot; &quot;)<br>  .map((s) =&gt; s.toLowerCase)<br>  .reduce((count, word) =&gt; word[0] === &quot;s&quot; ? count + 1 : count, 0)</pre><p>Whilst we have gained the benefit of having it all in one line, we have:</p><ul><li>Made it harder to read (lots of extra noise)</li><li>Harder for the reader to grok intent (they have to parse every single method call to figure out what it’s doing)</li><li>Removed any possibility of reusing those component parts (which leads to duplicate code)</li></ul><p><em>Originally published at </em><a href="https://struggling.dev/what-is-function-composition/"><em>https://struggling.dev</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=541511d37108" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elixir for Rubyists: Charlists |> Binary’s |> Strings | IOLists]]></title>
            <link>https://medium.com/@harry_dev/elixir-for-rubyists-charlists-binarys-strings-iolists-eeacf38db999?source=rss-e609451a5e39------2</link>
            <guid isPermaLink="false">https://medium.com/p/eeacf38db999</guid>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Harrison Lucas]]></dc:creator>
            <pubDate>Thu, 24 Nov 2016 10:20:32 GMT</pubDate>
            <atom:updated>2016-11-24T21:21:20.457Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the first <em>WTF? </em>moments every new Elixir developer experiences is trying to figure out how the hell entering[97,98,99] in IEx comes out as &#39;abc&#39;.</p><p>There are countless questions on Stackoverflow asking why this is happening because to most people, it makes no sense.</p><p>This generally leads to the realization that:</p><ul><li>A. Elixir treats &#39;&#39; and &quot;&quot; completely differently</li><li>B. Strings in Elixir are not like they are in Ruby</li></ul><p>Hopefully I am able to shed some light for those coming from Ruby on how exactly strings, binaries, bitstrings etc. work in the wonderful world of Elixir (and the BEAM).</p><h3>‘ &amp; “ are different things</h3><p>In ruby:</p><pre>irb&gt; &#39;string&#39; == &quot;string&quot;<br># =&gt; true</pre><p>In that ruby doesn’t care whether or not you single quote or double quote a string (except in the cases of string interpolation).</p><p>However, if you tried that in elixir, you would get the opposite answer:</p><pre>iex(1)&gt; &#39;string&#39; == &quot;string&quot;<br># =&gt; false</pre><p><strong>So what’s going on here?</strong></p><p>The first thing you should know is that Elixir (like ruby) <a href="https://www.bignerdranch.com/blog/unicode-and-utf-8-explained/">uses UTF-8</a> as its default encoding.</p><p>The next thing you should know is that Elixir treats anything between single quotes as <strong>a list of codepoints, </strong>called a <em>charlist.</em></p><p>So basically, when you enter &#39;hello&#39; , Elixir really treats this as a list of their corresponding UTF-8 codepoints, i.e. [104, 101, 108, 108, 111] . In fact, if you enter that list of integers into an IEx session it will output &#39;hello&#39; .</p><p>So what are they used for?</p><p>Well from the Elixir Docs:</p><blockquote>In practice, char lists are used mostly when interfacing with Erlang, in particular old libraries that do not accept binaries as arguments.</blockquote><p>However there are some other uses for them.</p><p>For example, let’s say you wanted to create a small program that checks whether or not a word is an anagram of another word.</p><p>In Elixir this is pretty easy to solve:</p><pre><strong>defmodule</strong> Anagram <strong>do</strong><br><br>  <strong>def</strong> match(base, candidates) when is_list(candidates), <strong>do<br>    </strong>base_fingerprint = fingerprint(base)</pre><pre>    candidates<br>    <strong>|&gt;</strong> Enum<strong>.</strong>filter(<strong>&amp;</strong>(fingerprint(&amp;1) <strong>==</strong> base_fingerprint))<br>  <strong>end</strong><br><br>  <strong>defp</strong> fingerprint(string) <strong>do</strong><br>    string<br>    <strong>|&gt;</strong> String<strong>.</strong>downcase<br>    <strong>|&gt;</strong> String<strong>.</strong>to_charlist<br>    <strong>|&gt;</strong> Enum<strong>.</strong>sort<br>  <strong>end</strong><br><strong>end</strong></pre><pre><strong>iex(1)&gt;</strong> Anagram.match(&quot;dog&quot;, [&quot;apple&quot;, &quot;cow&quot;, &quot;god&quot;])<br>[&quot;god&quot;]</pre><p>That’s all the Elixir code you need to find an anagram of a word.</p><p>Let’s run through this line by line.</p><ol><li>match/2 is called passing in two arguments. A base word which we want to find anagrams of and a list of words to check (candidates).</li><li>Those candidates are then piped through to the Enum.filter function. This function checks whether the fingerprint of a candidate word, matches the fingerprint of the base word.</li><li>To check this, fingerprint/1 is invoked on each word. This method takes in the string to check, downcases it and then converts it to a charlist. E.g. “DoG” =&gt; “dog” =&gt; [100, 111, 103]. Finally, it sorts that list. [100, 111, 103] |&gt; [100, 103, 111]</li><li>This leaves us with two lists of sorted integers. If these two match, then the words must be anagrams of each other.</li></ol><p><em>Try to implement something as succinct as that in Ruby! Edit: Probably should of made this rhetorical, nothing’s going to be Ruby in short syntax.</em></p><p>Ultimately, you probably won’t find yourself using charlists that much in Elixir, but it is definitely a benefit to be aware of them and cases like the above where you can make use of them.</p><h3><strong>Binaries &amp; BitStrings &amp; Strings</strong></h3><p>Now just to confuse you. Remember this from before?</p><pre>&#39;a&#39; == &quot;a&quot; <br># =&gt; false</pre><p>This is pretty much the same as saying 97 == &quot;a&quot; #=&gt; false which makes sense.</p><p>Well in Elixir, this is true:</p><pre>&lt;&lt;97&gt;&gt; == &quot;a&quot;<br># =&gt; true</pre><p>This is probably a tad confusing considering just above, 97 != &quot;a&quot; .</p><p>This is because a &lt;&lt; &gt;&gt; is considered a binary in Elixir, which (from the lang docs)</p><blockquote>A binary is a sequence of bytes. Those bytes can be organized in any way, even in a sequence that does not make them a valid string.</blockquote><p>So basically, anything inside the &lt;&lt; &gt;&gt; operator is just a comma delimited <strong>sequence </strong>of bytes.</p><p>A string is also sequence of bytes, the only difference between the two being that a string’s sequence of bytes <strong>happen to a UTF-8 valid sequence of bytes.</strong></p><p>To see whether or not a string is valid UTF-8, you can simply run:</p><pre>iex(1)&gt; String.valid?(&lt;&lt;226, 136, 134&gt;&gt;)<br><strong># =&gt; true</strong></pre><pre>iex(2)&gt; &lt;&lt;226, 136, 134&gt;&gt;<br><strong># =&gt; &quot;∆&quot;</strong></pre><pre>iex(1)&gt; String.valid?(&lt;&lt;226, 16, 134&gt;&gt;)<br><strong># =&gt; false</strong></pre><p>You can see there, that the original 3-byte binary was valid UTF-8 (codepoint 8710, the triangle) , but when changing the middle byte from 136 -&gt; 16, the binary became invalid UTF-8.</p><p>To see the binary representation of a string, simply open up IEx and call i(string) e.g.</p><pre><strong>iex(5)&gt;</strong> i &quot;dogs&quot;<br>Term<br>  &quot;dogs&quot;<br>Data type<br>  BitString<br>Byte size<br>  4<br>Description<br>  This is a string: a UTF-8 encoded binary. It&#39;s printed surrounded by<br>  &quot;double quotes&quot; because all UTF-8 encoded codepoints in it are printable.<br>Raw representation<br>  &lt;&lt;100, 111, 103, 115&gt;&gt;<br>Reference modules<br>  String, :binary</pre><p>Look at all that lovely information!</p><p>You can also use the above to dictate the size of the binary. By default, the numbers are stored as 8-bit integers. To specify that though you would write:</p><pre>iex(1)&gt; &lt;&lt;97::size(8)&gt;&gt;<br>&quot;a&quot;</pre><p>If we entered a number greater then what’s possible in 8-bits, it overflows. E.g.</p><pre>iex(1)&gt; &lt;&lt;257&gt;&gt;<br>&lt;&lt;1&gt;&gt;</pre><p>But if we increase the size to 16 bits we get the full binary representation:</p><pre>iex(1)&gt; &lt;&lt;257::size(16)&gt;&gt;<br>&lt;&lt;1, 1&gt;&gt;</pre><p><strong>What’s the benefit of being able to access a string in binary form?</strong></p><p>Well if you remember from my <a href="https://hackernoon.com/elixir-for-rubyists-pattern-matching-and-recursion-37a4416ba9c#.e9a28mxpo">previous</a> post on pattern-matching there was this syntax for destructuring lists:</p><pre>iex(1)&gt; [ head | tail ] = [1,2,3,4]</pre><pre>iex(2)&gt; head<br><strong># =&gt; 1</strong></pre><pre>iex(3)&gt; tail<br><strong># =&gt; [2,3,4]</strong></pre><p>This allowed us to grab the head of the list, do something with it then recurse on the tail.</p><p>Well, binary’s allow us to do that same thing with strings!</p><p>Let’s say you wanted to take a string, and remove all the vowels.</p><p>Below is an example using Binary pattern matching and recursion in elixir.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7aeb98e89dbafe3392368cd48a93e544/href">https://medium.com/media/7aeb98e89dbafe3392368cd48a93e544/href</a></iframe><p>There is a lot going on here.</p><p>Again, if you read my previous article, the best place to start with a tail-recursive function is the end case. This occurs when your function has nothing left to process and needs to terminate.</p><p>In our case, it is when we have gone through each letter and there are none left.</p><p>The next function head is where the binary-pattern matching occurs. Here we are matching head to the first byte (“<strong>binary-size(1)</strong>”) of the passed in string.</p><p>Now since this is UTF-8, we could potentially get letters such as æ, which are in fact two bytes, however for the purpose of this tutorial, assume that all strings fed into this machine are going to be [0–9a-zA-Z].</p><p>We then match rest to an arbitrary length of remaining bytes using the ::binary size descriptor. <em>Note you can only use </em><em>::binary at the end of a pattern match and not the beginning or middle.</em></p><p>We then pass the value matched to head and send it on it’s merry way to the check_vowel/1 method, which returns and empty string if it is and the letter if it isn’t.</p><p>Finally it concatenates this value returned form the check_vowel method with the accumulator and calling the strip_vowels/2 method once again, this time passing in the rest value.</p><p>Running this in IEx gives us:</p><pre>iex(1)&gt; Binary.strip_vowels(&quot;Ned Kelly was an australian bushranger.&quot;)<br>&quot;Nd Klly ws n strln bshrngr.&quot;</pre><p>Now this isn’t that hard to replicate in ruby actually, e.g:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/704a061bbe9016ac0b8097b82194f14f/href">https://medium.com/media/704a061bbe9016ac0b8097b82194f14f/href</a></iframe><p>However, this doesn’t look anywhere near as nice and performs no where near as well as the elixir version. <strong>Edit: Without using Regex, mean’t to be an illustration of recursion (which is probably unfair comparing ruby to a functional lang).</strong></p><p>We can even tidy the elixir version up some more to make it even shorter.</p><p>Since strings in Elixir are also binary’s we can also use them between the &lt;&lt; and &gt;&gt; of a binary operator. This allows us to pattern match on specific values. E.g.</p><pre>iex(2)&gt; &lt;&lt;&quot;Ned&quot;, rest::binary&gt;&gt; = &quot;Ned Kelly&quot;<br>&quot;Ned Kelly&quot;<br>iex(3)&gt; rest<br>&quot; Kelly&quot;</pre><p>So applying the above logic, our vowel stripping machine becomes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/544b19b6bea88a34ad4edb9ca7a8f19e/href">https://medium.com/media/544b19b6bea88a34ad4edb9ca7a8f19e/href</a></iframe><p>There are plenty more examples of using binary’s in elixir and their benefits:</p><ul><li><a href="http://benjamintan.io/blog/2014/06/10/elixir-bit-syntax-and-id3/">Reading the Data from an MP3 ID3 tag</a></li><li><a href="http://www.rokkincat.com/blog/2016/05/09/parsing-udp-in-elixir-with-binary-pattern-matching">Decoding UDP packets</a></li><li><a href="http://www.zohaib.me/binary-pattern-matching-in-elixir/">Parsing PNG’s</a></li><li>Or like above, efficient text parsing</li></ul><h3>IO Lists</h3><p>IO lists are another powerful feature for dealing with strings in Elixir. More importantly, for dealing with I/O operations and strings. <a href="https://www.bignerdranch.com/blog/elixir-and-io-lists-part-1-building-output-efficiently/">Nathan Long from Big Nerd Ranch</a> explains it well:</p><blockquote>An IO list just means “a list of things suitable for input/output operations”, like strings or <a href="https://www.bignerdranch.com/blog/unicode-and-utf-8-explained/">codepoints</a>. Functions like IO.puts/1 and File.write/2 accept “IO data”, which can be either a simple string or an IO list.</blockquote><p><em>But what is an iolist?</em></p><p>In it’s simplest form, an IO list looks just like any other list in Elixir:</p><pre>name = &quot;Harry&quot;</pre><pre>IO.puts [&quot;Hello &quot;, name]</pre><pre>#=&gt; Hello Harry</pre><p>Now you may be asking what the benefit of the above is, over the below:</p><pre>name = &quot;Harry&quot;</pre><pre>IO.puts &quot;Hello &quot; &lt;&gt; name</pre><pre>#=&gt; Hello Harry</pre><pre>#...or</pre><pre>IO.puts &quot;Hello #{name}&quot;</pre><pre>#=&gt; Hello Harry</pre><p>From the outset, it might not look like there is much difference considering they all output the same thing, however, the way in which they reach that outcome if fairly different.</p><p>In order to do one of the above concatenation examples the BEAM has to do a few things:</p><ul><li>Allocate Memory in order to create the new strings</li><li>Concatenate the two new strings together into a third string</li><li>Run the garbage collector to cleanup the old ones</li></ul><p>Whereas, using an IO list the BEAM:</p><ul><li>Doesn’t Concatenate the strings</li><li>Doesn’t create a new string to store the final result = less ram</li><li>Doesn’t have to garbage collect that final string = less cpu</li></ul><p>As in the words of Nathan Long:</p><blockquote><strong>All the BEAM had to do was to ask the OS to copy each byte of data to the file</strong>.</blockquote><blockquote><strong>The only place where the concatenation happens is in the file itself</strong>.</blockquote><p>So essentially, instead of having the BEAM create the string, the BEAM ask’s the OS to grab these three chunks of memory and throw them into the file, or in this case, the Stdout (although correct me if I’m wrong here).</p><p>In fact, <a href="https://www.bignerdranch.com/blog/elixir-and-io-lists-part-2-io-lists-in-phoenix/">using IO lists is how the phoenix framework is able to generate lightning fast templates</a> (when compared with Rails).</p><h3>Further Reading</h3><p>This is just the basics of dealing with strings in Elixir. If you want to know more, then I highly recommend checking out the following:</p><ul><li><a href="https://www.bignerdranch.com/blog/elixir-and-io-lists-part-1-building-output-efficiently/">https://www.bignerdranch.com/blog/elixir-and-io-lists-part-1-building-output-efficiently/</a></li><li><a href="https://www.bignerdranch.com/blog/elixir-and-io-lists-part-2-io-lists-in-phoenix/">https://www.bignerdranch.com/blog/elixir-and-io-lists-part-2-io-lists-in-phoenix/</a></li><li><a href="http://www.evanmiller.org/elixir-ram-and-the-template-of-doom.html">http://www.evanmiller.org/elixir-ram-and-the-template-of-doom.html</a></li></ul><p><strong>AND DEFINITELY WATCH THE FOLLOWING:</strong></p><p><a href="https://www.youtube.com/watch?v=zZxBL-lV9uA&amp;list=PLE7tQUdRKcyYoiEKWny0Jj72iu564bVFD&amp;index=16"><strong>https://www.youtube.com/watch?v=zZxBL-lV9uA&amp;list=PLE7tQUdRKcyYoiEKWny0Jj72iu564bVFD&amp;index=16</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eeacf38db999" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elixir for Rubyists: Pattern matching and Recursion]]></title>
            <link>https://medium.com/hackernoon/elixir-for-rubyists-pattern-matching-and-recursion-37a4416ba9c?source=rss-e609451a5e39------2</link>
            <guid isPermaLink="false">https://medium.com/p/37a4416ba9c</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Harrison Lucas]]></dc:creator>
            <pubDate>Mon, 07 Nov 2016 02:06:25 GMT</pubDate>
            <atom:updated>2019-05-02T06:35:27.033Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Note: This was taken from my blog over at http://www.weeklycommit.com</em></p><p>This blog post series is going to go through the basics of <a href="https://hackernoon.com/tagged/elixir">Elixir</a> for someone coming from the Ruby <a href="https://hackernoon.com/tagged/programming">programming</a> language.</p><p>Why?</p><p>Well from the outside, Elixir and Ruby <em>look</em> very similar syntactically. However, that is all they really share in common.</p><p>When you start using Elixir you will realise how different the two langauges are, I mean for starters, Elixir is a functional programming language where as Ruby is object orientated (although it can be argued that Elixir is the <a href="http://tech.noredink.com/post/142689001488/the-most-object-oriented-language">most Objected Oriented language out of them all</a>, but that’s an argument for another time).</p><p>This series isn’t going to look at the reasons why one would want to use Elixir over Ruby, but if you want to get an idea, check out these posts:</p><ul><li><a href="https://www.quora.com/Will-Elixir-Phoenix-destroy-Ruby-on-Rails">Will Phoenix Destroy Rails — Quora</a></li><li><a href="http://blog.carbonfive.com/2016/04/19/elixir-and-phoenix-the-future-of-web-apis-and-apps/">Elixir and Phoenix — CarbonFive</a></li><li><a href="https://medium.com/@kenmazaika/why-im-betting-on-elixir-7c8f847b58#.p010tmtsx">Why I’m betting on Elixir — Medium</a></li></ul><h3>Pattern Matching</h3><p>Alright so with that out of the way lets jump into one of my favorite features of Elixir — <em>pattern matching</em>.</p><p>If you have never used pattern matching before, be warned, once you start, you can’t go back.</p><p>At it’s very simplest form, pattern matching is a way to <strong>destructure complex data types</strong>.</p><p>But what does that mean?</p><p>Well with an example, let’s say you had a map (the Elixir equivalent of a Ruby Hash {key: &quot;val&quot;}):</p><p>%{age: 22, name: &quot;Harry&quot;}</p><p>How would you go about getting the value of the :name key?</p><p>In ruby, you might do something like this:</p><pre>person = {name: &quot;harry&quot;, age: 22}  <br>person[:name]  <br># =&gt; &quot;harry&quot;</pre><p>However, in Elixir you could do this instead:</p><pre>%{name: name} = %{age: 22, name: &quot;Harry&quot;}<br>name  <br># =&gt; &quot;Harry&quot;</pre><p>The reason this works is because that the = operator in Elixir doesn&#39;t mean assign, but rather match. Therefore the above statement is saying:</p><p>Make %{name: name} match %{age: 22, name: &quot;Harry&quot;}.</p><p>So how does it do this?</p><p>Well the process is something like this:</p><ol><li>It see’s that both the left and right hand side are the same data type (maps)</li><li>It then see’s that both maps have a key called :name so they match</li><li>It then notices that the map on the left does not have a value for the :name key however, the map on the right does have a value. Therefore, it must mean that name is the same as &quot;Harry&quot;.</li></ol><p>Another way to think of it is like algebra. E.g.</p><p>1 + x = 1 + 2</p><p>What’s the value of X?</p><p>2</p><p>But you can do even more then that. Let’s say that with the above, we wanted to have another person and see if it was the same as the second one. In Ruby, you might do something like this:</p><pre>person = {name: &quot;harry&quot;, age: 22}  <br>person2 = {name: &quot;glen&quot;, age: 33}</pre><pre>person[:name] == person2[:name]</pre><p>With Elixir, you could do this instead:</p><pre>%{name: name} = {age: 22, name: &quot;harry&quot;}<br>%{name: ^name} = {age: 33, name: &quot;glen&quot;}</pre><pre># =&gt; Error. No match on right hand side....</pre><p>What we are doing here is using the ‘pin’ operator. This is basically saying, that we matched the value of name to a particular value before, so use that value instead of trying to match against a new one.</p><p>The benefits of this may not be obvious straight away. However, for me the power of pattern matching really shines when writing functions.</p><p>Let’s say you have a method that takes in a hash and multiplies two numbers together, <strong>only if they are equal</strong>. One implementation in Ruby could be:</p><pre>def multiply_equal(n1, n2)  <br> raise &quot;Not equal&quot; if n1 != n2<br> n1 * n2<br>end</pre><p>Pretty simple.</p><p>However, we can do better.</p><p>In Elixir, using pattern matching you could instead do something like this:</p><pre>def multiply_equal(x,x) do  <br>  x * x<br>end</pre><pre>def multiply_equal(_,_) do  <br>  raise &quot;Not Equal&quot;<br>end</pre><pre># or in one line</pre><pre>def multiply_equal(x,x), do: x * x  <br>def multiply_eqaul(_,_), do: raise &quot;Not Equal&quot;</pre><p>So what’s going on here? It might not be evident at first but one thing to note, is that in elixir, you <em>can define a function multiple times</em>.</p><p>In the above example, we are defining two, two argument multiply_equal functions. The first function, will only ever be invoked if both arguments are the same, the second function, will be invoked for every other combination of two arguments.</p><p>Since a variable can only have one value, when we define a function head with the same two arguements e.g. (x,x) we are saying that this function is only ever to be invoked, if both arguments are the same. It makes no assumptions about the data types that get passed in.</p><p>E.g., these will all be invoked by the first function head</p><pre>multiply_equal( [1,2,3,4], [1,2,3,4] )  <br>multiply_equal( &quot;foo&quot;, &quot;foo&quot; )  <br>multiply_equal( &#39;N&#39;, [78] )</pre><p>(click <a href="http://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#char-lists">here</a> to see why that last one matches)</p><p>The benefits of this might not be immediately clear, but trust me, as you dive more and more into Elixir and Phoenix you will come to love this more then life itself.</p><h3>Putting it all together</h3><p>To illustrate how pattern matching and recursion can be used together we are going to work through creating our own multiply function.</p><p>Basically, we are going to replicate the * function common in most languages.</p><p>Now, it took me longer then I would like to admit to realise that multiplication is just addition, multiple times.</p><p>So how can we go about creating this using pattern matching and recursion?</p><p>Well do this, we are going to define 2 functions. In elixir, if a function has the same name but different arity (i.e. number of arguements, they are considered seperate functions).</p><p>So:</p><pre>def func(a,b)  <br>  IO.puts a<br>end</pre><p>Is a different function to:</p><pre>def func(a)  <br>  IO.puts a<br>end</pre><p>To that effect we are going to have two functions.</p><p>multiply/2 which is the one that is publicly exposed and consumed by other modules, and mulitply/3 which is called by the multiply/2function.</p><p>So basically we need first create a function which takes two numbers which are to be multiplied and call our second multiply/3 function passing in an accumulator. We will use the accumulator to track the end result from the function.</p><pre>def multiply(n1,n2) do  <br>  mutliply(n1,n2,0)<br>end</pre><p>(We could even write this on a single line)</p><pre>def multiply(n1,n2), do: multiply(n1,n2,0)</pre><p>Now we need to actually implement the multiplier logic.</p><p>When dealing with recursion in elixir it is always best to write your success or end function head first, so that the function actually has an exit point (e.g. won’t run into infinity).</p><p>The way our recursive function is going to work is that we are going to add n1 to the accumulator (which starts at 0) for every value of n2 and subtract 1 from n2 until we reach 0.</p><p>So basically:</p><p>10 * 10 would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aHvayUjvp604Q-BZqLTjOw.png" /></figure><p>From this, we can tell that our success or end case, is when n2 is equal to 0.</p><p>Therefore, the function will look like:</p><pre>def multiply(n1,n2), do: multiply(n1,n2,0)</pre><pre>defp multiply(_n1, 0, acc), do: acc</pre><p>(remember defp makes it a private method).</p><p>So here we are saying that if multiply/3 is called and the value of n2is equal to 0, then don&#39;t iterate again, instead just return the current value of the accumulator.</p><p>This is our end result. Now we need to implement the iteration logic.</p><p>Now remember we need to add the value of n1 to acc and subtract the value of n2 by 1.</p><p>That looks like this:</p><pre>def multiply(n1,n2,acc) do  <br>  new_acc = n1 + acc<br>  new_n2 = n2 - 1</pre><pre>  #recursive call<br>  multiply(n1, new_n2, new_acc)<br>end</pre><p>That last line calls the same function again with the new values of n2and acc.</p><p>However, this looks pretty messy.. let’s refactor it a bit to look more proper.</p><pre>def multiply(n1,n2,acc) do  <br>  multiply(n1, n2 - 1, acc + n1)<br>end</pre><p>We can even right this on a single line:</p><pre>def multiply(n1,n2,acc), do: multiply(n1, n2 - 1, acc + n1)</pre><p>And that’s it, we can now multiply two numbers together in just three lines of Elixir.</p><p>The final implementation looks like:</p><pre>def multiply(n1,n2), do: multiply(n1,n2,0)  <br>defp multiply(_n1, 0, acc), do: acc  <br>defp multiply(n1,n2,acc), do: multiply(n1, n2 - 1, acc + n1)</pre><p>Pretty neat huh?</p><p>Now obviously this won’t handle negative numbers, but that’s pretty easy to handle as well.</p><p>Again, there is no need to use if statements here, instead we can use <a href="http://elixir-lang.org/getting-started/case-cond-and-if.html#expressions-in-guard-clauses"><em>guard_clauses</em></a>.</p><p>Guard clauses are essentially, parts of a function head that add conditions to the passed in arguments. It’s another way of pattern matching basically.</p><p>In our case, this be achieved by checking if the value of n2 is less then 0. If it is, we simply reverse the operations on n2 and acc (e.g. we add 1 to n2 and subtract n1 from acc).</p><p>This is what that head will look like:</p><pre>def multiply(n1,n2,acc) when n2 &lt; 0 do  <br>  multiply(n1, n2 + 1, acc - n1)<br>end</pre><pre># or on one line</pre><pre>def multiply(n1,n2,acc) when n2 &lt; 0, do: multiply(n1, n2 + 1, acc - n1)</pre><p>That when n2 &lt; 0 part is the guard clause. We are saying here that first only call this function head if the arguments can be matched to n1,n2,acc (which they can) <strong>AND</strong> if n2 is less then 0.</p><p>Now because this is recursive, functions in Elixir are checked in the order that they are defined. So e.g. if we had this method below our original one, it would never get called:</p><pre>def multiply(n1,n2), do: multiply(n1,n2,0) # 1</pre><pre>  defp multiply(_n1,0,acc), do: acc # 2<br>  defp multiply(n1,n2,acc), do: multiply(n1,n2 - 1, acc + n1) #3<br>  defp multiply(n1,n2,acc) when n2 &lt; 0, do: multiply(n1,n2 + 1, acc - n1) #4 - Never called</pre><p>This is because function head #3 will always match the arguments (assuming it passes through function head #2). Therefore, all we need to do is move #4 above #3 e.g.</p><pre>def multiply(n1,n2), do: multiply(n1,n2,0) </pre><pre>  defp multiply(_n1,0,acc), do: acc<br>  defp multiply(n1,n2,acc) when n2 &lt; 0, do: multiply(n1,n2 + 1, acc - n1)<br>  defp multiply(n1,n2,acc), do: multiply(n1,n2 - 1, acc + n1)</pre><p>Now we have a fully working mutliply function written from scratch, using pattern matching and recursion.</p><p>Below is some sample output(note the method was defined under a module called Recurse:</p><pre>iex(1)&gt; Recurse.multiply(2,-3)  <br>#=&gt; -6<br>iex(2)&gt; Recurse.multiply(-2,-3)  <br>#=&gt; 6<br>iex(3)&gt; Recurse.multiply(2, 3)  <br>#=&gt; 6<br>iex(4)&gt; Recurse.multiply(-2, 3)  <br>#=&gt; -6</pre><p>If you have enjoyed this little guide or have any questions, let me know in the comments below. I’d be happy to help you out!</p><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a>family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp;sponsorship</a> opportunities.</blockquote><blockquote>To learn more, <a href="https://goo.gl/4ofytp">read our about page</a>, <a href="http://bit.ly/HackernoonFB">like/message us on Facebook</a>, or simply, <a href="https://goo.gl/k7XYbx">tweet/DM @HackerNoon.</a></blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37a4416ba9c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/elixir-for-rubyists-pattern-matching-and-recursion-37a4416ba9c">Elixir for Rubyists: Pattern matching and Recursion</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>