<?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 Little Bits of 1’s and 0&#39;s on Medium]]></title>
        <description><![CDATA[Stories by Little Bits of 1’s and 0&#39;s on Medium]]></description>
        <link>https://medium.com/@justinmanalad?source=rss-9febf9fe9b07------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*X__YGq5FpSxq3qJv7otIpQ.png</url>
            <title>Stories by Little Bits of 1’s and 0&amp;#39;s on Medium</title>
            <link>https://medium.com/@justinmanalad?source=rss-9febf9fe9b07------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 08:09:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@justinmanalad/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[Pre-save hooks in mongoose.js]]></title>
            <link>https://medium.com/@justinmanalad/pre-save-hooks-in-mongoose-js-cf1c0959dba2?source=rss-9febf9fe9b07------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf1c0959dba2</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Little Bits of 1’s and 0's]]></dc:creator>
            <pubDate>Sun, 10 Dec 2017 16:58:19 GMT</pubDate>
            <atom:updated>2017-12-10T16:58:19.883Z</atom:updated>
            <content:encoded><![CDATA[<p>Mongoose.js is a popular ODM for Express and MongoDB. It offers a great deal of power and flexibility and ease of use in defining “schemas”, models. and model methods in your application. One of the neat, but possibly confusing functionalities is its Middleware, also known as “pre” and “post” hooks that tie particular functions to particular lifecycle and query events. This middleware is defined on the schema level and can modify the query or the document itself as it executed. Today, we’re specifically looking at the Pre-save hook.</p><p>It might be obvious, but a pre-save hook is middleware that is executed when a document is saved. Let’s take a closer look at it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/992/1*S9MEieusFQZ56hboHIGEBw.png" /></figure><p>Again, this middleware is a function defined on the schema-level and is invoked with two arguments: the event trigger (as a string) and the callback function that is triggered for that particular event. The callback itself takes in an argument of a function, which we typically call next , and when invoked — advances the document/query to the next awaiting middleware (This is pretty much the exact same pattern you encounter when using Express middleware). Invoking this function is critical, mongoose will not auto-advance if this function is not called. Also, even though it may be obvious, keep in mind that next() will immediately proceed to the next step in the process and will not automatically wait for asynchronous functions to be resolved. We will look at one possible strategy to handle this, later on. BUT, speaking of this….</p><h4>This, that, and the other things….</h4><p>One thing to keep in mind here is that this inside of a pre-save hook is the document that is about to be saved. It has has a few helpful properties that we can use in our pre-save hook. For example, if we want to execute the middleware ONLY when this document is new, we can check the isNew property of the document that’s about to be saved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*cK9BWbPnkIQY0KDgufw1Pw.png" /></figure><p>Another thing we can do is if there are embedded subdocuments (documents saved within field within a MongoDB document) we can call the methods defined within those fields. In order to take a look at that, we can examine an app I’m currently building — a webapp to track my progress using the Wendlers 5/3/1 powerlifting program.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*trmm8s-bo0RPHDScOvd7_w.png" /></figure><p>Here, we’ve defined our programSchema with a cycles field that contains an array of cycle subdocs.</p><p>What we’re going to do is generate by pushing them directly into the cycles field using the attributes we have defined in our cycle schema. NOTE: That calling push() to create subdocs is a SYNCHRONOUS process. This is in contrast to create() which is an ASYNCHRONOUS process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IihtbYvva-1qD8VWjdj5wg.png" /></figure><p>This code is inside the body of a for loop that executes 12 times to represent the 12 cycles in wendler’s program. For this application, I want to also use these training maxes to generate workouts within a particular cycle. However, in the structure of this application, workouts are stored in their own collection and are this created when the generateWorkouts method is called. What I want is an array of reference IDs stored on a particular cycle, so I can populate those later. However, this can lead into a problem if I want to use the create method to make these workouts rather than push the documents into the these fields directly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EjLOet0RFcjKbBHvl72MZw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ohvsc0Rl84KeCa9rD9DeIA.png" /></figure><p>Observant readers me realize that this is an asynchronous function. So, if we do the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*drQdKG4BxCRBwU8SvAQbxQ.png" /></figure><p>this will be advanced to the next middleware likely before having those async functions resolved.</p><p>So, a conundrum — I have a synchronous for loop firing a bunch of asynchronous functions and I only want to advance to the next middleware when all those functions are resolved. What’s one to do? There are multiple ways to solve this, but a relatively easy way is with the magic of closures.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/371/1*CKDIv461CfLG3xsKmseAzQ.gif" /></figure><p>Stepping back into javascript land for a bit, closures are a way to give functions access to variables that are declared outside of this function. In our workout example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q7nt_LqUxkr2AIcmzozF7g.png" /></figure><p>This may be a bit confusing but the important thing is the completedCycleCounter that is declared outside of the function. What I have here is a counter that gets incremented in the then statement, and whenever that counter reaches four, it invokes a callback passed into the generateWorkouts method. This is an example of a closure!</p><p>But what is that callback being executed? Let’s step back a bit…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7upjBcDXB8RlZDCa2wcnOQ.png" /></figure><p>Instead of invoking the next() function immediately after the conclusion of the for loop, we have another counter that gets incremented every time that callback within generateWorkouts gets invoked. This way, we ensure that next() is ONLY invoked when all our asynchronous functions have resolved.</p><p>And that, folks, is a short intro to mongoose pre-save hooks. I hope this was informative and inspires you to explore this handy ODM even further!</p><h4>Further Reading:</h4><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures">Closures - JavaScript | MDN</a></li><li><a href="http://mongoosejs.com/docs/middleware.html">Mongoose v5.0.15: Middleware</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf1c0959dba2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tell me what you’re made of (Working with chart.js and react-chartjs-2)]]></title>
            <link>https://medium.com/@justinmanalad/tell-me-what-youre-made-of-working-with-chart-js-and-react-chartjs-2-13ae86f0d4cb?source=rss-9febf9fe9b07------2</link>
            <guid isPermaLink="false">https://medium.com/p/13ae86f0d4cb</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Little Bits of 1’s and 0's]]></dc:creator>
            <pubDate>Sat, 02 Dec 2017 23:10:04 GMT</pubDate>
            <atom:updated>2017-12-02T23:10:04.245Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the quotes that stays with me a quote from Carl Sagan, “We are made of star stuff.” Which means, there are parts of us that were forged in the crucible of a star’s core and parts of us that were birthed by the death of a large star. So, I decided to throw something together in React that will help me visualize how much of that stuff is actually part of us. So, to start, I looked up some references to how the proportions work out.</p><p>I used this site, and despite being a little on the old side — the format made it easy to scrape the data from the site. This provides a listing of the 60 elements that make up the majority of the mass of a 70kg human being.</p><p><a href="http://web2.airmail.net/uthman/elements_of_body.html">http://web2.airmail.net/uthman/elements_of_body.htm</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*PTkhkIJMJFRu4QiuUfs3kA.png" /><figcaption>What do they do with chemist when they die?</figcaption></figure><p>Scraping the site gave me a json list to work with, but with one complication — the units were not consistent. Some were in kg, mg, and even µg.</p><p>In order to calculate percentages, we need to go through this data and have a consistent unit that we can work with. To that end, I wrote a function to parse this JSON and that convert all values to grams — Which is a unit that would keep smaller units from being calculated inaccurately by the javascript Number class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bo_3nXc2u24B_C-kme4gpQ.png" /></figure><p>The heavy lifter, so to speak, in this code is the switch statement in the for loop. We use regex to pull that particular value from the key and compare its value in order to figure out what factor to change the value by. The default case, of course, is grams and we merely convert the number string into a float value in the match.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WZJizQltUbiB6k1GylPFKw.png" /></figure><p>Finally, we need to express that weight (Yes, yes… technically this should be mass, but I’m a programmer not a chemist) in grams as a percentage of a 70kg person’s weight. The formula is a consequence having to convert 70kg to grams (70000) and dividing that by 100 since we’re going to multiply by 100 anyway to get the actual percentages (and so 70000/100 = 700). Converting to percentages is not expressly necessary with data visualization libraries we are using, but it was a fun and easy touch to implement.</p><h4>Off the Chart(.j)s!</h4><p>Speaking of data visualization, what we’re going to use to show the proportions of these elements is a handy little library called Chart.js (<a href="http://www.chartjs.org/">http://www.chartjs.org/</a>). This basically is an entry level visualization library that displays and creates very good looking charts with a minimal amount of work from the developer. It uses the canvas element from HTML5 for your slick data viz needs. <br>Combined with that, we’re also going to use a wrapper called react-chartjs-2 that lets us create these various charts as react components. This makes the library even easier to use in conjuction with React.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*-j0pAbk61ax-RROOIIy0TQ.png" /><figcaption>Mmm… doughnuts….</figcaption></figure><p>Here is the component. I’m using the doughnut chart because doughnuts are delicious (but then again, so is Pie) and shows the different proportions relatively well. We’re passing in two props here, the data itself and an options — with both props being passed in as objects (we’ll take a look at the data object in a moment. There are also a good number of options and other props available to you, so I suggest taking a gander at the available ones here:<br><a href="https://github.com/jerairrest/react-chartjs-2">https://github.com/jerairrest/react-chartjs-2</a></p><p>The data object itself is pretty straight forward, with one possible gotcha. The colors for the chart itself as passed down in the data object. This makes sense to me, but I can see where that can be confusing for some.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FHsvHUaCKWVu043kxsnK0A.png" /></figure><p>Another thing to keep in mind is that the data and the labels have to be stored as arrays (in this case, I’m pulling the keys and the values from a percentages object created in a previous function). The onus is on you to keep the indexes lined up. Another thing to keep in mind is that the background color is also an array of hex color values stored as strings.<br>And I didn’t want to hand type 60 different hex color codes for the background color, so I literally googled “random hex code code” and that led me to this handy snippet pictured below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pdet-HpH6cAsDZHR-zAokQ.png" /><figcaption>Original answer here: <a href="https://stackoverflow.com/questions/5092808/how-do-i-randomly-generate-html-hex-color-codes-using-javascript">https://stackoverflow.com/questions/5092808/how-do-i-randomly-generate-html-hex-color-codes-using-javascript</a></figcaption></figure><p>And so here’s the finished result! A neat thing is that as you can see below, Oxygen and Carbon really make up the bulk of our bodies. You can exclude particular elements by clicking their labels (while I wish I could take credit for that, it’s actually a neat feature of Chart.js). Though, a little bit of fun I can take credit for — every time you refresh the page, you‘ll generate a different color set. That’s all I have for you today, see you next time, anonymous internet friends!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7SNn4f_OnCwAMEh3L5GA0A.png" /><figcaption><a href="http://humanelements.surge.sh/">http://humanelements.surge.sh/</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13ae86f0d4cb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Save the Environment (Variables)]]></title>
            <link>https://medium.com/@justinmanalad/save-the-environment-variables-ed8ecf45f775?source=rss-9febf9fe9b07------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed8ecf45f775</guid>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Little Bits of 1’s and 0's]]></dc:creator>
            <pubDate>Fri, 10 Nov 2017 14:23:22 GMT</pubDate>
            <atom:updated>2017-11-13T14:40:08.098Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GNUXvhMdw33CBi08TSeH_A.jpeg" /></figure><p>They say that children are a product of their environment. In programming, this is even more true. Basically, all processes are descendants of the core process of an operating system, the Kernel (still awaiting his promotion to General after all these years…) And on startup, the Kernel creates values and passes down these values to its children (though, in reality, the Kernel generally keeps itself segregated and protects most of its data from its descendant processes… what a deadbeat). Many of these are known as environment variables.</p><p>Simply, Environment variables that a process has access to from its runtime environment. I know that’s not terribly helpful or descriptive, but you can also think of them as variables that child processes inherit from their parents. In many cases, these environment variables determine particular behaviors in the process (for example, setting the environment variable HTTPS=true before starting a react dev server will force the server to load onto HTTPS) or as a way to determine which behaviors are desired for particular stages of building a software application (development, testing, or production). Most operating systems have a set of environment variables that are set on startup. For example, Windows OS’s stores its startup variables in the registry or in the AUTOEXEC.BAT file. They are also useful for keeping keys and certain secrets away from the user.</p><p>While these do tend to be critical to the function and behavior of programs, the way that these are set an accessed are subject to slight syntactical variation.</p><p>For example, setting an environment variable in a Bash shell is slightly different in setting in Windows Command Line:</p><pre>//Bash<br>export VARIABLE_NAME= variable_value</pre><pre>//CMD.EXE<br>set VARIABLE_NAME= variable_value</pre><p>And can be accessed by that process and all children of that particular process. Again, in bash:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*jN65VLYazz39E38BN4X62Q.png" /></figure><p>You will note that to access environment variables directly in my terminal, we the dollar sign to tell the system I want to access an environment variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*Kur6yCNAwdSu5i7-_dZLHA.png" /></figure><p>There are also other environment variables depending on the particular shell we’re working in. Common ones include HOME, PWD, and OLDPWD which are the home directory, present working directory, and old present working directory (Computers are confusing…) respectively.</p><p>But, wait… THERE’S MORE! Like we said before, child processes also have access to the environment variables and we can see this demonstrated when we go into irb or enter a node console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*zvvzXv4a7Gzq2xXNw0qFqA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*6pux4GyNoJoj5dHUlxWlLQ.png" /></figure><p>We don’t always have to export the environment variable to the parent process, and there are many times we don’t want or need all the child processes to know of a particular value. In most shells and runtime environments, we have the ability to set the environment variable only for that particular child.</p><p>Consider the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*uhn0WW_JQGEGGL3Y1yg6Ow.png" /></figure><p>And then the following output when we run it in the Bash shell:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9NvLu_C9J0UHiq5hNKihnw.png" /></figure><p>In the first line, we tell node to run example.js with the environment variable NEW_VAR set to “Howdy Blue Dot” and we have access to that environment variable in the process that runs. But, without defining that variable in that second command — it is, obviously, undefined.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uuOnAB_GBhnFVZiprt0FNw.png" /></figure><p>Not only can we access these values, child processes can manipulate these values just like regular variables (and just like anything in programming, whether or not this is a good idea is dependent on context and use case).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yj8Qg1JvaicHHr3TZT-Zhw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*I6Khu9kJquFISl1xkHUzMg.png" /></figure><p>And as you can see (at least in the Bash Shell) it doesn’t matter whether or not the variable is declared at the start of the child process or set in the parent.</p><p>Speaking of use cases, though, what are the use cases for these environment variables? Well, my anonymous internet friend, they are myriad and legion and too many to list here. However — I can share a few cases that I have used them.</p><p>First, is to set behavior for certain programs. For example, there may be instances where you want your create-react-app development server to load up on HTTPS rather than HTTP.</p><p>This can simply be done by writing HTTPS=true right before “npm start” or “yarn start”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/644/1*dOFLHbYSkkWBji0nnd9sxg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SoKbGwpoi_eQob0_BgSZKQ.png" /></figure><p>Second, is credentialing for external APIs. To make the best use of some google cloud platform technologies, you need to present credentials to the API that you are using. And since you’re either paying for the service or have a limited credit that you are using for that particular service, you don’t necessarily want to share those credentials to the world (like an API key embedded in an endpoint url). And so, we can use these environment variables to hide sensitive data from the user but still be able to pass it along to the proper end points.</p><pre>GOOGLE_APPLICATION_CREDENTIALS = &#39;./yourcredentials.json&#39;</pre><p>There are many more applications for environment variables — and also many critics (but we’re programmers, we will always have something to criticize).</p><p>‘til next time anonymous internet friends!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed8ecf45f775" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[BlockHeaded HardDrives]]></title>
            <link>https://medium.com/@justinmanalad/blockheaded-harddrives-c0a4df679ff8?source=rss-9febf9fe9b07------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0a4df679ff8</guid>
            <category><![CDATA[computers]]></category>
            <category><![CDATA[hardware]]></category>
            <dc:creator><![CDATA[Little Bits of 1’s and 0's]]></dc:creator>
            <pubDate>Thu, 19 Oct 2017 12:24:20 GMT</pubDate>
            <atom:updated>2017-10-19T13:56:00.171Z</atom:updated>
            <content:encoded><![CDATA[<p>Here’s a fun bit of trivia — What takes up more space? 1MB of data on a modern harddrive or 1MB of data on a Floppy?</p><p>Is this a trick question? Is this like the difference between a pound of lead and a pound of feathers? If you think so — You may be surprised that in the majority of cases, an equivalent amount of data on a Floppy will take up more space on a modern HD (and for that matter, an SSD). If that feels weird to you, and you’re asking why that’s the case — that’s natural, and we’ll answer that in the coming paragraphs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*s1yMenQ29noL9Ko4EEM6IQ.png" /><figcaption>A. Track B. Geometric Sector C. Track Sector D. Block(or Cluster, depending on context)</figcaption></figure><p>While we may have a conception of computer data as an enormous undifferentiated field of 1’s and 0’s, this isn’t the case. Our machines still need to be able to find the data that it stores to disk, so there are several layers of organization, abstract and physical, that are used to organize and address this data. To address data on platter, your machine’s operating system (specifically the part of the OS called the filesystem) organizes the drive into units called Blocks. It also takes care of organizing the data it stores into blocks (called, unimaginatively enough, <em>Blocking</em>) during the write cycle and assembling the data from the blocks during the read cycle (called, even more unimaginatively…<em>Deblocking</em>). The number of blocks an OS can address limits the the size of the disk. In the case of early versions of MS DOS, this was roughly 32 MB. With Windows 10, this is something close to 9.3 Zettabytes (9.3 x 10²¹ bytes)</p><p>Physically, hard disk platters are organized into tracks(concentric rings of data — very much like the tracks on an old LP) these tracks further are separated during manufacture into sectors by writing headers that separate each track into smaller pieces(a single track on an HD can have thousands of sectors). In SSDs, the constituent data recording units, NAND cells — are grouped into sectors). This goes for many other storage media as well, CD, floppies, and laserdiscs. Formerly, the standard was 512 bytes. Currently, the standard has been increased to 4096 byte sectors. The sector is the <em>smallest unit of storage</em> on a particular disk. ANY piece of data, even a single byte, will take up the entire sector. Which is why data stored on the floppy will almost always use less space than a modern Hard Disk or SSD. But…. this seems wasteful, so why do it?</p><p>A few reasons — first is that sector headers contain vital for telling the actual machine (specifically the read/write head) what part of the disk it’s looking at. Next, the sector also contains Error Correcting Codes and Alternate Addressing in case the data is corrupted, the sector is physically damaged, or if the contents are otherwise unreadable. Secondly, it improves read/write efficiency. In almost any computer, reading and writing to disk is the bottleneck that determines overall system performance (though, this is also due to the limitations of bandwidth)and a little wasted space is a reasonable trade off to improve the speed of the overall process. Think of it like a dresser drawer — sometimes you may not care about unfilled space to make sure it’s organized well enough so you can get find your clothes in a timely manner.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*1ed5MH8d4aQyO2XNDvK5XQ.jpeg" /><figcaption>Yeah, try finding your stuff in that mess….</figcaption></figure><p>Filesystems generally attempt to keep data from a single file stored in adjacent sectors and avoid mixing the contents of different files in the same sector. Furthermore, relational databases will try to keep records from a single item stored in the same sector so that the database can grab a single item in one sweep and not have to try to find the other pieces of that record across far flung regions of the disk. For Big Data applications, columnar databases will store entire datasets across blocks to be able to query the entire dataset without having to read from the disk multiple times.</p><p>For a database engineer, this read/write speed is a fact of life they deal with everyday. For the normal consumer, this wasted space is not terribly noticeable. Unless you’re storing millions of small files, files systems are generally intelligent enough to optimize space. And for people designing applications that interact with these systems, it is always important to keep these things in mind… Especially if performance is a priority.</p><p>References and Further Reading:</p><ul><li><a href="https://en.wikipedia.org/wiki/File_system"></a></li><li><a href="http://www.alphaurax-computer.com/computer-tips/hard-drive-knowledge-blocks-vs-sectors">Hard Drive Knowledge: Blocks vs. Sectors</a></li><li><a href="http://idema.org/?page_id=2369">The Advent of Advanced Format</a></li><li><a href="http://www.ntfs.com/hard-disk-basics.htm">NTFS information: Hard Disk Drive Basics</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0a4df679ff8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Big O Notation]]></title>
            <link>https://medium.com/@justinmanalad/big-o-notation-901bd0c92c07?source=rss-9febf9fe9b07------2</link>
            <guid isPermaLink="false">https://medium.com/p/901bd0c92c07</guid>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Little Bits of 1’s and 0's]]></dc:creator>
            <pubDate>Thu, 28 Sep 2017 02:15:13 GMT</pubDate>
            <atom:updated>2017-09-28T15:49:57.204Z</atom:updated>
            <content:encoded><![CDATA[<p>On October 13th, 1999 — an event that would have a profound impact on the world occurred. An event that united peoples and countries from all around the world. And that was the Japanese TV premiere of “The Big O”, a giant-robot anime directed by Miyazaki protégé Kazuyoshi Katayama.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EX98dmO6VDzU5BaaSRmqzw.jpeg" /><figcaption>Image copyright Sunrise Bandai — please don’t sue me.</figcaption></figure><p>While this was a great work and a wonderful deconstruction of the seinen mecha genre, more importantly, it shares its name with an important concept in computer science — Big O notation. What the “O” stands for is actually a matter of (half-hearted) debate. The most likely explanation is that it was coined by 19th century German Mathematician Paul Bachman as “Ordnung von” — German for “Order of”.</p><p>Computer Science was not really extant field in the 19th century, but early computer science liberally borrows (or outright steals) terms from late 19th century and early 20th century mathematics. Broadly speaking, Big O notation is a way to denote the resource use of a particular algorithm, in terms of speed or space used and typically in the worst case scenario.</p><h3><strong>Big O Classes</strong></h3><p>Algorithms can scale in a variety of ways depending on the size of the input (Or in some cases, not at all) and so, algorithms can fall into one of a few categories (Note: the following is not an exhaustive list, but a list of the most common classes that can be encountered).</p><p><strong>O(1): Constant</strong></p><p>This means that the number of steps that occurs stays the same regardless of the size. For example, accessing the first element of an array will be the same regardless of whether or not the array is 10 elements or 10 million elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*WJMHzNJFV5vDYkDVu7rP-w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h_b_q9swMURQmcqgfVQJZQ.png" /></figure><p><strong>O(n): linear</strong></p><p>For a function with O(n) complexity, the algorithm’s complexity will in as much as the input increases. This can be seen in a loop that iterates through an array one element at a time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*r35y5824Kl2JseDhoI-7UQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*vJxORQ2blJowgW6QUzX9Qw.png" /></figure><p><strong>O(n²): quadratic &amp; O(n^c): Polynomial</strong></p><p>A nested loops fall into this category, especially if the array is traversed entirely at each index. Complexity becomes an even greater issue for more deeply nested loops, the function increases polynomially — with the curve of the plot growing more steeply as the number of the exponent rises.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*wzJL2YdmbXwx-qimbKOyZA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*kJawJ6XZjNL4YkbbNAO4Ng.png" /></figure><p><strong>O(log(n)): logarithmic</strong></p><p>This is one of the more interesting classes. There’s an increase in complexity with increasing n, but it plateaus after a certain point. This class includes binary search, merge sort and other algorithms that are optimized for larger datasets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*tCwNx5IJ6KxPa5s0rZPV1g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ayfQEazF4NQT7vPIw8ppFA.png" /><figcaption>Code snippet from: <a href="http://www.anujgakhar.com/2014/03/01/binary-search-in-javascript/">http://www.anujgakhar.com/2014/03/01/binary-search-in-javascript/</a></figcaption></figure><p><strong>Important considerations</strong></p><p>Now, when talking about Big O notation, it’s helpful to think of them as classifications rather than a exact descriptor of the number of steps an algorithm has. So, when classifying an algorithm, any term that becomes insignificant as n gets arbitrary large is discarded.</p><p>For example: If in the worst case for an algorithm takes n²+5n+2 steps to complete for input n, we would classify that as an O(n²) since 5n and 2 will eventually add very little to the complexity of the algorithm when n is large enough.</p><p>More importantly, though, it’s good to remember that Big O notation is not meant to be exact— the exact numbers and exact shape of the growth curve don’t matter.</p><p><strong>Resources</strong></p><p><a href="https://en.wikipedia.org/wiki/Big_O_notation"></a></p><p><a href="http://bigocheatsheet.com/">http://bigocheatsheet.com/</a></p><p><a href="http://web.mit.edu/16.070/www/lecture/big_o.pdf">http://web.mit.edu/16.070/www/lecture/big_o.pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=901bd0c92c07" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Little Packets that Could — A TCP/IP Story]]></title>
            <link>https://medium.com/@justinmanalad/the-little-packets-that-could-a-tcp-ip-story-66b555b74359?source=rss-9febf9fe9b07------2</link>
            <guid isPermaLink="false">https://medium.com/p/66b555b74359</guid>
            <category><![CDATA[internet]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Little Bits of 1’s and 0's]]></dc:creator>
            <pubDate>Wed, 06 Sep 2017 23:49:14 GMT</pubDate>
            <atom:updated>2017-09-06T23:56:11.783Z</atom:updated>
            <content:encoded><![CDATA[<p>This is a tale, a magical tale of transmission protocols, data packets, and acronyms…. many, many acronyms. A tale as old as time itself (especially if you count January 1st, 1983 as the beginning of time). It is also a tale that spans the entire globe and is part of the magical fabric that makes modern communication possible. It is also a complicated story that spans decades and involves a myriad of public and private entities and a vast interconnected system. So, we’re going to focus our story on two of tiniest of heroes: the UDP and TCP packet.</p><p>Almost all the traffic on the internet is packaged into discreet packets. It enables large volumes of data to be transferred reliably even when network conditions are far less than optimal. Two of the most common communication protocols on the internet are <strong>UDP</strong> (User Datagram Protocol) and <strong>TCP</strong> (Transmission Control Protocol). They operate on what’s known as the transport layer of the Internet Protocol because they are basically responsible for transporting data from a source to its destination. While they both contain data and are transmitted over the internet, they are structured VERY differently and are generally used for different things.</p><p>In short, UDP is a connectionless protocol with a minimum data overhead.</p><p>So… What does that mean?</p><p>Basically, there is no accounting for what order data is sent in, if packets are missing, or even if the destination actually received the transmission. In IP version 4, the checksum portion is optional — in IP version 6, the Source Port portion is non-mandatory. It relies on higher-level protocols and the actual network infrastructure for its reliability. This means that it is transmitted towards its destination without having the destination expecting that it is coming. You can see this “minimum overhead” when you examine the UDP header.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/1*rFoHKQ3uqv8jV4-w6PwkIA.png" /></figure><p>UDP is basically the the unwary traveler who ventures out unprepared to a neighboring village. Basically, all he knows is where he comes from and where he’s going (AND actually in IPv6, he doesn’t even know where he’s coming from). He doesn’t even bother to tell the people he’s visiting that he’s coming. UDP basically makes the assumption that the roads are in fine working order and there will be absolutely no pitfalls on the way there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*21dCoU70IMvcLmxElVZxvA.jpeg" /><figcaption>Nope… nothing absolutely going wrong here.</figcaption></figure><p>If UDP is so unreliable, why do we even use it?</p><p>Because we can use it to transmit data very quickly with very little overhead. This is actually perfect for a few applications where the the anticipated packet loss is very close to 0%, data transmitted is relatively small, when a constant stream of data is preferred over the integrity of that data. This finds a home in a few applications: network reliability testing, live video and audio, and perhaps most critically — Domain Name queries. Machines in the DNS are inundated with a countless number of requests per second and have to respond quickly and UDP provides them. But what if reliability and data integrity are paramount? Well, we have the workhorse of the internet to the rescue, TCP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CrbyyCYdQNcKJj9Hz-V-iQ.png" /><figcaption>Period photograph of a 19th century TCP data packet — State Archives of North Carolina</figcaption></figure><p>Most internet applications communicate using TCP and it has grown to be one of the cornerstones of the internet and communication technology. In fact, the main protocol suite of the internet is named for this humble little package of data. It provides error-free data transmission and many other functionalities such as flow-control (managing the rate of data transfer to keep the receiving end from being overwhelmed) and the retransmission of lost packets. All that and it makes julienne fries.</p><p>But what makes it so reliable and versatile?</p><p>Through the magical power of connections, headers, three-way handshakes. You can see this capability when you examine the header of TCP packet:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0I1a3OK5IiZAW6N8Rsxrzw.png" /><figcaption>from <a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol">https://en.wikipedia.org/wiki/Transmission_Control_Protocol</a></figcaption></figure><p>As you can see — a TCP header is far more complex than UDP and can account for more situations and events, such as retransmission in the case of a lost packet, or preventing packets from getting lost in the first place. We have to make sure that when we are transmitting, that our destination is not busy/able to take our messages. Unlike UDP, TCP is a “connectionful” protocol that requires that a connection is made before data is exchanged.</p><p>This is accomplished by means of what’s known as the “Three Way Handshake.” Which is probably as awkward as it sounds:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*86t8wwPOX7WoVfT8R5zEuQ.png" /><figcaption>Kevin Lamarque/Reuters</figcaption></figure><p>In a simplified version — the source machine sends a data packet (known as SYN for synchronize) to the intended destination. If there are available ports open, the destination machine sends back a data packet (known as an SYN-ACK packet for synchronize-acknowledgement). When the source receives this SYN-ACK packet it sends a final ACK packet to establish the connection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*XZK9fmkLjEM-9BrnfXcMGQ.png" /><figcaption>By Scil100, CC BY-SA 3.0, <a href="https://commons.wikimedia.org/w/index.php?curid=30810617">https://commons.wikimedia.org/w/index.php?curid=30810617</a></figcaption></figure><p>The header also contains other vital pieces of information such as the order and number of data packets that are going to be sent. So, in the case of incomplete data transmission or missing packets, the source can request those packets again and ensure that all data is accounted for. This does come at a bit of a cost. A TCP header can be anywhere from a minimum of 20 bytes to a maximum of of 60 bytes. Furthermore, it takes more machine steps to assemble a TCP packet rather than a UDP one due to the more sophisticated and intricate nature of the header. Of course, machines can do these tasks blisteringly fast and in fractions of a section. However, this process takes time and it adds up as the volume of data packets increase. So, in cases where you prioritize speed over reliability, UDP is your guy.</p><p>This has been very high level overview of UDP and TCP — there is still ALOT that we haven’t covered and it is an incredibly complicated topic. However, if you’re interested in learning more (and I know you are), I’ve included some further reading below. ’Til next time, happy coding and may your connections be always error-free.</p><p>Further Reading:</p><p><a href="http://www.freeccnastudyguide.com/study-guides/ccna/ch1/1-4-tcpip-model/">1-4 TCP/IP Model</a></p><p><a href="http://www.cs.rpi.edu/~kotfid/ne1/CCNA_chapter2.pdf">http://www.cs.rpi.edu/~kotfid/ne1/CCNA_chapter2.pdf</a></p><p><a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol">https://en.wikipedia.org/wiki/Transmission_Control_Protocol</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66b555b74359" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[This blog post writes itself]]></title>
            <link>https://medium.com/@justinmanalad/this-blog-post-writes-itself-e3af154f28a5?source=rss-9febf9fe9b07------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3af154f28a5</guid>
            <dc:creator><![CDATA[Little Bits of 1’s and 0's]]></dc:creator>
            <pubDate>Sat, 19 Aug 2017 20:33:13 GMT</pubDate>
            <atom:updated>2017-08-24T12:44:28.559Z</atom:updated>
            <content:encoded><![CDATA[<p>…kind of.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*cCKAOFSh1LOiB_vG335icw.png" /></figure><h4>Pre(r)amble.</h4><p>As tech enthusiasts and programmers we know the value of automating our work. Writing doesn’t come terribly easy to me, so I decided to try and build something that writes a blog post for me. However, as I started actually working on it, I realized two things. Putting together a sentence is HARD and, more importantly there’s more to programming than just writing code. Good engineering relies on understanding the interactions of the various systems that you are working with — and that’s something your brain does without you even thinking about it.</p><p>There’s a lot of contextual processing that goes on in our meat computer that enables us to string together words in a meaningful way. Parts of speech, sentence structure, rhetorical devices and other considerations interact to help us create ways to relay information from one brain to another. So, how do I model this behavior in a computer program, especially as a novice web development student with no resources to speak of?</p><p>I’m not — not even close.</p><h4>Methods Operandi</h4><p>Basically, I’m going to do is grab a bunch of words out the dictionary and throw them together into sentences (which is basically how I operate, anyway). The program will then generate a bunch of sentences and I will keep the ones that are most amusing and assemble them into a paragraph, eventually accomplishing that through a command-line-interface.</p><p>Initially, my plan was to load a wordlist file as an array and grab and use #sample method to grab random elements within the array. Luckily the word list was formatted with each entry occupying one line, which allows me to use the File.readlines method that separates each line as an element within an array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*q0p2F3mjlr27bC9YaCF2_g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*482SLtBBNAkRzoVkzWQruQ.png" /></figure><p>Great! But there was a hitch in this plan — with the first implementation of the sentence constructor this method was called for every single word I wanted to create. So for every single sentence it was constructing, it had to read and iterate through all 300,000 records several times. This was done on a fairly new machine and it was a fairly small dataset, however it did seem like it was taking just a tad longer than it should have.</p><p>While this may seem like a small error in implementation, it’s been quite eye opening as a burgeoning developer on many levels. First, is the realization that we are manipulating fundamental forces of the universe to build things (even something as silly as this sentence constructor). Next, is that being a good programmer isn’t just about writing code, it’s about understanding how various systems interact. While, we don’t have to be able to map the entirety of every integrated circuit in our machines, we do need to know how our instructions will cause the machine to behave.</p><p>In this case, the way I was calling this method was causing a significant bottleneck in performance. Luckily, I hadn’t done that much work on this project and I was given a new tool to work with: ActiveRecord.</p><h4>All Your Database Are Belong to Us</h4><p>Using ActiveRecord allows me to use SQLite to store a sizable number or records and query the database to grab the quickly grab the words I need. The current structure allows for greater flexibility in scaling the functionalities later on. Basically, I have three models mapped to different tables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5GOnn2ZLizeOzuQRX6mBiA.png" /></figure><p>We have a Paragraph class which will be using Sentence objects to constructor blocks of text and the Sentence class uses the Word class to well… get words. Specifically it does this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5KPHVHS_o6YGBvqucpsRCw.png" /></figure><p>That first line writes a query to reorder the database and return a random number rows from the database (up to 10) as an array. The next block iterates through that array and returns an array of just the words (stored as an “entry” column in the table. The result is that it reads the disk just once and performance is increased!</p><h4>The Mark of the Benches</h4><p>So, you may be asking, <em>“How do I know this isn’t just some placebo effect and you’re just THINKING it’s a performance boost?”</em></p><p>That’s a good question and aren’t you impressed that I read your mind?</p><p>…No?</p><p>Well… in any event we can figure this out using the incredibly handy ‘Benchmark’ module from the Ruby Standard Library. This enables us to assess the performance of a block of code through many different methods. The Benchmark class allows us to evaluate how long it will take to go through a given block of code and the .realtime class method allows us to output that as the time elapsed in seconds.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*brlZdlsw_A9QLWCKSHhDKw.png" /></figure><p>By testing out both methods we see that there is about a 400% performance increase by using ActiveRecord and SQL to pull random records from the database. While this is still surprisingly slow (could be other issues involved), it is still a remarkable increase in performance.</p><h4>Wrapping Things Up</h4><p>So, basically I was able to increase the efficiency of this particular method 4-fold just by changing a few lines of code(from my perspective, at least). More importantly, I realized that programming and becoming a better programmer relies not just on memorizing code, but in understanding (even on a surface level) on how these systems interact.</p><p>I also learned that it’s hard to teach a computer to speak english. So, what do you think about that, random sentence generator?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*qWjhwSzs1hLKLkmPd-mAiQ.png" /></figure><p>Indeed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3af154f28a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>