<?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 Matthew Reid on Medium]]></title>
        <description><![CDATA[Stories by Matthew Reid on Medium]]></description>
        <link>https://medium.com/@matthew.reid.os1?source=rss-8e20f3b3037c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*OgZX3nfgpIDj4FruWZT5Dw.gif</url>
            <title>Stories by Matthew Reid on Medium</title>
            <link>https://medium.com/@matthew.reid.os1?source=rss-8e20f3b3037c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 11:36:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@matthew.reid.os1/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[Exploring The Look and Say Sequence Using Javascript]]></title>
            <link>https://medium.com/@matthew.reid.os1/exploring-the-look-and-say-sequence-using-javascript-6a1aee8a88a3?source=rss-8e20f3b3037c------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a1aee8a88a3</guid>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[encoding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Matthew Reid]]></dc:creator>
            <pubDate>Mon, 26 Aug 2019 21:57:20 GMT</pubDate>
            <atom:updated>2019-08-28T12:24:08.982Z</atom:updated>
            <content:encoded><![CDATA[<p>Today, I’m going to explore a classic mathematical sequence and show how to implement it using Javascript.</p><p>The Look &amp; Say sequence, also known as the Cuckoo’s Egg, the Conway Sequence, the <a href="https://oeis.org/A005150">A005150 sequence</a>, and more. It is a similar idea as<a href="https://www.geeksforgeeks.org/run-length-encoding/"> run-length encoding</a>, which has been around for many years and had its heyday with CompuServe image compression and was also used with fax-machines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*ioxOQzkUu98fFZfSX4oW7A.jpeg" /></figure><h3>Let’s get started. How does the sequence work?</h3><p>The Look &amp; Say sequence is quite easy to understand. I explained it to my dog in about 10 seconds and she stayed focused the entire time. That’s about how long it should take anyone to understand the concept. Implementing it manually beyond sixteen or seventeen iterations, however, is a little tricky unless you have a LOT of time and a HUGE whiteboard.</p><p>Here’s the basic idea:</p><ul><li><em>Look</em> at the number group the consecutive same digits visually</li><li><em>Say</em> the number of consecutive digits followed by the digit you count</li><li>This is now the next number in the Look and Say sequence</li></ul><p>I’m going to start with the number 1, because that is what the referenced version of the sequence is based on. There are other versions based on 2, 3, etc. but 1 is what we are going to use here. None are harder than the others; they’re just different.</p><h4>1</h4><p>What is 1? It is <strong><em>one one</em></strong>.</p><h4>11</h4><p>What is 11? You might think eleven. But with look and say. It is <strong><em>two ones</em></strong>.</p><h4>21</h4><p>What is 21? You‘re right to say twenty-one. But… it’s also <strong><em>one two</em></strong> &amp; <strong><em>one one</em></strong>.</p><h4>1211</h4><p>What is 1211? It is <strong><em>one one</em></strong>, <strong><em>one two</em></strong>, &amp; <strong><em>two ones</em></strong>. Catching the pattern yet? Probably so. But let’s look at one more iteration to be sure.</p><h4>111221</h4><p>Now we have <strong><em>three ones</em></strong>, <strong><em>two twos</em></strong>, &amp; <strong><em>one one</em></strong>. Also known as…</p><h4>312211</h4><p>This is all great and really fun, but what happens when you want to see the thirteenth iteration?</p><p><strong>11131221131211131231121113112221121321132132211331222113112211</strong></p><p>That’s 62 characters on the 13th. The sequence passes 1000 characters on the 25th coming in with a whopping 1182 characters. Big whiteboard, right? Just wait. It gets much crazier really fast.</p><p>I started playing around with this sequence on a rather large whiteboard and decided around the 15th iteration to use some code to see how far I could get with a basic machine. After about a minute, on the 65th iteration, it threw an error that following error: <em>Javascript heap out of memory</em>. It blew through the 1.7GB that V8 allocates by default. Then, I ran the file with a command that allowed for all of my available MacBook Pro memory to be available After doing so, I was able to get 69 iterations, which is comprised of 137,842,560 Digits and took a little over 2 minutes. Here are the last 10 iterations I was able to get to:</p><ul><li>Iteration 60: <strong>12,680,852</strong> Digits</li><li>Iteration 61: <strong>16,530,884</strong> Digits</li><li>Iteration 62: <strong>21,549,544</strong> Digits</li><li>Iteration 63: <strong>28,091,184</strong> Digits</li><li>Iteration 64: <strong>36,619,162 </strong>Digits</li><li>Iteration 65: <strong>47,736,936 </strong>Digits</li><li>Iteration 66: <strong>62,226,614 </strong>Digits</li><li>Iteration 67: <strong>81,117,366</strong> Digits</li><li>Iteration 68: <strong>105,745,224</strong> Digits</li><li>Iteration 69: <strong>137,842,560</strong> Digits</li></ul><h3>Let’s take a look at the code.</h3><p>Here’s the code that I used to test it. It may not be the most efficient, but I don’t think that the issue of being stopped at 69 on my machine could be fixed by better code, because the seed is being overwritten on each iteration and should be the only thing stored in memory. It’s the 137 million+ digits in that 69th seed that are the issue. I would love to try it on a high memory AWS server. Maybe I’ll spin one up and see just how far I can go one day if I get some free credits!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*mmfqUXnbYoujvAXcsAXKTQ.png" /><figcaption>The look and say function</figcaption></figure><p>The function above is taking the digits, which is the starting or most recent seed, and comparing each digit to the next one and counting how many times duplicates occur and returning the result. The result is the next number in the sequence.</p><p>Below, we are telling the machine where to start and how many times we would like to iterate through the sequence. We’re then running the passed number of iterations, calling the lookAndSay function each time to build the new sequence based on the previous seed. Pretty cool, huh?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*36307iC3PrIaG2rM7bo1VQ.png" /></figure><p>I hope you’ve enjoyed this journey as much as me. It’s a fun sequence that’s easy for anyone to understand and play around with. I am planning on exploring more sequences in the near future so stay tuned!</p><p>Edit: Below is the command to increase your memory allocation for Node. I created a few functions in my Z shell config file to be able to easily choose between 4, 8, and 12 GBs. If you aren’t familiar with shell functions, the “$1” is your input. You would call the function in the terminal as <em>mem4 filename.js</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*71MTRlZCzerE8zmpIFxb5Q.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a1aee8a88a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is the Event Loop in JavaScript]]></title>
            <link>https://levelup.gitconnected.com/what-is-an-event-loop-69e1581ecdcc?source=rss-8e20f3b3037c------2</link>
            <guid isPermaLink="false">https://medium.com/p/69e1581ecdcc</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Matthew Reid]]></dc:creator>
            <pubDate>Fri, 02 Aug 2019 17:51:10 GMT</pubDate>
            <atom:updated>2019-11-12T13:17:25.483Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*QQE8gwh0QvP9-Ij0xablAA.gif" /></figure><p>Javascript is a single threaded language. It is interpreted by various engines, the most famous of which is V8 used in Chrome and Node.</p><p>However, the engine is really only doing a couple of things. It runs the code line by line and adds functions to the call stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RLbK8nM3pfLWPu4qIUaWww.png" /></figure><p>So what happens if you send a recursive function such as this?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*7-hqPbh3tEWELMWIlbPOxQ.png" /></figure><p>The engine is going to add each call to the stack until the stack is overloaded. You’ve probably seen a “Maximum call stack exceeded error” a time or two, right?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SsUMEKtyEcfyj67N6cWhzQ.png" /></figure><p>To be very clear, the event loop is not going to fix your bad recursion.</p><p>However, what if you want to use a canvassing library that performs extremely complex image transformations on an app. You certainly don’t want a user to have to wait on that to happen before the page renders. Otherwise, they will likely just leave your app. Thanks to a few features that handle asynchronous functions in Javascript, we have the ability to see things before each prior function has been called. These functions are managed by the event loop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*LvbUhFpMUeN9xoaazrp_gQ.jpeg" /></figure><p>What exactly does the event loop do:</p><ul><li>Code that waits for and dispatches events</li><li>Handles the events for its environment</li><li>Monitors the call stack and callback queue</li><li>Breaks down asynchronous code into ticks</li></ul><p>What is a tick — a tick a single iteration of the event loop that consists of phases that handle very specific things.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*YkyXaNiIixCaRkkpjAywkQ.png" /></figure><h4>Event Loop Phases</h4><p>Timers — Specify threshold after which callback may be executed</p><p>Pending Callbacks — Executes system operation callbacks</p><p>Idle/Prepare — Internal use only</p><p>Poll — Retrieves new events, executes I/O callbacks and timers</p><p>Check — setImmediate() callbacks invoked</p><p>Close Callbacks — Any specific ‘close’ callbacks</p><p>The event loop code is written in C and managed by a couple of companies. Libuv (Node) and Libevent (Google Chrome, Tor) are two of the main ones being used today. Here is an example of the Libuv event loop source code being applied to a specific environment, such as Node.js, which it was developed for.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NtNILGhqsjzoekld5PgisA.png" /><figcaption>The context being passed in is the environment and the loop being passed in is the libuv event loop.</figcaption></figure><p>So what is the above code actually doing? Let’s take a look at the pseudocode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I6f72a76phc_kAKdvOSQOQ.png" /></figure><p>It’s really a simple process handled by some brilliant code that has transformed the way we write applications. Without an event loop, applications would either be very slow or very boring.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*7E8nrGSkD9UuCbmd5uLgqA.jpeg" /></figure><p>So what again is Libuv?</p><ul><li>Libuv was developed for Node.js using ideas from Libev and Microsoft IOCP because Libev did not support Microsoft, and Node wanted a single dependency for all machines’ loop handling</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*5H0t7xXzy8rKhK14RxjsUg.png" /><figcaption>UNICORN VELOCIRAPTOR LIBRARY — aka Libuv</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69e1581ecdcc" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/what-is-an-event-loop-69e1581ecdcc">What is the Event Loop in JavaScript</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>