<?xml version="1.0" encoding="UTF-8"?>
<rss version='2.0' 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">
  <channel>
    <title>Jessica O&#39;Brien</title>
    <description></description>
    <link>https://jessicaobrien.silvrback.com/feed</link>
    <atom:link href="https://jessicaobrien.silvrback.com/feed" rel="self" type="application/rss+xml"/>
    <category domain="jessicaobrien.silvrback.com">Content Management/Blog</category>
    <language>en-us</language>
      <pubDate>Sun, 01 Nov 2015 19:31:50 +0100</pubDate>
    <managingEditor>j.e.obrien28@gmail.com (Jessica O&#39;Brien)</managingEditor>
      <item>
        <guid>http://jessicaobrien.net/node#19213</guid>
          <pubDate>Sun, 01 Nov 2015 19:31:50 +0100</pubDate>
        <link>http://jessicaobrien.net/node</link>
        <title>The JS Event Loop</title>
        <description>Asynchronous events and non-blocking I/O</description>
        <content:encoded><![CDATA[<p>Node is a JavaScript interpreter for running apps outside the browser (like on a server, for example). It uses non-blocking I/O and asynchronous events, and each module is created within its own filed with an isolated scope. These are fundamental concepts to understand before being able to make Node interpretation work in the intended way, so in this post I&#39;ll go through each of these statements and walk through a few examples to illustrate.</p>

<h1 id="non-blocking-i-o">Non-blocking I/O</h1>

<p>From operations on the database, HTTP requests, and reading/writing to disc, pretty much all input and output in Javascript is non-blocking. By that, I mean that there is a <em>single</em> stream of operations for the interpreter to execute. I imagine them to be like a queue of people waiting in line, each waiting for the interpreter to perform their operation, and chained together through callback functions. After each operation completes, a message is enqueued along with the provided callback function. Later down the track the message is dequeued and the callback fired.</p>

<p>For example:</p>
<div class="highlight"><pre><span></span><span class="nx">request</span><span class="p">(</span><span class="s1">&#39;http://www.youtube.com&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">error</span><span class="p">,</span> <span class="nx">response</span><span class="p">,</span> <span class="nx">body</span><span class="p">)</span> <span class="p">{</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">body</span><span class="p">);</span>
<span class="p">});</span>

<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Done!&#39;</span><span class="p">);</span>
</pre></div>
<p>Here, you might expect that the request function to google.com is executed, outputting its body to the console, and <em>then</em> &#39;Done!&#39; is output to the console. Instead what happens is that while the request function is executed first, it passes an anonymous function as a callback to execute <em>when a response is available</em>. “Done!” is immediately output to the console, and then sometime in the future, the response comes back and the callback is executed, outputting its body to the console.</p>

<h1 id="the-event-loop">The Event Loop</h1>

<p>It&#39;s important to understand how the runtime stores these callbacks and in what order they are executed. Conceptually, JavaScript runtime environments use a queue and a loop. The queue stores the list of messages to be processed along with their provided callback functions. The messages are added to the queue in response to certain external events like clicks / HTTP requests.For example, let&#39;s say that an event listener is set up to change the font colour of a page when a certain button is clicked. When the button is clicked, the message is enqueued, and enters into the event loop, which follows the pattern:</p>

<p>while there are messages in the queue do<br>
        dequeue the first message<br>
        execute its callback // the font colour changes</p>

<p>Another interesting example is using setTimeout, which by nature is asynchronous, so the provided callback will be executed as part of a different queued message, on a future run of the event loop. </p>
<div class="highlight"><pre><span></span><span class="kd">function</span> <span class="nx">apple</span><span class="p">()</span> <span class="p">{</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;an apple a day&quot;</span><span class="p">);</span>    <span class="c1">// 1ST</span>
  <span class="nx">setTimeout</span><span class="p">(</span><span class="nx">grape</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;keeps the doctor away&quot;</span><span class="p">);</span> <span class="c1">// 2ND</span>
  <span class="nx">banana</span><span class="p">();</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">grape</span><span class="p">()</span> <span class="p">{</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;a bunch of grapes&quot;</span><span class="p">);</span> <span class="c1">//4TH</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">banana</span><span class="p">()</span> <span class="p">{</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;banana split&quot;</span><span class="p">);</span>  <span class="c1">//3RD   </span>
<span class="p">}</span>

<span class="nx">apple</span><span class="p">();</span>
</pre></div>
<p>In this example, setTimeout is being invoked, passing the callback grape with a timeout of 0 milliseconds. You might be tempted to see the 0 and think that grape will be fired immediately. In reality, because setTimeout is non-blocking, regardless of the specified time  (in this case, almost* instantly) a separate message is enqueued containing grape as its callback. &#39;a bunch of grapes&#39; is logged last, as it has to wait until the next run of the event loop for the callback to the fired.</p>

<p>*the way that setTimeout is usually implemented, it is just meant to execute after a given delay, and once the browser&#39;s thread is free to execute it, so even if you say 0 it might not be exactly 0 ms. Unless there&#39;s some serious backlog of activity though, a human still won&#39;t be able to notice any difference !</p>

<p>One last comment: if in the same call frame two calls are made to setTimeout – passing the same value for a second argument – their callbacks will be queued in the order of invocation.</p>

<h1 id="summary">Summary</h1>

<p>With a simple queue and event loop, JavaScript allows us to build systems around a collection of asynchronously-fired callbacks, freeing the runtime to handle concurrent operations while waiting on external events to happen. In the next post I&#39;ll  walk through the setup of a basic server using node and express.</p>

<p>Checkout the mdn documentation here: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop">https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop</a></p>
]]></content:encoded>
      </item>
      <item>
        <guid>http://jessicaobrien.net/recursive-backtracking#19029</guid>
          <pubDate>Mon, 26 Oct 2015 06:27:06 +0100</pubDate>
        <link>http://jessicaobrien.net/recursive-backtracking</link>
        <title>Recursive Backtracking</title>
        <description></description>
        <content:encoded><![CDATA[<p>To put it simply, the idea behind backtracking comes down to trial and error. If you can&#39;t think of a way to jump to finding the solution to a problem, there is always the strategy of just trying different things until one of them works. When you search for a solution, try a possibility; if you find yourself stuck, back up and try the next one. Repeat until you find a viable solution.</p>

<p>In a general algorithm this looks something like:<br>
1. Have I arrived at a viable solution?<br>
  1a) Yes: return true! (or whatever makes sense for a successful outcome)<br>
  1b) No: So where can I go from here? Choose an alternative and go there. Is this new path a solution? This is where the recursive call comes into play - restart the algorithm from the new location.<br>
2. Are there any remaining alternative routes? If there are, choose one and start over.<br>
3. Finally, when I am out of places to go, return false (or whatever makes sense for a termination outcome)</p>

<p>I&#39;ll apply this logic to two examples:</p>

<h1 id="rock-paper-scissors">Rock, Paper, Scissors</h1>

<p>This problem asks you to write a function that generates every sequence of throws a single player could throw over a three-round game of rock-paper-scissors. The output should look something like:<br>
[[&quot;rock&quot;, &quot;rock&quot;, &quot;rock&quot;],<br>
[&quot;rock&quot;, &quot;rock&quot;, &quot;paper&quot;],<br>
[&quot;rock&quot;, &quot;rock&quot;, &quot;scissors&quot;],<br>
[&quot;rock&quot;, &quot;paper&quot;, &quot;rock&quot;],<br>
             ...etc...<br>
At first glance, this could easily be solved by nesting 3 for-loops together, one for each round: </p>
<div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">rockPaperScissors</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">options</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;rock&quot;</span><span class="p">,</span> <span class="s2">&quot;paper&quot;</span><span class="p">,</span> <span class="s2">&quot;scissors&quot;</span><span class="p">];</span>
  <span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="p">[];</span>
  <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// for each option</span>
    <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">j</span> <span class="o">&lt;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// loop through the other options</span>
      <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">k</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">k</span> <span class="o">&lt;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">k</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="kd">var</span> <span class="nx">combination</span> <span class="o">=</span> <span class="p">[</span><span class="nx">options</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span> <span class="nx">options</span><span class="p">[</span><span class="nx">k</span><span class="p">],</span> <span class="nx">options</span><span class="p">[</span><span class="nx">j</span><span class="p">]];</span>
        <span class="nx">result</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">combination</span><span class="p">);</span>
      <span class="p">}</span>
    <span class="p">}</span>
  <span class="p">}</span>
  <span class="k">return</span> <span class="nx">result</span><span class="p">;</span>  
<span class="p">};</span>
</pre></div>
<p>But what if we don&#39;t know how many rounds there will be? This can be solved through recursive backtracking by starting off with an empty array, and each time checking if the length of that array has built up to the number of rounds specified in the arguments to the function. Until then, an inner function looks through all of the options and calls itself, passing the growing results array.</p>
<div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">rockPaperScissors</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">rounds</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">options</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;rock&quot;</span><span class="p">,</span> <span class="s2">&quot;paper&quot;</span><span class="p">,</span> <span class="s2">&quot;scissors&quot;</span><span class="p">];</span>
  <span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="p">[];</span>
  <span class="nx">rounds</span> <span class="o">=</span> <span class="nx">rounds</span> <span class="o">||</span> <span class="mi">3</span><span class="p">;</span>

  <span class="kd">var</span> <span class="nx">findOutcome</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">playedSoFar</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// recursive to find all possible combinations</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">playedSoFar</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="nx">rounds</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">return</span> <span class="nx">result</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">playedSoFar</span><span class="p">);</span> <span class="c1">// .push() returns length of resulting array</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
      <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">findOutcome</span><span class="p">(</span><span class="nx">playedSoFar</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">options</span><span class="p">[</span><span class="nx">i</span><span class="p">]));</span> <span class="c1">// concat adds a val or an array to an array</span>
      <span class="p">}</span>
    <span class="p">}</span>
  <span class="p">};</span>

  <span class="nx">findOutcome</span><span class="p">([]);</span>
  <span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
<h1 id="n-queens">N-Queens</h1>

<p>This algorithm returns the number of nxn chessboards that exist, with n queens placed such that none of them can attack each other. In the below implementation, there are a couple of helper functions I refer to. Firstly, the board passed in is just a 2D array representing an n by n matrix filled with 0&#39;s. togglePiece() simply changes the cell at the given column and row to 1 or 0, depending on what it was before. hasAnyQueensConflicts() is the culmination of a number of checks up and down the columns, and across the board diagonals, checking for any other queens already placed on the board and returning true if any conflicts exist. </p>

<p>As you can see, it actually follows quite a simple pattern. True to the recursive backtracking general pattern, we start by checking for a successful case - if we have reached the last column, it must mean that we have created a board without collisions, and should add to the count of solutions. </p>

<p>The problem is recursively being divided into the sub problem of placing single queen on the chess board. Then, to identify if the toggled queen resulted in a solution, we can check whether any of the already placed queens attack this position. If yes, then it&#39;s safe to move to the next column. If no conflict exists, then place the queen and move on to placing the next queen (i.e. the next row).</p>
<div class="highlight"><pre><span></span>  <span class="kd">var</span> <span class="nx">solveNQueens</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">column</span><span class="p">,</span> <span class="nx">n</span><span class="p">,</span> <span class="nx">board</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">column</span> <span class="o">===</span> <span class="nx">n</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">count</span><span class="o">++</span><span class="p">;</span>
      <span class="k">return</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">row</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">row</span> <span class="o">&lt;</span> <span class="nx">n</span><span class="p">;</span> <span class="nx">row</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">board</span><span class="p">.</span><span class="nx">togglePiece</span><span class="p">(</span><span class="nx">row</span><span class="p">,</span> <span class="nx">column</span><span class="p">);</span>
      <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">board</span><span class="p">.</span><span class="nx">hasAnyQueensConflicts</span><span class="p">())</span> <span class="p">{</span>
        <span class="nx">solveNQueens</span><span class="p">(</span><span class="nx">column</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">n</span><span class="p">,</span> <span class="nx">board</span><span class="p">);</span>
      <span class="p">}</span>
      <span class="nx">board</span><span class="p">.</span><span class="nx">togglePiece</span><span class="p">(</span><span class="nx">row</span><span class="p">,</span> <span class="nx">column</span><span class="p">);</span>
    <span class="p">}</span>
  <span class="p">};</span>

  <span class="nx">solveNQueens</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">n</span><span class="p">,</span> <span class="nx">board</span><span class="p">);</span>
</pre></div>]]></content:encoded>
      </item>
      <item>
        <guid>http://jessicaobrien.net/pseudoclassical-vs-prototypal-javascript-allows-both#18543</guid>
          <pubDate>Mon, 05 Oct 2015 05:19:27 +0200</pubDate>
        <link>http://jessicaobrien.net/pseudoclassical-vs-prototypal-javascript-allows-both</link>
        <title>Pseudoclassical vs. Prototypal: JavaScript allows both</title>
        <description></description>
        <content:encoded><![CDATA[<p>There are two broad types of inheritance in programming: protoypal and classical. Prototypal just means that objects can inherit directly other objects, while classical involves objects being instances of classes, and subclasses inheriting from superclasses.</p>

<h1 id="pseudoclassical-inheritance">Pseudoclassical Inheritance</h1>

<p>This type of object inheritance uses:<br>
1. The &#39;constructor&#39; function (to create objects)<br>
2. The &#39;new&#39; operator (to create objects)<br>
3. The &#39;prototype&#39; property (to build the inheritance chain)<br>
Here, the pattern involves creating pseudoclasses using constructor functions, then using &#39;new&#39; to create instances of the pseudoclass. The prototype property (inherited by all instances of the pseudoclass) contains the methods common to all instances of the pseudoclass.</p>

<h1 id="prototypal-inheritance">Prototypal Inheritance</h1>

<p>This type of object inheritance uses:<br>
1. &#39;Object.create&#39; (to create a new object)<br>
2. A parameter object (the prototype for the new object)<br>
As mentioned above, here we are creating new objects <strong>directly</strong> from existing ones, without any notion of classes.</p>
]]></content:encoded>
      </item>
  </channel>
</rss>