<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Will Roe&#146;s blog]]></title>
  <link href="http://blog.wjlr.org.uk/atom.xml" rel="self"/>
  <link href="http://blog.wjlr.org.uk/"/>
  <updated>2017-03-19T00:15:29+00:00</updated>
  <id>http://blog.wjlr.org.uk/</id>
  <author>
    <name><![CDATA[Will Roe]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Fast Rust Builds on GitLab CI]]></title>
    <link href="http://blog.wjlr.org.uk/2016/08/16/fast-rust-gitlab-ci.html"/>
    <updated>2016-08-16T22:10:00+01:00</updated>
    <id>http://blog.wjlr.org.uk/2016/08/16/fast-rust-gitlab-ci</id>
    <content type="html"><![CDATA[<p><a href="https://gitlab.com/">GitLab</a> has a very useful integrated CI environment that you can use with pretty much any project.</p>

<!--more-->


<p>For a simple Rust project, we could use a configuration that looks like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="l-Scalar-Plain">image</span><span class="p-Indicator">:</span> <span class="s">&quot;scorpil/rust:stable&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="l-Scalar-Plain">test:cargo</span><span class="p-Indicator">:</span>
</span><span class='line'>  <span class="l-Scalar-Plain">script</span><span class="p-Indicator">:</span>
</span><span class='line'>  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">rustc --version &amp;&amp; cargo --version</span>      <span class="c1"># Print version info for debugging</span>
</span><span class='line'>  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">time cargo test --verbose --jobs 1 --release</span> <span class="c1"># Don&#39;t paralize to make errors more readable</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you build a project with this, it will work (and it&#8217;ll use a Docker image for buzzword compliance), but it&#8217;ll be quite slow, several minutes at least. Most of that time is spent downloading your dependencies and compiling them - on every single build, even if they are unchanged. If you run <code>cargo test</code> locally, even if it needs to compile your code, it should take roughly 10 seconds (probably less).</p>

<p>In order to get fast test runs with Rust, some configuration is necessary. Let&#8217;s walk through changes to the <code>.gitlab-ci.yml</code> file that&#8217;ll speed things up.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="l-Scalar-Plain">test:cargo</span><span class="p-Indicator">:</span>
</span><span class='line'>  <span class="l-Scalar-Plain">...</span>
</span><span class='line'>  <span class="l-Scalar-Plain">cache</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="l-Scalar-Plain">paths</span><span class="p-Indicator">:</span>
</span><span class='line'>      <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">target/</span>
</span><span class='line'>      <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">cargo/</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is necessary to ensure that artifacts such as dependencies are retained between builds. Without this, upon every build, you will see lines like this in the build log:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="l-Scalar-Plain">Downloading cookie v0.2.5</span>
</span><span class='line'><span class="nn">...</span>
</span><span class='line'><span class="l-Scalar-Plain">Compiling cookie v0.2.5</span>
</span></code></pre></td></tr></table></div></figure>


<p>To ensure that dependencies are cached correctly, we need to set the <code>$CARGO_HOME</code> to be inside the build directory (I&#8217;m not sure why, but if you try to cache it as is, it doesn&#8217;t work):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="l-Scalar-Plain">variables</span><span class="p-Indicator">:</span>
</span><span class='line'>  <span class="l-Scalar-Plain">CARGO_HOME</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">$CI_PROJECT_DIR/cargo</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>$CI_PROJECT_DIR</code> is defined by GitLab to be the directory that it unpacks your project in and runs your build.</p>

<p>With this all in place, when you next build your project (after one build to fill the cache with your dependencies), you should see this instead:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="l-Scalar-Plain">Fresh cookie v0.2.5</span>
</span></code></pre></td></tr></table></div></figure>


<p>See that? Everybody likes fresh cookies. Cargo doesn&#8217;t need to compile this library because the built library was in the cache. This reduced my small project&#8217;s build time from ~4 minutes to ~1.2 minutes.</p>

<p>The final <code>.gitlab-ci.yml</code> file looks like this now:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="l-Scalar-Plain">image</span><span class="p-Indicator">:</span> <span class="s">&quot;scorpil/rust:stable&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="l-Scalar-Plain">variables</span><span class="p-Indicator">:</span>
</span><span class='line'>  <span class="l-Scalar-Plain">CARGO_HOME</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">$CI_PROJECT_DIR/cargo</span>
</span><span class='line'>
</span><span class='line'><span class="l-Scalar-Plain">test:cargo</span><span class="p-Indicator">:</span>
</span><span class='line'>  <span class="l-Scalar-Plain">script</span><span class="p-Indicator">:</span>
</span><span class='line'>  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">du -hs target</span>
</span><span class='line'>  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">du -hs cargo</span>
</span><span class='line'>  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">rustc --version &amp;&amp; cargo --version</span>      <span class="c1"># Print version info for debugging</span>
</span><span class='line'>  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">time cargo test --verbose --jobs 1 --release</span> <span class="c1"># Don&#39;t paralize to make errors more readable</span>
</span><span class='line'>  <span class="l-Scalar-Plain">cache</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="l-Scalar-Plain">paths</span><span class="p-Indicator">:</span>
</span><span class='line'>      <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">target/</span>
</span><span class='line'>      <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">cargo/</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Module Pattern in Javascript]]></title>
    <link href="http://blog.wjlr.org.uk/2015/04/04/js-module-pattern.html"/>
    <updated>2015-04-04T09:00:00+01:00</updated>
    <id>http://blog.wjlr.org.uk/2015/04/04/js-module-pattern</id>
    <content type="html"><![CDATA[<p>Javascript has a liberal array of techniques for constructing objects.
Although object oriented style programming can be simulated in
Javascript (which is prototypical in nature), it can get a little
unwieldy.</p>

<!--more-->


<h2>Pseudoclassical OO</h2>

<p>The following code is in the style referred to as pseudoclassical by
Douglas Crockford in <a href="http://shop.oreilly.com/product/9780596517748.do">Javascript: The Good Parts</a>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">empathy</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">name</span><span class="p">;</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">empathy</span> <span class="o">=</span> <span class="nx">empathy</span> <span class="o">||</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">Person</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">get_name</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">name</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">Person</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">get_empathy</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">empathy</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">Person</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">change_empathy</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">amount</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">empathy</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">empathy</span> <span class="o">+</span> <span class="nx">amount</span><span class="p">;</span>
</span><span class='line'>    <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">empathy</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">Person</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">status</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">get_name</span><span class="p">()</span> <span class="o">+</span> <span class="s2">&quot; has &quot;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">get_empathy</span><span class="p">()</span> <span class="o">+</span> <span class="s2">&quot; empathy&quot;</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">Player</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">empathy</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">name</span><span class="p">;</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">empathy</span> <span class="o">=</span> <span class="nx">empathy</span> <span class="o">||</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">Player</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Person</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">Mob</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">empathy</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">name</span><span class="p">;</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">empathy</span> <span class="o">=</span> <span class="nx">empathy</span> <span class="o">||</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">Mob</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Person</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>And we can use the &#8216;classes&#8217; like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">player</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Player</span><span class="p">(</span><span class="s2">&quot;Ada&quot;</span><span class="p">,</span> <span class="mi">100</span><span class="p">);</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">player</span><span class="p">.</span><span class="nx">status</span><span class="p">());</span> <span class="c1">// &#39;Ada has 100 empathy&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">mob</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Mob</span><span class="p">(</span><span class="s2">&quot;Bob&quot;</span><span class="p">,</span> <span class="o">-</span><span class="mi">10</span><span class="p">);</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">mob</span><span class="p">.</span><span class="nx">status</span><span class="p">());</span> <span class="c1">// &#39;Bob has -10 empathy&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">mob</span><span class="p">.</span><span class="nx">change_empathy</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">mob</span><span class="p">.</span><span class="nx">status</span><span class="p">());</span> <span class="c1">// &#39;Bob has -8 empathy&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>There are a few things to note here:</p>

<ul>
<li>There are no private fields or methods, it&#8217;s not feasible to add them</li>
<li>There is duplication in constructors that lie in an inheritance chain
(i.e. where an object&#8217;s prototype is set to another object)</li>
<li>It&#8217;s horribly verbose (a matter of personal taste however)</li>
<li>Due to the lack of private fields, you can&#8217;t have a method called
<code>name()</code> that returns a field called <code>name</code> - one will overwrite the
other in this style</li>
</ul>


<h2>The limits of inheritance</h2>

<p>Even ignoring these issues (and there are other ways to create objects
that work around this limitation), object oriented inheritance is not
the most flexible way to design software. For many domains of
problems, composition or augmentation trumps inheritance. Let&#8217;s
examine a motivating example. In the following class diagram we have
some domain objects for a game. Players and Mobs are Person(s),
Person(s) and Rucksacks are Objects. All is well with this view of the
world, no code is duplicated between objects and it all works as
intended.</p>

<p><img class="extrawide"
src="http://blog.wjlr.org.uk/images/components-before-container-extraction.png" title="Class
object diagram"></p>

<p>Here are some unit tests of these objects to illustrate their functionality:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">JS</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">&quot;jstest&quot;</span><span class="p">),</span>
</span><span class='line'>    <span class="nx">components</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">&quot;../lib/components&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nx">JS</span><span class="p">.</span><span class="nx">Test</span><span class="p">.</span><span class="nx">describe</span><span class="p">(</span><span class="s2">&quot;components&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">before</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="nx">rucksack</span> <span class="o">=</span> <span class="nx">components</span><span class="p">.</span><span class="nx">rucksack</span><span class="p">({</span><span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;Bag of Holding&quot;</span><span class="p">});</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="nx">another_rucksack</span> <span class="o">=</span> <span class="nx">components</span><span class="p">.</span><span class="nx">rucksack</span><span class="p">({</span><span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;Bigger bag&quot;</span><span class="p">});</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="nx">spell</span> <span class="o">=</span> <span class="nx">components</span><span class="p">.</span><span class="nx">spell</span><span class="p">({</span><span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;Potion of empathy&quot;</span><span class="p">});</span>
</span><span class='line'>    <span class="p">});</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">describe</span><span class="p">(</span><span class="s2">&quot;container&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="nx">it</span><span class="p">(</span><span class="s2">&quot;can contain other containers&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>            <span class="k">this</span><span class="p">.</span><span class="nx">rucksack</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">spell</span><span class="p">);</span>
</span><span class='line'>            <span class="k">this</span><span class="p">.</span><span class="nx">another_rucksack</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">rucksack</span><span class="p">);</span>
</span><span class='line'>            <span class="k">this</span><span class="p">.</span><span class="nx">assertEqual</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">another_rucksack</span><span class="p">.</span><span class="nx">quantity</span><span class="p">());</span>
</span><span class='line'>            <span class="k">this</span><span class="p">.</span><span class="nx">assertEqual</span><span class="p">(</span><span class="s2">&quot;Bag of Holding&quot;</span><span class="p">,</span>
</span><span class='line'>                             <span class="k">this</span><span class="p">.</span><span class="nx">another_rucksack</span><span class="p">.</span><span class="nx">items</span><span class="p">()[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span><span class="p">());</span>
</span><span class='line'>        <span class="p">});</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="nx">it</span><span class="p">(</span><span class="s2">&quot;can contain other objects&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>            <span class="k">this</span><span class="p">.</span><span class="nx">rucksack</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">spell</span><span class="p">);</span>
</span><span class='line'>            <span class="k">this</span><span class="p">.</span><span class="nx">assertEqual</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">rucksack</span><span class="p">.</span><span class="nx">quantity</span><span class="p">());</span>
</span><span class='line'>            <span class="k">this</span><span class="p">.</span><span class="nx">assertEqual</span><span class="p">(</span><span class="s2">&quot;Potion of empathy&quot;</span><span class="p">,</span>
</span><span class='line'>                             <span class="k">this</span><span class="p">.</span><span class="nx">rucksack</span><span class="p">.</span><span class="nx">items</span><span class="p">()[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span><span class="p">());</span>
</span><span class='line'>        <span class="p">});</span>
</span><span class='line'>    <span class="p">});</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now say we want to extend our player objects to have an inventory (or
<code>container</code>). We could approach this in a number of ways but
inheritance is not one of them. We already have <code>rucksack</code> and
<code>person</code> inheriting from <code>object</code> so we can&#8217;t just create <code>container</code>
as a superclass of those. We need a way to extend both those objects
and extract the <code>container</code>-related behaviour and state so we don&#8217;t
have code duplication. In Ruby, we might create <code>container</code> as a
<code>Module</code> and <code>include</code> it in our classes.</p>

<p>The method pattern in JS allows us to do something quite similar, but
first, let&#8217;s write a failing test:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="k">this</span><span class="p">.</span><span class="nx">describe</span><span class="p">(</span><span class="s2">&quot;player&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">it</span><span class="p">(</span><span class="s2">&quot;has an inventory&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="nx">assertEqual</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">player</span><span class="p">.</span><span class="nx">quantity</span><span class="p">());</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="nx">player</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">spell</span><span class="p">);</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="nx">assertEqual</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">player</span><span class="p">.</span><span class="nx">quantity</span><span class="p">());</span>
</span><span class='line'>        <span class="k">this</span><span class="p">.</span><span class="nx">assertEqual</span><span class="p">(</span><span class="s2">&quot;Potion of empathy&quot;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">player</span><span class="p">.</span><span class="nx">items</span><span class="p">()[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span><span class="p">());</span>
</span><span class='line'>    <span class="p">});</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<h2>The Module Pattern</h2>

<p>Now we can go ahead and examine how to construct the relationship
between objects that we need. This diagram approximates the idea (I&#8217;m
not exactly clear what options to pass GraphViz to make the
<code>container</code> obviously indicate it is included as a module):</p>

<p><img class="extrawide"
src="http://blog.wjlr.org.uk/images/components-after-container-extraction.png" title="Class
object diagram with container component"></p>

<p>The basic &#8216;shape&#8217; of an object using &#8216;functional composition&#8217; looks
like the following:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">object</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">spec</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="p">{};</span>
</span><span class='line'>
</span><span class='line'>    <span class="nx">that</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nx">spec</span><span class="p">.</span><span class="nx">name</span><span class="p">;</span>
</span><span class='line'>    <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="nx">that</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>spec</code> argument is the object to be augmented (sometimes that&#8217;s as
simple as being the parent object). The <code>that</code> object which gets
returned constitutes the public interface that this object returns.
Because <code>spec</code> is passed in and <code>that</code> is returned, this <code>object</code>
constructor is a base class.</p>

<p>The <code>container</code> constructor function looks like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">container</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">that</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">inventory</span> <span class="o">=</span> <span class="p">[];</span>
</span><span class='line'>
</span><span class='line'>    <span class="nx">that</span><span class="p">.</span><span class="nx">add</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">item</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">inventory</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">item</span><span class="p">);</span>
</span><span class='line'>    <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>    <span class="nx">that</span><span class="p">.</span><span class="nx">quantity</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nx">inventory</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
</span><span class='line'>    <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>    <span class="nx">that</span><span class="p">.</span><span class="nx">items</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nx">inventory</span><span class="p">;</span>
</span><span class='line'>    <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="nx">that</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>Unlike the previous example, <code>container</code> exists to decorate the
provided object with more functionality. If you needed to provide
defaults for this object specifically, you can pass a <code>spec</code> argument
in also but it wasn&#8217;t necessary here. Crucially, the <code>inventory</code>
variable is <a href="https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scope_vs._dynamic_scope">lexically scoped</a> to the <code>container</code> constructor, meaning
no code gets access to it except that inside the <code>container</code>
constructor. This is in contrast to <code>Modules</code> in Ruby where
everything, fields and all, gets included into a <code>Class</code> and becomes
part of that class.</p>

<p>The <code>person</code> object is a little different:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">person</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">spec</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="nx">container</span><span class="p">(</span><span class="nx">object</span><span class="p">(</span><span class="nx">spec</span><span class="p">)),</span>
</span><span class='line'>        <span class="nx">empathy</span> <span class="o">=</span> <span class="nx">spec</span><span class="p">.</span><span class="nx">empathy</span> <span class="o">||</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="nx">that</span><span class="p">.</span><span class="nx">change_empathy</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">amount</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">empathy</span> <span class="o">=</span> <span class="nx">empathy</span> <span class="o">+</span> <span class="nx">amount</span><span class="p">;</span>
</span><span class='line'>    <span class="p">};</span>
</span><span class='line'>    <span class="nx">that</span><span class="p">.</span><span class="nx">empathy</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nx">empathy</span><span class="p">;</span>
</span><span class='line'>    <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="nx">that</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>We&#8217;re back to using a <code>spec</code> argument here so that we can instantiate
a <code>person</code> with some default amount of empathy. The <code>that =
container(object(spec))</code> stands in for traditional object inheritance.</p>

<p>After we&#8217;ve created the <code>container</code> object and wired it up to <code>person</code>
and <code>rucksack</code>, the code for concrete objects in our toy example looks
like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span><span class="p">.</span><span class="nx">rucksack</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">spec</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="nx">container</span><span class="p">(</span><span class="nx">object</span><span class="p">(</span><span class="nx">spec</span><span class="p">));</span>
</span><span class='line'>    <span class="k">return</span> <span class="nx">that</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span><span class="p">.</span><span class="nx">mob</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">spec</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="nx">person</span><span class="p">(</span><span class="nx">spec</span><span class="p">);</span>
</span><span class='line'>    <span class="k">return</span> <span class="nx">that</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span><span class="p">.</span><span class="nx">player</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">spec</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="nx">person</span><span class="p">(</span><span class="nx">spec</span><span class="p">);</span>
</span><span class='line'>    <span class="k">return</span> <span class="nx">that</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span><span class="p">.</span><span class="nx">spell</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">spec</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="nx">object</span><span class="p">(</span><span class="nx">spec</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="nx">that</span><span class="p">.</span><span class="nx">affect</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="nx">that</span><span class="p">.</span><span class="nx">inRange</span><span class="p">(</span><span class="nx">obj</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'>            <span class="nx">obj</span><span class="p">.</span><span class="nx">change_empathy</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="nx">that</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>Although the diagram above implies a strict inheritance hierarchy,
that is not necessarily the case. The difference between <code>object</code> and
<code>container</code> is subtle, with <code>object</code> hiding the <code>spec</code> object and
returning a new object (just as <code>person</code> and <code>rucksack</code> do), whereas
<code>container</code> just adds methods to an object.</p>

<p>One last thing! It might be apparent, but it&#8217;s worth calling out the
other advantage this gives us: dynamic runtime extensions. If you want
the <code>spell</code> object to be able to contain items, you could do so at any
stage, running:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">my_spell</span> <span class="o">=</span> <span class="nx">spell</span><span class="p">({</span><span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;Confusing potion&quot;</span><span class="p">});</span>
</span><span class='line'><span class="c1">// my_spell doesn&#39;t have an inventory</span>
</span><span class='line'><span class="nx">container</span><span class="p">(</span><span class="nx">my_spell</span><span class="p">);</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">my_spell</span><span class="p">.</span><span class="nx">quantity</span><span class="p">());</span> <span class="c1">// &#39;0&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This code modifies the <code>my_spell</code> variable, but no other variables
that use the <code>spell</code> constructor function. This could be useful in
games where items can interact, giving each other different
functionality (e.g. imagine a potion that allows bookcases to float or
drinking a flying potion adds the <code>flying</code> component to a player). In
those cases, there&#8217;s no need for complex if/else/switch-style
programming, just augment the object with the desired module
and that&#8217;s it!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Raspberry Pi 2 Speed]]></title>
    <link href="http://blog.wjlr.org.uk/2015/03/25/raspberry-pi-2-speed.html"/>
    <updated>2015-03-25T22:18:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2015/03/25/raspberry-pi-2-speed</id>
    <content type="html"><![CDATA[<p>I just benchmarked my new <a href="http://www.raspberrypi.org/products/raspberry-pi-2-model-b/">Raspberry Pi 2 Model B</a> against my old
<a href="http://www.raspberrypi.org/products/model-b/">Raspberry Pi 1 Model B</a> using the
<a href="https://github.com/clojure/clojurescript/wiki/Quick-Start">ClojureScript Quick Start Guide</a> (specifically timing the build
step):</p>

<!--more-->


<pre><code>time java -cp cljs.jar:src clojure.main build.clj
</code></pre>

<p>On the <a href="http://www.raspberrypi.org/products/model-b/">Raspberry Pi 1 Model B</a> this takes 61.59 seconds, whereas on
the new <a href="http://www.raspberrypi.org/products/raspberry-pi-2-model-b/">Raspberry Pi 2 Model B</a> this takes 18.42 seconds!</p>

<figure class="img fillwidth"><img src="http://blog.wjlr.org.uk/images/raspberrypi2.jpg"
alt="Raspberry Pi 2" title="Raspberry Pi 2"><figcaption>My Raspberry Pi
2 in a very fetching <a href="http://shop.pimoroni.com/products/pibow-coupe">PiBow Coupé case</a></figcaption></figure>


<p>For comparison, on my MacBook Air (11-inch, Mid 2013, 1.4 GHz Intel
Core i5, 8GB RAM), while lots of software was running, so not a
complete fair test (the Pis were booted up into the console, not even
running X) this takes 5.75 seconds.</p>

<p>Considering the difference in specifications of these machines, the
Raspberry Pi 2 is remarkably fast. I could even load up and play with
<a href="http://sonic-pi.net/">Sonic Pi</a> pretty quickly (on the original Model B that took long
enough to make a cup of tea).</p>

<p>Looks like I&#8217;m going to be drinking less tea in the future.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Free as in Learning]]></title>
    <link href="http://blog.wjlr.org.uk/2015/03/24/free-as-in-learning.html"/>
    <updated>2015-03-24T09:00:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2015/03/24/free-as-in-learning</id>
    <content type="html"><![CDATA[<p>If you want to release your music for everybody to hear then it&#8217;s a good idea to
licence it with a creative commons licence. That way your music or drawings can
be remixed and used in surprising ways.</p>

<!--more-->


<p>But the emphasis on the finished product doesn&#8217;t interest me as much. I&#8217;d rather
open source the process by which art is made rather than the result.</p>

<p>The new wave of digitally assembled music opens the way to truly open source
music - when music can have source code, can have a version history - we can all
learn from how it gets made.</p>

<p>Put your music in the public domain if you like, but don&#8217;t forget to release the
things you learned along the way.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Serialist Micro Piano Trio No. 1]]></title>
    <link href="http://blog.wjlr.org.uk/2015/03/23/serial-micro-piano-trio-no-1.html"/>
    <updated>2015-03-23T20:00:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2015/03/23/serial-micro-piano-trio-no-1</id>
    <content type="html"><![CDATA[<p>My first &#8220;released&#8221; (algorithmically generated) composition is a micro
piano trio in a <a href="http://en.wikipedia.org/wiki/Serialism">serialist</a> style.</p>

<!--more-->


<p>It all started when I borrowed the book <a href="https://www.goodreads.com/book/show/3150827-simple-composition">Simple Composition</a> by
<a href="http://en.wikipedia.org/wiki/Charles_Wuorinen">Charles Wuorinen</a> from the Sussex University library (I was
studying Computer Science &amp; Artificial Intelligence at the time, some
years ago).</p>

<figure class="img fillwidth"><img src="http://blog.wjlr.org.uk/images/University_of_Sussex_Library.jpg" alt="Sussex Uni Library" title="Sussex Uni Library"><figcaption>Sussex University Library &#8211; Photo Public Domain, Wikipedia, Filtered</figcaption></figure>


<p>More recently I tracked down a copy so that I could have a go at
writing twelve-tone music. This book explains the basics of composing
in the <a href="http://en.wikipedia.org/wiki/Twelve-tone_technique">twelve tone technique</a> created by <a href="http://en.wikipedia.org/wiki/Arnold_Schoenberg">Arnold Schoenberg</a>.
Some days ago I started playing around with the concepts from that
book in <a href="http://overtone.github.io/">Overtone</a>. It was a lot of fun.</p>

<div class='embed tweet'><blockquote class="twitter-tweet"><p>I might be playing with <a href="https://twitter.com/overtone">@overtone</a> possibly <a href="https://twitter.com/hashtag/clojure?src=hash">#clojure</a> <a href="http://t.co/8KRPc2qmpr">pic.twitter.com/8KRPc2qmpr</a></p>&mdash; Will Roe (@wjlroe) <a href="https://twitter.com/wjlroe/status/575775558082150402">March 11, 2015</a></blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script></div>


<p>If you&#8217;re not familiar with twelve-tone composition or serialism in
music, you could do a lot worse than to watch this awesome video by
<a href="http://vihart.com">ViHart</a>:</p>

<iframe width="560" height="315" src="https://www.youtube.com/embed/4niz8TfY794" frameborder="0" allowfullscreen></iframe>


<h2>The code</h2>

<p>I used a library called <a href="https://github.com/ctford/leipzig">Leipzig</a> written by <a href="http://literateprogrammer.blogspot.co.uk/">Chris Ford</a> to build
up the composition from an initial tone row (a set of 12 integers),
via several functional transformations of the tone row and into 3
instrumental parts: piano, violin and cello.</p>

<p>Since this piece uses a randomly-generated tone row and
randomly-generated sequence of both note duration and playing style
(vibrato, non-vibrato or pizzicato),
it&#8217;s not actually reproducible from the code. The code is useful
mainly for encoding the basic form of the piece and the parameters
of the work (such as instruments, length of the piece, which elements
are random and which aren&#8217;t etc.).</p>

<p>In future I will work out some way to capture the notes being
generated. The simplest way to do this would be to save all the
structures produced by the <code>piano-trio</code> function into an <a href="https://github.com/edn-format/edn">EDN</a> file,
which could be trivially played back at a later date. Transformation
of that data into traditional musical notation wouldn&#8217;t be too tricky
either.</p>

<p>The structure of this piece consists of 3 instrumental parts played in
parallel. The main function is
<a href="https://github.com/wjlroe/serial/blob/micro-piano-trio-1/src/serial/twelve_tone.clj#L200-L213"><code>piano-trio</code></a>
and is inline below:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">piano-trio</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">bpm</span> <span class="nv">tone-row</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">in-tempo</span>
</span><span class='line'>   <span class="nv">bpm</span>
</span><span class='line'>   <span class="p">(</span><span class="nf">with</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">-&gt;&gt;</span> <span class="p">(</span><span class="nf">total-serial</span> <span class="nv">tone-row</span><span class="p">)</span>
</span><span class='line'>         <span class="p">(</span><span class="nf">play-on</span> <span class="ss">:cello</span><span class="p">)</span>
</span><span class='line'>         <span class="p">(</span><span class="nf">serial-style</span> <span class="ss">:cello</span><span class="p">))</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">-&gt;&gt;</span> <span class="p">(</span><span class="nf">total-serial</span> <span class="nv">tone-row</span><span class="p">)</span>
</span><span class='line'>         <span class="p">(</span><span class="nf">play-on</span> <span class="ss">:violin</span><span class="p">)</span>
</span><span class='line'>         <span class="p">(</span><span class="nf">serial-style</span> <span class="ss">:violin</span><span class="p">))</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">-&gt;&gt;</span> <span class="p">(</span><span class="nf">total-serial</span> <span class="nv">tone-row</span><span class="p">)</span>
</span><span class='line'>         <span class="p">(</span><span class="nf">play-on</span> <span class="ss">:piano</span><span class="p">)</span>
</span><span class='line'>         <span class="p">(</span><span class="nf">serial-style</span> <span class="ss">:piano</span><span class="p">)))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The most interesting aspect as it relates to musical transformation is
the definition of retrograde, inversion and retrograde-inversion:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">retrograde</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">row</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">reverse </span><span class="nv">row</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">inversion</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">row</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">map - </span><span class="nv">row</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">retrograde-inversion</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">comp </span><span class="nv">retrograde</span> <span class="nv">inversion</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>It was for this reason - the applicability of pure functions and
functional composition - that I felt <a href="http://overtone.github.io/">overtone</a> and Clojure were such a
good fit for writing <a href="http://en.wikipedia.org/wiki/Serialism">serialist</a> music. It&#8217;s even more applicable to
12-tone composition than tonal (diatonic) music since the rules are so
much more mechanistic.</p>

<h2>The recording</h2>

<p><iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/197309694&amp;color=ff5500&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false"></iframe></p>


<p>I had a lot of fun exploring serialism through Overtone and the
Leipzig library. I was inspired to release this little track and to
spend more time on code like this when I watched a great talk called
<a href="https://www.youtube.com/watch?v=kovJHzQNsg0">Make Art, Not Apps</a> by <a href="https://twitter.com/ThisIsJohnBrown">@ThisIsJohnBrown</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Building Minecraft Plugins With Tup]]></title>
    <link href="http://blog.wjlr.org.uk/2015/03/10/building-minecraft-plugins-with-tup.html"/>
    <updated>2015-03-10T23:10:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2015/03/10/building-minecraft-plugins-with-tup</id>
    <content type="html"><![CDATA[<p>I&#8217;m having fun reading <a href="https://pragprog.com/book/ahmine2/learn-to-program-with-minecraft-plugins">Learn to Program with Minecraft Plugins</a> at
the moment and I decided to try out a new Make-like tool called
<a href="http://gittup.org/tup">tup</a> for building the plugins.</p>

<!--more-->


<p>Each example plugin from the source code of this book has a build
script that looks like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c">#!/bin/sh</span>
</span><span class='line'><span class="c"># Set the variable MCSERVER to ~/Desktop/server</span>
</span><span class='line'><span class="c"># unless it&#39;s already set</span>
</span><span class='line'>: <span class="k">${</span><span class="nv">MCSERVER</span><span class="p">:=</span><span class="s2">&quot;$HOME/Desktop/server&quot;</span><span class="k">}</span>
</span><span class='line'><span class="nv">MODS</span><span class="o">=</span><span class="s2">&quot;$MCSERVER&quot;</span>/CanaryMod.jar
</span><span class='line'>
</span><span class='line'><span class="c"># Make the build directories if they aren&#39;t there.</span>
</span><span class='line'><span class="c"># Throw away any error if they are.</span>
</span><span class='line'>mkdir bin 2&gt;/dev/null
</span><span class='line'>mkdir dist 2&gt;/dev/null
</span><span class='line'>
</span><span class='line'><span class="c"># Remove any previous build products</span>
</span><span class='line'>rm -f bin/*/*.class
</span><span class='line'>rm -f dist/*.jar
</span><span class='line'>
</span><span class='line'><span class="c"># Get the name of this plugin</span>
</span><span class='line'><span class="c"># from the directory it&#39;s in</span>
</span><span class='line'><span class="nv">HERE</span><span class="o">=</span><span class="sb">`</span><span class="nb">pwd</span><span class="sb">`</span>
</span><span class='line'><span class="nv">NAME</span><span class="o">=</span><span class="sb">`</span>basename <span class="s2">&quot;$HERE&quot;</span><span class="sb">`</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 1. Compile</span>
</span><span class='line'><span class="nb">echo</span> <span class="s2">&quot;Compiling with javac...&quot;</span>
</span><span class='line'>javac -Xlint:deprecation src/*/*.java -d bin -classpath <span class="s2">&quot;$MODS&quot;</span> -sourcepath src -g:lines,vars,source <span class="o">||</span> <span class="nb">exit </span>2
</span><span class='line'>
</span><span class='line'><span class="c"># 2. Build the jar</span>
</span><span class='line'><span class="nb">echo</span> <span class="s2">&quot;Creating jar file...&quot;</span>
</span><span class='line'>jar -cf dist/<span class="s2">&quot;$NAME.jar&quot;</span> *.inf -C bin . <span class="o">||</span> <span class="nb">exit </span>3
</span><span class='line'>
</span><span class='line'><span class="c"># 3. Copy to server</span>
</span><span class='line'><span class="nb">echo</span> <span class="s2">&quot;Deploying jar to $MCSERVER/plugins...&quot;</span>
</span><span class='line'><span class="nb">test</span> ! -d <span class="s2">&quot;$MCSERVER/plugins&quot;</span> <span class="o">&amp;&amp;</span> mkdir <span class="s2">&quot;$MCSERVER/plugins&quot;</span>
</span><span class='line'>cp dist/<span class="nv">$NAME</span>.jar <span class="s2">&quot;$MCSERVER&quot;</span>/plugins <span class="o">||</span> <span class="nb">exit </span>4
</span><span class='line'>
</span><span class='line'><span class="nb">echo</span> <span class="s2">&quot;Completed Successfully.&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>The above is a perfectly good build script especially as the intended
audience of the book doesn&#8217;t need to learn about build tools like Make
or Maven or Ant.</p>

<p><a href="http://gittup.org/tup">tup</a> is an interesting build tool for a number of reasons.
Particularly the fairly straightforward configuration and file monitoring
rebuilds (only on Linux). The other intriguing feature of tup is you
can run <code>tup upd</code> anywhere in your source code and it&#8217;ll build
everything that needs building, which is pretty convenient for large
source trees with multiple build targets/libraries - just like the
example plugins for this book!</p>

<p>So here&#8217;s my first attempt at a <code>Tupfile</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">MCSERVER</span>   <span class="o">=</span> /Users/will/Downloads/canary-server
</span><span class='line'><span class="nv">MODS</span>       <span class="o">=</span> <span class="k">$(</span>MCSERVER<span class="k">)</span>/CanaryMod.jar
</span><span class='line'><span class="nv">JAVAC_OPTS</span> <span class="o">=</span> -Xlint:deprecation
</span><span class='line'><span class="nv">DEBUGGING</span>  <span class="o">=</span> -g:lines,vars,source
</span><span class='line'>
</span><span class='line'>: foreach src/helloworld/*.java <span class="p">|</span>&gt; javac <span class="k">$(</span>JAVAC_OPTS<span class="k">)</span> %f -d bin -classpath <span class="s2">&quot;$(MODS)&quot;</span> -sourcepath src <span class="k">$(</span>DEBUGGING<span class="k">)</span> <span class="p">|</span>&gt; bin/helloworld/%g.class
</span><span class='line'>: *.inf bin/helloworld/*.class <span class="p">|</span>&gt; jar -cf %o *.inf -C bin . <span class="o">&amp;&amp;</span> cp %o <span class="k">$(</span>MCSERVER<span class="k">)</span>/plugins/ <span class="p">|</span>&gt; dist/%d.jar
</span></code></pre></td></tr></table></div></figure>


<p>I have a few reflections about this:</p>

<ul>
<li>I don&#8217;t know how environment variables work with tup. Just like
Make, it has its own variables but it doesn&#8217;t appear to let any
shell variables in (that&#8217;s why my full home directory is hard-coded
at the top of the file).</li>
<li>Each line in the Tupfile consists of <code>inputs |&gt; commands |&gt; outputs</code>
which is very functional-like and intuitive to me. There doesn&#8217;t
appear to be an easy way to have outputs that are outside your
source tree because I had errors when I tried to copy the JAR into
my server/plugins directory as a separate step, so I collapsed it
into the previous JAR-building step.</li>
<li><code>%g</code> refers to a glob in the input files, but alas only the first
glob, so I couldn&#8217;t write a general rule for building any package
regardless (like: <code>foreach src/*/*.java |&gt; javac %f -d bin |&gt;
bin/%g.class</code>). The other options for output flags include <code>%b</code>
which is the input base filename (e.g. <code>HelloWorld.java</code>) and <code>%B</code>
which is the same without the extension (e.g. <code>HelloWorld</code>). I
couldn&#8217;t work out how to translate the <code>src/a/b.java</code> into
<code>bin/a/b.class</code> for any <code>a</code> and <code>b</code>. In Make you can do something
like <code>$(source_files:%.java=bin/%.class)</code> or similar and I can&#8217;t
find an analogue in tup so far.</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Custom Colours for Tmux]]></title>
    <link href="http://blog.wjlr.org.uk/2015/02/27/custom-colours-for-tmux.html"/>
    <updated>2015-02-27T17:31:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2015/02/27/custom-colours-for-tmux</id>
    <content type="html"><![CDATA[<p>I have been using a rather dull <a href="http://ethanschoonover.com/solarized">Solarized</a> (by
<a href="http://observer.com/2015/02/meet-the-man-behind-solarized-the-most-important-color-scheme-in-computer-history/">Ethan Schoonover</a>) tmux theme for a while now<!--more-->, it looks like this:</p>

<p><img src="http://blog.wjlr.org.uk/images/tmux-solarized.png" class="extrawide"></p>

<p>I tried to tweak the colours to vaguely match the magenta powerline theme of an
Emacs theme called <a href="https://github.com/kuanyui/moe-theme.el">moe-theme</a> (Emacs doesn&#8217;t work well with the
powerline-patched fonts, so the unicode arrows look funny):</p>

<p><img src="http://blog.wjlr.org.uk/images/emacs-moe-theme.png" class="extrawide"></p>

<p>I have acheived something close and it looks like this (you need
powerline-patched fonts for the unicode characters to work):</p>

<p><img src="http://blog.wjlr.org.uk/images/tmux-magenta.png" class="extrawide"></p>

<p>Here is the final config for the status colours and content (I decided
to ditch the segment that shows my username, since it&#8217;s always the
same and I know who I am). This goes in your <code>~/.tmux.conf</code>:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>set -g status-fg black
</span><span class='line'>set -g status-bg colour231
</span><span class='line'>set -g status-left '#[fg=colour234,bg=colour162,bold] ❐ #S #[fg=colour162,bg=colour231,nobold]⮀'
</span><span class='line'>set -g window-status-format "#[fg=black,bg=colour231] #I #[fg=black,bg=colour231]#W "
</span><span class='line'>set -g window-status-current-format "#[fg=colour231,bg=colour141]⮀#[fg=colour231,bg=colour141,bold] #I ⮁ #W #[fg=colour141,bg=colour231,nobold]⮀"
</span><span class='line'>set -g status-right '#[fg=colour162,bg=colour231]⮂##[fg=colour231,bg=colour162] %H:%M %d-%b-%y'</span></code></pre></td></tr></table></div></figure>


<p>Emacs themes and tmux have different colour palettes. Here&#8217;s how
moe-theme specifies some powerline colours for the modeline:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='elisp'><span class='line'><span class="p">(</span><span class="nv">set-face-attribute</span> <span class="ss">&#39;powerline-active1</span> <span class="no">nil</span> <span class="ss">:background</span> <span class="s">&quot;#ffafff&quot;</span> <span class="ss">:foreground</span> <span class="s">&quot;#ff1f8b&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Which is easy enough - they are simply hex colour codes, the same as
can be used in CSS. However tmux uses 24-bit <a href="http://jonasjacek.github.io/colors/">xterm colour codes</a>
(for a generous total of 256 colours). In order to map between them
and find the closest colour I found a handy <a href="https://gist.github.com/MicahElliott/719710">python script</a> which
you can call with a hex code (e.g. <code>python colortrans.py ff4ea3</code>) and
it&#8217;ll print the nearest xterm colour code. If you call the script with
no arguments it&#8217;ll print a mapping table of xterm colour codes to hex
codes, so you can tweak the value you&#8217;re using. Or you can simply use
this <a href="http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html">colour chart</a>. One thing that stalled my progress for a while
was I kept writing <code>[fg=color183,bg=color213]</code> sort of thing and tmux
is unusual in using the French/British spelling of colour, which is
unusual in programming. At least it&#8217;s otherwise straightforward,
unlike <a href="https://wiki.archlinux.org/index.php/Color_Bash_Prompt#List_of_colors_for_prompt_and_Bash">bash color codes</a> which look like somebody fell on the keyboard.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Css-mode Broken on Debian With Emacs 24]]></title>
    <link href="http://blog.wjlr.org.uk/2015/01/24/css-mode-emacs-debian.html"/>
    <updated>2015-01-24T09:00:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2015/01/24/css-mode-emacs-debian</id>
    <content type="html"><![CDATA[<!--more-->


<p>If you get an error like this</p>

<pre><code>Symbol's function definition is void: apropos-macrop
</code></pre>

<p>When editing CSS files with Emacs on Debian (testing or unstable at
the time of writing), then this is because the package <code>css-mode</code>
includes code that no longer works with Emacs version 24 and has been
abandoned (emacs contains its own css-mode).</p>

<p>The solution is simply to uninstall <code>css-mode</code>:</p>

<pre><code>sudo apt-get remove css-mode
</code></pre>

<p>That&#8217;s it.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Fast String Interpolation in ClojureScript]]></title>
    <link href="http://blog.wjlr.org.uk/2015/01/17/fast-string-interpolation-cljs.html"/>
    <updated>2015-01-17T03:30:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2015/01/17/fast-string-interpolation-cljs</id>
    <content type="html"><![CDATA[<p>In my
<a href="http://blog.wjlr.org.uk/2015/01/15/string-interpolation-clojure.html">previous post on string interpolation in Clojure</a>,
I benchmarked <code>&lt;&lt;</code> from <code>core.incubator</code> and it proved both useful and
performant. But can this useful macro be used from ClojureScript? Yes.</p>

<!--more-->


<p>Add <code>core.incubator</code> to your dependencies in <code>project.clj</code> of your
ClojureScript project:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defproject </span><span class="nv">some-project</span> <span class="s">&quot;1.0.0-SNAPSHOT&quot;</span>
</span><span class='line'>  <span class="ss">:dependencies</span> <span class="p">[</span><span class="nv">...</span>
</span><span class='line'>                 <span class="p">[</span><span class="nv">org.clojure/core.incubator</span> <span class="s">&quot;0.1.3&quot;</span><span class="p">]</span>
</span><span class='line'>                 <span class="nv">...</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now you can require the <code>strint</code> macros in your ClojureScript source
(e.g. in <code>src/cljs/app/core.cljs</code>):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">ns </span><span class="nv">app.core</span>
</span><span class='line'>  <span class="p">(</span><span class="ss">:require-macros</span> <span class="p">[</span><span class="nv">clojure.core.strint</span> <span class="ss">:as</span> <span class="nv">strint</span><span class="p">]))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now you can use the fast string interpolator thus:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">enable-console-print!</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nb">name </span><span class="s">&quot;Ethel Smyth&quot;</span>
</span><span class='line'>      <span class="nv">profession</span> <span class="s">&quot;Composer&quot;</span>
</span><span class='line'>      <span class="nv">born</span> <span class="mi">1858</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">println</span>
</span><span class='line'>   <span class="p">(</span><span class="nf">strint/&lt;&lt;</span> <span class="s">&quot;The person named ~{name} works as a ~{profession}</span>
</span><span class='line'><span class="s">and was born in ~{born}&quot;</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>That&#8217;s it!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Fast String Interpolation in Clojure]]></title>
    <link href="http://blog.wjlr.org.uk/2015/01/15/string-interpolation-clojure.html"/>
    <updated>2015-01-15T21:55:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2015/01/15/string-interpolation-clojure</id>
    <content type="html"><![CDATA[<!--more-->


<p>There are two main ways to build strings in Clojure: <a href="https://clojuredocs.org/clojure.core/str"><code>str</code></a> and
<a href="https://clojuredocs.org/clojure.core/format"><code>format</code></a>. <code>str</code> essentially does string concatenation like
this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nb">str </span><span class="s">&quot;This is a sentence with &quot;</span> <span class="nb">some </span><span class="s">&quot; variables &quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>I find <code>str</code> forms somewhat unreadable, especially on one line. They
require the reader to mentally keep track of quote marks and spaces
around variables.</p>

<p>On the other hand, <code>format</code> offers a fully-featured string interpolation
function using Java&#8217;s <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html">Formatter</a> class:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">format</span> <span class="s">&quot;This string has another string: %s in it and a number: %.2f&quot;</span>
</span><span class='line'>         <span class="s">&quot;hello!&quot;</span>
</span><span class='line'>         <span class="mf">30.1</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>In ruby we might use string interpolation thus:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">str_interpolate</span> <span class="nb">name</span><span class="p">,</span> <span class="n">profession</span><span class="p">,</span> <span class="n">born</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="s2">&quot;The person named </span><span class="si">#{</span><span class="nb">name</span><span class="si">}</span><span class="s2"> works as a </span><span class="si">#{</span><span class="n">profession</span><span class="si">}</span><span class="s2"> and was born in </span><span class="si">#{</span><span class="n">born</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">str_interpolate</span> <span class="s2">&quot;Ethel Smyth&quot;</span><span class="p">,</span> <span class="s2">&quot;Composer&quot;</span><span class="p">,</span> <span class="mi">1858</span>
</span></code></pre></td></tr></table></div></figure>


<p>The advantage of string interpolation like this is how readable the
code can be.</p>

<p>The problem with prefering <code>format</code> over <code>str</code> is the difference in
performance. <code>format</code> is a lot more complex and if all you&#8217;re doing is
string concatenation, then it&#8217;ll not do the job as quickly as <code>str</code>
does (which uses a <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html">StringBuilder</a> under the hood).</p>

<h2>Benchmarks!</h2>

<p>The following code uses the <a href="https://github.com/hugoduncan/criterium">Criterium</a> library aliased to <code>bench</code>.</p>

<p>First let&#8217;s define a function using <code>str</code> and benchmark it:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">str-concat-fun</span>
</span><span class='line'>  <span class="p">[</span><span class="nb">name </span><span class="nv">profession</span> <span class="nv">born</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">str </span><span class="s">&quot;The person named &quot;</span>
</span><span class='line'>       <span class="nv">name</span>
</span><span class='line'>       <span class="s">&quot; works as a &quot;</span>
</span><span class='line'>       <span class="nv">profession</span>
</span><span class='line'>       <span class="s">&quot; and was born in &quot;</span>
</span><span class='line'>       <span class="nv">born</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">bench/bench</span> <span class="p">(</span><span class="nf">str-concat-fun</span> <span class="nb">name </span><span class="nv">profession</span> <span class="nv">born</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Which results in (256ns):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="nv">Evaluation</span> <span class="nb">count </span><span class="err">:</span> <span class="mi">241104540</span> <span class="nv">in</span> <span class="mi">60</span> <span class="nv">samples</span> <span class="nv">of</span> <span class="mi">4018409</span> <span class="nv">calls.</span>
</span><span class='line'>             <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">mean</span> <span class="err">:</span> <span class="mf">256.236563</span> <span class="nv">ns</span>
</span><span class='line'>    <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">std-deviation</span> <span class="err">:</span> <span class="mf">4.617404</span> <span class="nv">ns</span>
</span><span class='line'>   <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">lower</span> <span class="nv">quantile</span> <span class="err">:</span> <span class="mf">250.226629</span> <span class="kd">ns </span><span class="p">(</span> <span class="mf">2.5</span><span class="nv">%</span><span class="p">)</span>
</span><span class='line'>   <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">upper</span> <span class="nv">quantile</span> <span class="err">:</span> <span class="mf">266.339191</span> <span class="kd">ns </span><span class="p">(</span><span class="mf">97.5</span><span class="nv">%</span><span class="p">)</span>
</span><span class='line'>                   <span class="nv">Overhead</span> <span class="nv">used</span> <span class="err">:</span> <span class="mf">1.166050</span> <span class="nv">ns</span>
</span><span class='line'>
</span><span class='line'><span class="nv">Found</span> <span class="mi">5</span> <span class="nv">outliers</span> <span class="nv">in</span> <span class="mi">60</span> <span class="nv">samples</span> <span class="p">(</span><span class="mf">8.3333</span> <span class="nv">%</span><span class="p">)</span>
</span><span class='line'>        <span class="nv">low-severe</span>       <span class="mi">5</span> <span class="p">(</span><span class="mf">8.3333</span> <span class="nv">%</span><span class="p">)</span>
</span><span class='line'> <span class="nv">Variance</span> <span class="nv">from</span> <span class="nv">outliers</span> <span class="err">:</span> <span class="mf">7.7764</span> <span class="nv">%</span> <span class="nv">Variance</span> <span class="nv">is</span> <span class="nv">slightly</span> <span class="nv">inflated</span> <span class="nv">by</span> <span class="nv">outliers</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now let&#8217;s check the <code>format</code> version:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">format-fun</span>
</span><span class='line'>  <span class="p">[</span><span class="nb">name </span><span class="nv">profession</span> <span class="nv">born</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">format</span> <span class="s">&quot;The person named %s works as a %s and was born in %d&quot;</span>
</span><span class='line'>          <span class="nv">name</span>
</span><span class='line'>          <span class="nv">profession</span>
</span><span class='line'>          <span class="nv">born</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">bench/bench</span> <span class="p">(</span><span class="nf">format-fun</span> <span class="nb">name </span><span class="nv">profession</span> <span class="nv">born</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Which results in (1.7µs):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="nv">Evaluation</span> <span class="nb">count </span><span class="err">:</span> <span class="mi">34997760</span> <span class="nv">in</span> <span class="mi">60</span> <span class="nv">samples</span> <span class="nv">of</span> <span class="mi">583296</span> <span class="nv">calls.</span>
</span><span class='line'>             <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">mean</span> <span class="err">:</span> <span class="mf">1.703759</span> <span class="err">µ</span><span class="nv">s</span>
</span><span class='line'>    <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">std-deviation</span> <span class="err">:</span> <span class="mf">36.732362</span> <span class="nv">ns</span>
</span><span class='line'>   <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">lower</span> <span class="nv">quantile</span> <span class="err">:</span> <span class="mf">1.663752</span> <span class="err">µ</span><span class="nv">s</span> <span class="p">(</span> <span class="mf">2.5</span><span class="nv">%</span><span class="p">)</span>
</span><span class='line'>   <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">upper</span> <span class="nv">quantile</span> <span class="err">:</span> <span class="mf">1.779579</span> <span class="err">µ</span><span class="nv">s</span> <span class="p">(</span><span class="mf">97.5</span><span class="nv">%</span><span class="p">)</span>
</span><span class='line'>                   <span class="nv">Overhead</span> <span class="nv">used</span> <span class="err">:</span> <span class="mf">1.166050</span> <span class="nv">ns</span>
</span><span class='line'>
</span><span class='line'><span class="nv">Found</span> <span class="mi">2</span> <span class="nv">outliers</span> <span class="nv">in</span> <span class="mi">60</span> <span class="nv">samples</span> <span class="p">(</span><span class="mf">3.3333</span> <span class="nv">%</span><span class="p">)</span>
</span><span class='line'>        <span class="nv">low-severe</span>       <span class="mi">2</span> <span class="p">(</span><span class="mf">3.3333</span> <span class="nv">%</span><span class="p">)</span>
</span><span class='line'> <span class="nv">Variance</span> <span class="nv">from</span> <span class="nv">outliers</span> <span class="err">:</span> <span class="mf">9.4397</span> <span class="nv">%</span> <span class="nv">Variance</span> <span class="nv">is</span> <span class="nv">slightly</span> <span class="nv">inflated</span> <span class="nv">by</span> <span class="nv">outliers</span>
</span></code></pre></td></tr></table></div></figure>


<p>That&#8217;s not good, but not entirely unexpected. An order of magnitude
slower to use <code>format</code> for string contatenation tasks like this.</p>

<p>One of the advantages of Clojure is the promise of powerful,
expressive abstractions and not having to compromise on those
abstractions to achieve performance. In a <a href="http://cemerick.com/2009/12/04/string-interpolation-in-clojure/">blog post</a> about
string interpolation in Clojure, Chas Emerick proposes a macro for
simple string interpolation that would behave much like Ruby&#8217;s does.
This has made its way into the <a href="https://github.com/clojure/core.incubator/">core.incubator</a> project and can be
used in projects today.</p>

<p>To require it, add <code>core.incubator</code> to your project&#8217;s dependencies and
add the following to any namespace that needs it:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">ns </span><span class="nv">example...</span><span class="p">)</span>
</span><span class='line'>  <span class="p">(</span><span class="ss">:require</span> <span class="p">[</span><span class="nv">clojure.core.strint</span> <span class="ss">:refer</span> <span class="p">[</span><span class="nv">&lt;&lt;</span><span class="p">]]))</span>
</span></code></pre></td></tr></table></div></figure>


<p>So now we can define a function like the others using this new macro:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">interpolation-fun</span>
</span><span class='line'>  <span class="p">[</span><span class="nb">name </span><span class="nv">profession</span> <span class="nv">born</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">&lt;&lt;</span> <span class="s">&quot;The person named ~{name} works as a ~{profession} and was born in ~{born}&quot;</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">bench/bench</span> <span class="p">(</span><span class="nf">interpolation-fun</span> <span class="nb">name </span><span class="nv">profession</span> <span class="nv">born</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>And this results in (272ns):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="nv">Evaluation</span> <span class="nb">count </span><span class="err">:</span> <span class="mi">222317940</span> <span class="nv">in</span> <span class="mi">60</span> <span class="nv">samples</span> <span class="nv">of</span> <span class="mi">3705299</span> <span class="nv">calls.</span>
</span><span class='line'>             <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">mean</span> <span class="err">:</span> <span class="mf">271.580867</span> <span class="nv">ns</span>
</span><span class='line'>    <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">std-deviation</span> <span class="err">:</span> <span class="mf">2.117763</span> <span class="nv">ns</span>
</span><span class='line'>   <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">lower</span> <span class="nv">quantile</span> <span class="err">:</span> <span class="mf">267.593081</span> <span class="kd">ns </span><span class="p">(</span> <span class="mf">2.5</span><span class="nv">%</span><span class="p">)</span>
</span><span class='line'>   <span class="nv">Execution</span> <span class="nb">time </span><span class="nv">upper</span> <span class="nv">quantile</span> <span class="err">:</span> <span class="mf">274.826087</span> <span class="kd">ns </span><span class="p">(</span><span class="mf">97.5</span><span class="nv">%</span><span class="p">)</span>
</span><span class='line'>                   <span class="nv">Overhead</span> <span class="nv">used</span> <span class="err">:</span> <span class="mf">1.166050</span> <span class="nv">ns</span>
</span></code></pre></td></tr></table></div></figure>


<p>Not bad! Ever so slightly slower than the <code>str</code> version but a
performance penalty well worth paying for to get more expressive
string interpolation I feel.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[2014 in Books]]></title>
    <link href="http://blog.wjlr.org.uk/2015/01/06/2014-in-books.html"/>
    <updated>2015-01-06T14:45:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2015/01/06/2014-in-books</id>
    <content type="html"><![CDATA[<p>According to <a href="https://www.goodreads.com/">GoodReads</a> I read 17 books
this year. Here are some I really liked.</p>

<!--more-->


<p><img src="https://d.gr-assets.com/books/1394357588l/19932464.jpg" class="book-cover"></p>

<h2>
  <a href="https://www.goodreads.com/book/show/19932464-kindred">Kindred by Octavia Butler</a>
</h2>


<p>Kindred is classified as sci-fi, but that doesn’t do it justice
really. It’s more of a sci-fi conceit (time-shifting) to allow Butler
to immerse the reader in the world of 1815 on a plantation with
slaves. The protagonist is black and the abrupt time shift lands her
in palpable danger. The relationships between characters was always
nuanced and interesting and the tension was high throughout - tension
from real danger. I especially appreciated the subtle way Butler
weaved in historical names and events into the narrative. I will
definitely be reading more of Octavia Butler’s books.</p>

<p><img src="https://d.gr-assets.com/books/1383652605l/18754367.jpg" class="book-cover"></p>

<h2>
  <a href="https://www.goodreads.com/book/show/18754367-nothing-o-clock">Nothing O&#8217;Clock by Neil Gaiman</a>
</h2>


<p>Part of a series of short stories, one for each Doctor, released on
the 50th anniversary of Doctor Who. This was an undemanding
entertaining read and I intend to read more of them.</p>

<p><img src="https://d.gr-assets.com/books/1328327063l/13127093.jpg" class="book-cover"></p>

<h2>
  <a href="https://www.goodreads.com/book/show/13127093-flat-earth-news">Flat-Earth News by Nick Davies</a>
</h2>


<p>We all know the media are dreadful, but just how dreadful and why?
This book attempts to shine some light on the shady world of British
media (although for the most part the same problems exist elsewhere).
It is somewhat unscrupulous in aiming its critical eye at broadsheet
and tabloid alike and generally castigates broadsheets for pumping out
unchecked nonsense because we expect them to be a cut above the
tabloids (spoiler: they’re not). Shocking, sickening, depressing
- exactly what you’d expect from a book about newspapers.</p>

<p><img src="https://d.gr-assets.com/books/1407785478l/22903174.jpg" class="book-cover"></p>

<h2>
  <a href="https://www.goodreads.com/book/show/22903174-clipping-through">Clipping Through by Leigh Alexander</a>
</h2>


<p>This book by my favourite games journalist - Leigh Alexander - takes
you into the surreal world of games journalism itself. Part stream of
consciousness and part short biography this is a world most of us have
no idea about. It kept me entertained on a few train commutes.
<a href="https://gumroad.com/l/aisCU">You can and should buy it here</a></p>

<p><img src="https://d.gr-assets.com/books/1401632138l/22387085.jpg" class="book-cover"></p>

<h2>
  <a href="https://www.goodreads.com/book/show/22387085-a-natural-history-of-dragons-a-memoir-of-lady-trent">A Natural History of Dragons by Marie Brennan</a>
</h2>


<p>The first part of a series of books about the main character - Lady
Trent. A cross between Pride and Prejudice and Reign of Fire, sort of.
Thoroughly entertaining and I shall be reading the rest of the series.</p>

<p><img src="https://d.gr-assets.com/books/1328302275l/10081041.jpg" class="book-cover"></p>

<h2>
  <a href="https://www.goodreads.com/book/show/10081041-the-long-ships">The Long ships by Frans Bengtsson</a>
</h2>


<p>This Swedish classic about Vikings in the 10th century in Denmark was
a surprisingly good read. The humour was super dry and for some
considerable time it passed me by. My main criticism of it is that
it’s rather overly long and the novelty of the sarcasm wore out well
before the end for me.</p>

<p><img src="https://d.gr-assets.com/books/1351039314l/11272152.jpg" class="book-cover"></p>

<h2>
  <a href="https://www.goodreads.com/book/show/11272152-jane-eyre">Jane Eyre by Charlotte Brontë</a>
</h2>


<p>Quite simply a must read. I particularly enjoyed cross-referencing
with King Lear and the various other literary works Brontë weaves in.</p>

<p><img src="https://d.gr-assets.com/books/1348918036l/2639853.jpg" class="book-cover"></p>

<h2>
  <a href="https://www.goodreads.com/book/show/2639853-to-boulez-and-beyond">To Boulez and Beyond by Joan Peyser</a>
</h2>


<p>The first half of this book is a biography of composers from Debussy
to Boulez, roughly speaking. Lots of time is spent covering
Schoenberg, Berg and Webern as you might expect. The second half of
the book is a biography of Boulez himself. I found the combination
fascinating since it put the musical trends and works of Boulez in
perspective wonderfully. This book lead me to seek out and appreciate
a number of compositions which makes it my favourite of the books I
read in 2014.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Multi-Machine Vagrant Ansible Gotcha]]></title>
    <link href="http://blog.wjlr.org.uk/2014/12/30/multi-machine-vagrant-ansible-gotcha.html"/>
    <updated>2014-12-30T16:00:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2014/12/30/multi-machine-vagrant-ansible-gotcha</id>
    <content type="html"><![CDATA[<p><a href="https://docs.vagrantup.com/" title="Vagrant">Vagrant</a> is a fantastic tool for defining how virtual instances are
to be run and provisioned. I&#8217;ve used Vagrant with <a href="https://docs.chef.io/chef_solo.html" title="Chef-solo">Chef-Solo</a> and
<a href="http://docs.ansible.com/index.html">Ansible</a> provisioners and it&#8217;s helped me understand those tools and
iterate quickly. There are some gotchas however and in this post I
will explore a particular flaw in the way Vagrant and Ansible
cooperate.</p>

<!--more-->


<h2>Multi-machine setup</h2>

<p>Let&#8217;s begin by defining a Vagrant environment that we will play with
(you will need <a href="https://www.virtualbox.org">VirtualBox</a>, Vagrant and Ansible installed):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>mkdir multi-vagrant-ansible
</span><span class='line'><span class="nb">cd </span>multi-vagrant-ansible
</span><span class='line'>vagrant init
</span></code></pre></td></tr></table></div></figure>


<p>This will create a <code>Vagrantfile</code> in the current directory with
commented contents. Let&#8217;s cut it back to the essentials and add in a
URL for the base box (<a href="https://cloud-images.ubuntu.com/vagrant/" title="Ubuntu Cloud Vagrant images">Ubuntu Trusty</a> is the latest LTS
release so that&#8217;s what I&#8217;ll use):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># -*- mode: ruby -*-</span>
</span><span class='line'><span class="c1"># vi: set ft=ruby :</span>
</span><span class='line'>
</span><span class='line'><span class="no">Vagrant</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">config</span><span class="o">|</span>
</span><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">box</span> <span class="o">=</span> <span class="s2">&quot;trusty64&quot;</span>
</span><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">box_url</span> <span class="o">=</span> <span class="s2">&quot;https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box&quot;</span>
</span><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">network</span> <span class="s2">&quot;private_network&quot;</span><span class="p">,</span> <span class="ss">type</span><span class="p">:</span> <span class="s2">&quot;dhcp&quot;</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we run Vagrant now, it&#8217;ll clone that base box (downloading it first
if it hasn&#8217;t already done so) and boot it up. This is already quicker
than downloading an ISO, creating a new VirtualBox instance, booting
that up and going through the installation procedure.</p>

<p>Let&#8217;s define some machines and set them up to be provisioned by
Ansible. We&#8217;ll have two web servers and one load balancer, because
that&#8217;s boringly conventional:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">define</span> <span class="s2">&quot;ariadne&quot;</span> <span class="k">do</span> <span class="o">|</span><span class="n">ariadne</span><span class="o">|</span>
</span><span class='line'>    <span class="n">ariadne</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">provision</span> <span class="s2">&quot;ansible&quot;</span> <span class="k">do</span> <span class="o">|</span><span class="n">ansible</span><span class="o">|</span>
</span><span class='line'>      <span class="n">ansible</span><span class="o">.</span><span class="n">playbook</span> <span class="o">=</span> <span class="s2">&quot;loadbalancer.yml&quot;</span>
</span><span class='line'>      <span class="n">ansible</span><span class="o">.</span><span class="n">sudo</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">define</span> <span class="s2">&quot;minos&quot;</span> <span class="k">do</span> <span class="o">|</span><span class="n">minos</span><span class="o">|</span>
</span><span class='line'>    <span class="n">minos</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">provision</span> <span class="s2">&quot;ansible&quot;</span> <span class="k">do</span> <span class="o">|</span><span class="n">ansible</span><span class="o">|</span>
</span><span class='line'>      <span class="n">ansible</span><span class="o">.</span><span class="n">playbook</span> <span class="o">=</span> <span class="s2">&quot;webserver.yml&quot;</span>
</span><span class='line'>      <span class="n">ansible</span><span class="o">.</span><span class="n">sudo</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">define</span> <span class="s2">&quot;pasiphae&quot;</span> <span class="k">do</span> <span class="o">|</span><span class="n">pasiphae</span><span class="o">|</span>
</span><span class='line'>    <span class="n">pasiphae</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">provision</span> <span class="s2">&quot;ansible&quot;</span> <span class="k">do</span> <span class="o">|</span><span class="n">ansible</span><span class="o">|</span>
</span><span class='line'>      <span class="n">ansible</span><span class="o">.</span><span class="n">playbook</span> <span class="o">=</span> <span class="s2">&quot;webserver.yml&quot;</span>
</span><span class='line'>      <span class="n">ansible</span><span class="o">.</span><span class="n">sudo</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>So in the above, <code>minos</code> and <code>pasiphae</code> are web servers (i.e. they
will be running <a href="http://nginx.org">nginx</a>) and <code>ariadne</code> is the load balancer. The
location of the ansible playbooks are relative to the Vagrantfile so
in the same directory we will create <code>webserver.yml</code> with the
following contents:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="nn">---</span>
</span><span class='line'>
</span><span class='line'><span class="p-Indicator">-</span> <span class="l-Scalar-Plain">hosts</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">all</span>
</span><span class='line'>  <span class="l-Scalar-Plain">tasks</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">apt</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">name=nginx state=present</span>
</span><span class='line'>    <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">service</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">name=nginx state=started</span>
</span></code></pre></td></tr></table></div></figure>


<p>Which ensures that nginx is not only installed but also running (it&#8217;ll
also mean that if that server is rebooted, it&#8217;ll still run nginx).</p>

<p>Now for the load balancer, <code>loadbalancer.yml</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="nn">---</span>
</span><span class='line'>
</span><span class='line'><span class="p-Indicator">-</span> <span class="l-Scalar-Plain">hosts</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">all</span>
</span><span class='line'>  <span class="l-Scalar-Plain">tasks</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">apt</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">name=haproxy state=present</span>
</span><span class='line'>    <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">service</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">name=haproxy state=started</span>
</span></code></pre></td></tr></table></div></figure>


<p>Which ensures <a href="http://www.haproxy.org">haproxy</a> is installed and running in the same way.</p>

<p>These two playbooks are not aware of each other, they act
independently and you could use <code>ansible-playbook</code> to provision any
server you liked with them.</p>

<p>If you run <code>vagrant up</code> at this point (assuming you&#8217;ve not done that
with this Vagrantfile before), it&#8217;ll boot up new VirtualBox instances
and provision them with ansible, installing the necessary software
etc. All well and good so far.</p>

<h2>Ansible facts</h2>

<p>Ansible starts off by collecting facts about the nodes it&#8217;ll run on.
It does this so that you can use information about the node in your
playbooks, roles and tasks.</p>

<p>To see the kind of facts that ansible collects about a node, you can
run ansible&#8217;s <code>setup</code> module like this (for the minos instance):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>ansible -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory -m setup -u vagrant --private-key<span class="o">=</span>.vagrant/machines/minos/virtualbox/private_key minos
</span></code></pre></td></tr></table></div></figure>


<p>The above command should print out a large JSON structure of all the
facts ansible has collected about that node. Ansible facts are
somewhat extensible so it can include information gathered using the
<a href="https://github.com/opscode/ohai">Ohai</a> or <a href="https://github.com/puppetlabs/facter">Facter</a> tools.</p>

<p>The facts relevant to our example are under the <code>ansible_eth1</code>
key and they include an IPv4 address - which will come in handy in a
moment.</p>

<h2>Facts &amp; templates</h2>

<p>Now let&#8217;s create a <a href="http://docs.ansible.com/template_module.html">template</a> for the haproxy configuration (in
<code>templates/haproxy.cfg.j2</code>):</p>

<figure class='code'><figcaption><span>Haproxy config  (haproxy.cfg.j2)</span> <a href='http://blog.wjlr.org.uk/downloads/code/haproxy.cfg.j2'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='jinja'><span class='line'><span class="x">backend web-backend</span>
</span><span class='line'><span class="x">  balance roundrobin</span>
</span><span class='line'><span class="x">  mode http</span>
</span><span class='line'><span class="x">  </span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">host</span> <span class="k">in</span> <span class="nv">groups</span><span class="o">[</span><span class="s1">&#39;webservers&#39;</span><span class="o">]</span> <span class="cp">%}</span><span class="x"></span>
</span><span class='line'><span class="x">     server </span><span class="cp">{{</span> <span class="nv">host</span> <span class="cp">}}</span><span class="x"> </span><span class="cp">{{</span> <span class="nv">hostvars</span><span class="o">[</span><span class="nv">host</span><span class="o">]</span><span class="nv">.ansible_eth1.ipv4.address</span> <span class="cp">}}</span><span class="x">:80 check</span>
</span><span class='line'><span class="x">  </span><span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x"></span>
</span></code></pre></td></tr></table></div></figure>


<p>We&#8217;ll also need to ensure that template gets used in the loadbalancer
playbook:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="nn">---</span>
</span><span class='line'>
</span><span class='line'><span class="p-Indicator">-</span> <span class="l-Scalar-Plain">hosts</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">all</span>
</span><span class='line'>  <span class="l-Scalar-Plain">tasks</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">apt</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">name=haproxy state=present</span>
</span><span class='line'>    <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">service</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">name=haproxy state=started</span>
</span><span class='line'>    <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">name</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">Configure haproxy</span>
</span><span class='line'>      <span class="l-Scalar-Plain">template</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">src=templates/haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we run this now, we&#8217;ll get a cryptic error:</p>

<p><code>fatal: [ariadne] =&gt; {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'dict object' has no attribute 'webservers'", 'failed': True}</code></p>

<p>One possible reason for this is that we haven&#8217;t defined any groups for
our vagrant instances, let&#8217;s do that now. We&#8217;ll start by defining the
groups at the top of the Vagrantfile, before anything else (but after
the emacs/vi mode comments):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">groups</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>  <span class="s2">&quot;webservers&quot;</span> <span class="o">=&gt;</span> <span class="o">[</span><span class="s2">&quot;minos&quot;</span><span class="p">,</span> <span class="s2">&quot;pasiphae&quot;</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;loadbalancers&quot;</span> <span class="o">=&gt;</span> <span class="o">[</span><span class="s2">&quot;ariadne&quot;</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'>  <span class="s2">&quot;all_groups:children&quot;</span> <span class="o">=&gt;</span> <span class="o">[</span><span class="s2">&quot;webservers&quot;</span><span class="p">,</span> <span class="s2">&quot;loadbalancers&quot;</span><span class="o">]</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This correlates to the playbooks we&#8217;ve assigned for each node in the
Vagrantfile. Then we need to refer to that variable in each of our
machine definitions, adding a line that says <code>ansible.groups =
groups</code>, so the modified ariadne definition should now be:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'>  <span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">define</span> <span class="s2">&quot;ariadne&quot;</span> <span class="k">do</span> <span class="o">|</span><span class="n">ariadne</span><span class="o">|</span>
</span><span class='line'>    <span class="n">ariadne</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">provision</span> <span class="s2">&quot;ansible&quot;</span> <span class="k">do</span> <span class="o">|</span><span class="n">ansible</span><span class="o">|</span>
</span><span class='line'>      <span class="n">ansible</span><span class="o">.</span><span class="n">playbook</span> <span class="o">=</span> <span class="s2">&quot;loadbalancer.yml&quot;</span>
</span><span class='line'>      <span class="n">ansible</span><span class="o">.</span><span class="n">sudo</span> <span class="o">=</span> <span class="kp">true</span>
</span><span class='line'>      <span class="n">ansible</span><span class="o">.</span><span class="n">groups</span> <span class="o">=</span> <span class="n">groups</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we run <code>vagrant provision</code> now we get a different error! Ah Ha!
Progress:</p>

<p><code>fatal: [ariadne] =&gt; {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'dict object' has no attribute 'ansible_eth1'", 'failed': True}</code></p>

<h2>Oh no!</h2>

<p>It would be useful at this point to examine what we <em>do</em> have in that
dictionary object. Maybe I mistyped the key? In order to do that, we
can add a debug line above the haproxy configuration line in the
<code>loadbalancer.yml</code> file, like this: <code>- debug: var=hostvars['minos']</code>.</p>

<p>When we run <code>vagrant provision</code> now, we will get the facts about minos
printed in JSON to the console. It&#8217;ll look something like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="nt">&quot;hostvars[&#39;minos&#39;]&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class='line'>    <span class="nt">&quot;inventory_hostname_short&quot;</span><span class="p">:</span> <span class="s2">&quot;minos&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nt">&quot;inventory_hostname&quot;</span><span class="p">:</span> <span class="s2">&quot;minos&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nt">&quot;group_names&quot;</span><span class="p">:</span> <span class="p">[</span>
</span><span class='line'>      <span class="s2">&quot;all_groups&quot;</span><span class="p">,</span>
</span><span class='line'>      <span class="s2">&quot;webservers&quot;</span>
</span><span class='line'>    <span class="p">],</span>
</span><span class='line'>    <span class="nt">&quot;ansible_ssh_port&quot;</span><span class="p">:</span> <span class="mi">2200</span><span class="p">,</span>
</span><span class='line'>    <span class="nt">&quot;ansible_ssh_host&quot;</span><span class="p">:</span> <span class="s2">&quot;127.0.0.1&quot;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Clearly all those facts gathered aren&#8217;t here. Why? The reason for this
is that Vagrant runs provisioning separately on each virtual machine -
so each ansible run is not aware of anything from another ansible
run. If you look this up online, you will find apparent answers to
this problem that reconfigure vagrant to connect to all hosts when
doing an ansible run. Let&#8217;s do that now.</p>

<p>For each ansible block in the Vagrantfile, add the line:
<code>ansible.limit = 'all'</code>. Let&#8217;s try <code>vagrant provision</code> again now
that&#8217;s in place.</p>

<p>The error that I get after making this change is that SSH is failing.
If we add <code>ansible.verbose = 'vvvv'</code> to each ansible block in the
Vagrantfile then with a lot of scrolling around we can deduce that
ansible is attempting to connect to each machine in the inventory
using the same private key as it would for the machine it&#8217;s currently
provisioning. In other words, while provisioning <code>ariadne</code>, it uses
the <code>ariadne</code> private SSH key to log on to both the other servers.
This won&#8217;t work of course because those SSH keys are generated by
Vagrant per machine. Not only that but the private key is on the host
machine, not on the guests so it&#8217;s a fools errand.</p>

<p>I&#8217;m not sure what kind of SSH key setup would allow <code>ansible.limit =
'all'</code> to work at all, but it&#8217;s hardly straightforward.</p>

<h2>Potential workaround: Redis</h2>

<p>The only way I&#8217;ve discovered to have ansible and Vagrant work well
together is to use <a href="http://docs.ansible.com/playbooks_variables.html#fact-caching">Fact Caching</a>. This allows ansible to cache all
facts from a node in Redis (or memcached) so that nodes can refer to
each other without requiring an extra ssh connection for every node.</p>

<p>In order to enable fact caching, you will need Redis installed and
running. Then create an <code>ansible.cfg</code> file in the same directory as
your Vagrantfile, with the following contents:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='cfg'><span class='line'><span class="k">[defaults]</span>
</span><span class='line'><span class="na">fact_caching</span> <span class="o">=</span> <span class="s">redis</span>
</span><span class='line'><span class="na">fact_caching_timeout</span> <span class="o">=</span> <span class="s">86400</span>
</span></code></pre></td></tr></table></div></figure>


<p>You will need to provision <code>minos</code> and <code>pasiphae</code> first so that their
facts are stored in Redis before provisioning <code>ariadne</code> (because it
refers to those other nodes):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>vagrant provision minos
</span><span class='line'>vagrant provision pasiphae
</span></code></pre></td></tr></table></div></figure>


<p>Now that those facts have been gathered, we can run <code>vagrant
provision</code> and it should complete without trouble this time.</p>

<p>Now to verify that the haproxy config has been written as we expect,
we can run <code>vagrant ssh ariadne -- cat /etc/haproxy/haproxy.cfg</code> and
get something akin to:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='text'><span class='line'>backend web-backend
</span><span class='line'>  balance roundrobin
</span><span class='line'>  mode http
</span><span class='line'>    server minos 172.28.128.4:80 check
</span><span class='line'>    server pasiphae 172.28.128.5:80 check
</span></code></pre></td></tr></table></div></figure>


<p>It worked! So although fact caching is intended for use in large
organisations with thousands of nodes (possibly in disparate data
centres) to speed up deployment, it can be handy working around
weaknesses in the vagrant+ansible combination.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Secret Santa in core.logic]]></title>
    <link href="http://blog.wjlr.org.uk/2014/12/28/secret-santa-logic.html"/>
    <updated>2014-12-28T19:04:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2014/12/28/secret-santa-logic</id>
    <content type="html"><![CDATA[<p>Logic programming is an odd beast. As I lounged around over the
holidays I tried to work out how to write a Secret Santa algorithm
using core.logic. It seemed like the obvious choice but I quickly
realised I&#8217;d bitten off more than I could chew.</p>

<!--more-->


<h2>Simple Secret Santa</h2>

<p>Your common or garden Secret Santa consists of putting names in a hat
and picking them out again, ensuring you don&#8217;t pick yourself. Suitable
for offices and other groups of awkward strangers and acquaintances.</p>

<p>We need <code>core.logic</code> imported, so here&#8217;s the namespace declaration
(note that both <code>clojure.core</code> and <code>clojure.core.logic</code> define <code>==</code>
for totally different purposes. I have never used <code>clojure.core/==</code> so
I&#8217;m a bit hazy as to what it&#8217;s for - we exclude it so they don&#8217;t clash):</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">ns </span><span class="nv">secret-santa.core</span>
</span><span class='line'>  <span class="p">(</span><span class="ss">:refer-clojure</span> <span class="ss">:exclude</span> <span class="p">[</span><span class="nv">==</span><span class="p">])</span>
</span><span class='line'>  <span class="p">(</span><span class="ss">:require</span> <span class="p">[</span><span class="nv">clojure.core.logic</span> <span class="ss">:refer</span> <span class="ss">:all</span><span class="p">]</span>
</span><span class='line'>            <span class="p">[</span><span class="nv">clojure.core.logic.pldb</span> <span class="ss">:refer</span> <span class="ss">:all</span><span class="p">]))</span>
</span></code></pre></td></tr></table></div></figure>


<p>We&#8217;ll start by defining a relation:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">db-rel</span> <span class="nv">santa</span> <span class="nv">p</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is so that we can constrain values to only those names we&#8217;ve
defined are santas (otherwise you get weird logic variables and nobody
wants to deal with that noise).</p>

<p>Now for the Secret Santa function. It takes a list of friend names
(not actually a requirement that they are friends, they may not be
after exchanging presents) and returns a list of lists. Each inner
list is a pair of giver and receiver of presents.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">simple-secret-santa</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">friends</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">facts</span> <span class="p">(</span><span class="nf">coll-&gt;db</span> <span class="p">(</span><span class="nb">for </span><span class="p">[</span><span class="nv">friend</span> <span class="nv">friends</span><span class="p">]</span>
</span><span class='line'>                           <span class="p">[</span><span class="nv">santa</span> <span class="nv">friend</span><span class="p">]))</span>
</span><span class='line'>        <span class="nv">num</span> <span class="p">(</span><span class="nb">count </span><span class="nv">friends</span><span class="p">)</span>
</span><span class='line'>        <span class="nv">givers</span> <span class="p">(</span><span class="nf">repeatedly</span> <span class="nv">num</span> <span class="nv">lvar</span><span class="p">)</span>
</span><span class='line'>        <span class="nv">receivers</span> <span class="p">(</span><span class="nf">repeatedly</span> <span class="nv">num</span> <span class="nv">lvar</span><span class="p">)]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">-&gt;&gt;</span> <span class="p">(</span><span class="nf">with-db</span> <span class="nv">facts</span>
</span><span class='line'>           <span class="p">(</span><span class="nf">run*</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span>
</span><span class='line'>             <span class="p">(</span><span class="nf">everyg</span> <span class="nv">santa</span> <span class="nv">givers</span><span class="p">)</span>
</span><span class='line'>             <span class="p">(</span><span class="nf">everyg</span> <span class="nv">santa</span> <span class="nv">receivers</span><span class="p">)</span>
</span><span class='line'>             <span class="p">(</span><span class="nf">distincto</span> <span class="nv">givers</span><span class="p">)</span>
</span><span class='line'>             <span class="p">(</span><span class="nf">distincto</span> <span class="nv">receivers</span><span class="p">)</span>
</span><span class='line'>             <span class="p">(</span><span class="nf">pairupo</span> <span class="nv">givers</span> <span class="nv">receivers</span> <span class="nv">q</span><span class="p">)))</span>
</span><span class='line'>      <span class="p">(</span><span class="nb">map </span><span class="nv">sort</span><span class="p">)</span>
</span><span class='line'>      <span class="nv">distinct</span>
</span><span class='line'>      <span class="nv">rand-nth</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we call it thus:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">simple-secret-santa</span> <span class="p">[</span><span class="s">&quot;Tommen&quot;</span> <span class="s">&quot;Gregor&quot;</span> <span class="s">&quot;Daenerys&quot;</span> <span class="s">&quot;Arya&quot;</span><span class="p">])</span>
</span></code></pre></td></tr></table></div></figure>


<p>Then we should get the following as a result:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">([</span><span class="s">&quot;Arya&quot;</span> <span class="s">&quot;Gregor&quot;</span><span class="p">]</span>
</span><span class='line'> <span class="p">[</span><span class="s">&quot;Daenerys&quot;</span> <span class="s">&quot;Tommen&quot;</span><span class="p">]</span>
</span><span class='line'> <span class="p">[</span><span class="s">&quot;Gregor&quot;</span> <span class="s">&quot;Daenerys&quot;</span><span class="p">]</span>
</span><span class='line'> <span class="p">[</span><span class="s">&quot;Tommen&quot;</span> <span class="s">&quot;Arya&quot;</span><span class="p">])</span>
</span></code></pre></td></tr></table></div></figure>


<p>Please note my chronic lack of imagination evidenced by my lazily
using characters from Game of Thrones who would likely not be involved
in this kind of tomfoolery.</p>

<p>We need a helper function for pairing people up now:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">defne</span> <span class="nv">pairupo</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">givers</span> <span class="nv">receivers</span> <span class="nv">pairs</span><span class="p">]</span>
</span><span class='line'>  <span class="p">([()</span> <span class="p">()</span> <span class="p">()])</span>
</span><span class='line'>  <span class="p">([[</span><span class="nv">g</span> <span class="k">. </span><span class="nv">gs</span><span class="p">]</span> <span class="p">[</span><span class="nv">r</span> <span class="k">. </span><span class="nv">rs</span><span class="p">]</span> <span class="p">[</span><span class="nv">p</span> <span class="k">. </span><span class="nv">ps</span><span class="p">]]</span>
</span><span class='line'>   <span class="p">(</span><span class="nf">!=</span> <span class="nv">g</span> <span class="nv">r</span><span class="p">)</span>
</span><span class='line'>   <span class="p">(</span><span class="nb">== </span><span class="nv">p</span> <span class="p">[</span><span class="nv">g</span> <span class="nv">r</span><span class="p">])</span>
</span><span class='line'>   <span class="p">(</span><span class="nf">pairupo</span> <span class="nv">gs</span> <span class="nv">rs</span> <span class="nv">ps</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The mind-bending nature of that function can be rather confusing. In
core.logic (as with <a href="http://minikanren.org">miniKanren</a> and others),
it&#8217;s common for an &#8220;output
value&#8221; to be one of the parameters, in this case the 3rd parameter
(<code>pairs</code>). This function defines a relationship between the parameters
provided so you can call it with <code>givers</code> and <code>pairs</code> and it&#8217;ll fill
in the blanks for the <code>receivers</code> value (effectively unzipping the
<code>pairs</code> value), which is pretty neat.</p>

<h3>Unify what?</h3>

<p>It&#8217;s crucial to understand the <code>clojure.core.logic/==</code> unification
function to understand how any of this works. It looks like an
equality test from one of those other programming languages we shall
not mention here, but it isn&#8217;t like that. Sometimes I thought of it as
a kind of wishful-thinking-equality - a sort of &#8220;wouldn&#8217;t it be just
lovely if these things were the same&#8221;.</p>

<p>Taking an example from
<a href="https://github.com/clojure/core.logic/wiki/A-Core.logic-Primer">the core.logic wiki</a>:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">run*</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">fresh</span> <span class="p">[</span><span class="nv">a</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">membero</span> <span class="nv">a</span> <span class="p">[</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">])</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">membero</span> <span class="nv">q</span> <span class="p">[</span><span class="mi">3</span> <span class="mi">4</span> <span class="mi">5</span><span class="p">])</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">== </span><span class="nv">a</span> <span class="nv">q</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>In the above we are asking core.logic for the possible values of <code>q</code>
(the output variable). We start using a new logic variable <code>a</code> which
we unify with <code>q</code> using the <code>==</code> unification function. That is to say,
all solutions must satisfy the equation <code>a = q</code>, they must have the
same value. Core.logic tends to add &#8216;o&#8217; to the end of common function
names so <code>(membero a [1 2 3])</code> isn&#8217;t a boolean predicate function for
testing for list membership, it attempts to make that statement true.
With simply that statement - <code>a</code> can be 1 or 2 or 3. The next line
asserts that <code>q</code> is a member of the list <code>[3 4 5]</code> so its value can be
3 or 4 or 5. The last line unifies <code>a</code> and <code>q</code> so they must have the
same value. We can tell that the only cross-over between the 2 lists
already mentioned is the value 3, so <code>q</code> can only be 3. <code>run*</code> returns
a list of all possible solutions so in fact we will get a single
element list: <code>(3)</code> as the result from that code.</p>

<p>The input of our Secret Santa function will be a list of names
(strings), so it&#8217;d be useful to turn that list of names into a
database that we can constrain our logic functions on:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">coll-&gt;db</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">coll</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">apply </span><span class="nv">db</span> <span class="nv">coll</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p><a href="http://crossclj.info/fun/clojure.core.logic.pldb/db.html"><code>clojure.core.logic.pldb/db</code></a> takes a variable number of parameters and
returns a database with those facts contained. We want to call it with
a collection so <code>apply</code> comes in handy here. Effectively you can use
<code>apply</code> to turn this: <code>(apply somefn [arg1 arg2 arg3])</code> into <code>(somefn
arg1 arg2 arg3)</code></p>

<h3>How does it work again?</h3>

<p>Let&#8217;s concentrate on the core part of the function:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">with-db</span> <span class="nv">facts</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">run*</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">everyg</span> <span class="nv">santa</span> <span class="nv">givers</span><span class="p">)</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">everyg</span> <span class="nv">santa</span> <span class="nv">receivers</span><span class="p">)</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">distincto</span> <span class="nv">givers</span><span class="p">)</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">distincto</span> <span class="nv">receivers</span><span class="p">)</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">pairupo</span> <span class="nv">givers</span> <span class="nv">receivers</span> <span class="nv">q</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>We are asking for all the solutions where every item in the <code>givers</code>
collection is a santa and every item in the <code>receivers</code> collection is
a santa (lines 3 and 4). This ensures that those collections contain
only valid santa names and not logic variables or numbers or anything
else. There&#8217;s something subtle about this - it&#8217;s not simply an
assertion. core.logic will make those statements true in every way
they can. One possible version of that (assuming there were 4 names
input to the function) is that <code>givers =
["Arya" "Arya" "Arya" "Arya"]</code>. That&#8217;s nonsensical for our purposes
(remember we mean to zip up these collections so the first giver is
giving to the first receiver, the 2nd giver to the 2nd receiver and so
on). So then we ensure that the collection of givers has distinct
elements in it (line 5), saying the same about receivers on line 6. If
you imagine core.logic has assembled all the combinations of values
that are santas first - imagine now it throws away any where values
repeat. One of the remaining values for <code>givers</code> will be
[&#8220;Arya&#8221; &#8220;Gregor&#8221; &#8220;Tommen&#8221; &#8220;Daenerys&#8221;]. There will be more. How many?</p>

<h3>Derangements, subfactorials, oh my!</h3>

<p>The mathematical name for the number of unique permutations of
elements in a set (that are different from their original arrangement)
is the number of
<a href="https://en.wikipedia.org/wiki/Derangement">derangements</a> or the subfactorial.</p>

<p>Roughly speaking, Secret Santa is a special case of finding a
derangement of names. When you have 4 names, there are 9 derangements.</p>

<p>Here is some clojure that calculates that:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">derangements</span>
</span><span class='line'>  <span class="s">&quot;Calculate the number of derangements of n elements.&quot;</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">n</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">condp</span> <span class="nb">= </span><span class="nv">n</span>
</span><span class='line'>    <span class="mi">0</span> <span class="mi">1</span>
</span><span class='line'>    <span class="mi">1</span> <span class="mi">0</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">* </span><span class="p">(</span><span class="nb">dec </span><span class="nv">n</span><span class="p">)</span>
</span><span class='line'>       <span class="p">(</span><span class="nb">+ </span><span class="p">(</span><span class="nf">derangements</span> <span class="p">(</span><span class="nb">dec </span><span class="nv">n</span><span class="p">))</span>
</span><span class='line'>          <span class="p">(</span><span class="nf">derangements</span> <span class="p">(</span><span class="nb">- </span><span class="nv">n</span> <span class="mi">2</span><span class="p">))))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>This algorithm is primarily useful for checking your results in unit
tests for example. Another way to use it is as the parameter to
<code>run</code> - you can ask for all the possible derangements of friends. I
didn&#8217;t have much luck with that because I misunderstood the values
that my logic code was producing, also there is no need to know
beforehand how many combinations are possible since you only want one.</p>

<h2>Further improvements</h2>

<p>One weakness of the solutions described above is that they model the
relationship between the gift giver and receiver as a binary - allowed
or not. It would be useful to be able to first attempt to solve the
problem under ideal conditions before falling back to less and less
ideal solutions. For example, it&#8217;s not ideal to have people paired up
symmetrically - it&#8217;s just a bit boring that way. It&#8217;s more optimal to
assign Santas in a circle, which makes it slightly more difficult to
identify who&#8217;s assigned who.</p>

<p>Finding all possible solutions is extremely slow in core.logic (at
least, the way I&#8217;ve written it) so this could do with a fair amount of
optimisation.</p>

<p>I punted the problem of picking a solution out of possible solutions
to the combination of <code>(map sort)</code>, <code>distinct</code> and <code>rand-nth</code>. This
isn&#8217;t really necessary, I could have told core.logic what constitutes
a distinct solution (it doesn&#8217;t realise that order of pairings doesn&#8217;t
matter) and then simply picked one. My brain hurt so much by that
point that I decided to call it a day and move on to more interesting
problems, like</p>

<h2>Final thoughts</h2>

<p>I had a great deal of difficulty writing the <code>pairupo</code> function,
largely because all the various <code>defne</code>/<code>defnu</code>/<code>defna</code> confused me -
I still couldn&#8217;t tell you what they do. This was partly due to me
moving from a real problem (Secret Santa) to the implementation in
core.logic on the basis of sketchy logic programming knowledge, so I
missed a lot of subtlety related to <code>conde</code> which is crucial for
understanding this stuff. The official documentaion for core.logic is
extremely sparse also, you are very much on your own if the problem
you want to solve isn&#8217;t Sudoku or the Typed Lambda Calculus (it
boggles my mind that that is on the
<a href="https://github.com/clojure/core.logic/wiki/Examples">Examples</a>
wiki page, I&#8217;m not sure if the intention is to educate or
obfuscate there).</p>

<p>I hope that a bit more blogging from mere mortals like myself might
help others grok this mind-mending area of programming.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Ludum Dare 31 Postmortem]]></title>
    <link href="http://blog.wjlr.org.uk/2014/12/08/ludum-dare-31-postmortem.html"/>
    <updated>2014-12-08T20:01:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2014/12/08/ludum-dare-31-postmortem</id>
    <content type="html"><![CDATA[<p>On the weekend of 6th December 2014, I participated in <a href="http://ludumdare.com/compo/">Ludum Dare</a> 31. The theme was &#8220;Entire Game on One Screen&#8221;. I didn&#8217;t finish my game and didn&#8217;t hand anything in. This is the 3rd(?) time I&#8217;ve started but not finished a Ludum Dare game in recent years. The first time I participated in Ludum Dare, I completed a game and handed it in for the competition (48 hours), so I&#8217;m curious as to what has been holding me back since then.</p>

<!--more-->


<p><img src="http://blog.wjlr.org.uk/images/robot-scene.png" class="extrawide pixel-art"></p>

<h2>Lack of planning</h2>

<p>It was only after Ludum Dare was completed and I filled in a <a href="http://trello.com">Trello</a> board with the tasks necessary to complete my game that I realised the immensity of the project. I was reading <a href="https://twitter.com/amyhoy">Amy Hoy&#8217;s</a> new book <a href="https://unicornfree.com/just-fucking-ship">Just Fucking Ship</a> and it emphasises working backwards from a goal to a set of tasks that need to be done. Hoy also advocates <a href="http://en.wikipedia.org/wiki/Mise_en_place">Mise en place</a> style of working, which I&#8217;ve never identified as a <em>thing</em> before but it&#8217;s obvious in hindsight. Ludum Dare advice usually contains something about starting on paper, getting all the high-level details worked out before diving into code and that&#8217;s absolutely right. The other massive advantage of having a todo list (e.g. in Trello) is that it&#8217;s super easy to order the list of things in terms of what needs to be done and what is optional. Does it matter that the resolution is fixed at 800x600? Nope. But I&#8217;ve totally sunk hours/days into coding around the idea I need to have a scalable interface on the web for my games (and it&#8217;s always really painful).</p>

<p><img src="http://blog.wjlr.org.uk/images/Elm_Editor_Mario.png" class="extrawide"></p>

<p>Case in point - this Ludum Dare, I decided to use <a href="http://elm-lang.org">Elm</a>, which was entirely new to me. The conventional wisdom has it that one should never learn tools while participating in Ludum Dare, just use whatever you have at hand (i.e. programming languages/IDEs/libraries etc. that you have used extensively before). I only make games during Ludum Dare though and I&#8217;ve never reached a level of proficiency with gamedev that I feel leaves me with a default technology choice, so I used a new thing. However, this time round, using Elm, the web game ends up getting drawn using the 2D canvas and image elements positioned on top of it basically. You can&#8217;t control a lot of how Elm does any of this and there was a minor wrinkle - there&#8217;s no way to use CSS&#8217;s <a href="https://developer.mozilla.org/en/docs/Web/CSS/image-rendering">image-rendering</a> option to optimise for pixel art (crisp-edges is what you want - which is essentially nearest-neighbour scaling up/down). The result is you get blurry pixel art in your games and this sucks. It&#8217;s not at all a blocking problem for Ludum Dare games though and I feel silly for spending any more than 5 minutes on it.</p>

<h2>Overly ambitious game design</h2>

<p>I&#8217;m not a regular gamedev, this is something I do very occasionally. I&#8217;m a software engineer and therefore I completely and wildly underestimate the complexity and difficulty of programming tasks. Not to mention my total lack of ability to gauge time requirements. In the world of work, many of us software engineers avoid the near-impossible task of time estimation by using Agile-based practice and estimating complexity instead. It&#8217;s entirely based on teamwork and iteration of process and makes no sense for an individual working towards a goal on a deadline.</p>

<p>Aside: within the confines of Agile processes and teams, individuals can and do use more &#8220;traditional&#8221; time estimation and tracking tools to live up to their own expectations of themselves, so we do think &#8220;this will probably take half a day&#8221; but the discipline of working in an Agile team is never externalising that because it becomes an expectation and a commitment.</p>

<p>All my weaknesses as a software engineer aside, I made the fundamental error of dreaming up a cool game that was not within my reach of a 48 hour deadline. This is a completely avoidable self-imposed problem. The solution seems to be, sketch out the game in its entirety - all the objects in the game, all the components and all the behaviour. Make sure you have all the artwork at least approximated (I did this in Paper on the iPad because I could doddle on that while watching TV and it didn&#8217;t even feel like work). At this point you can identify elements that can be left out - think &#8220;what could I postpone if I was forced to launch this in an hour?&#8221; - or some other mind trick to force yourself to be ruthless with the scope of your game. One advantage of sketching everything on paper or in Paper on an iPad is that if time gets really tight and you don&#8217;t have super great pixel artwork ready - just chop up/scan in/photograph the sketches and use them as your assets. There will be people playing your game who think the hand-drawn nature of the artwork was a stylistic choice you made from the beginning and you know you ran out of time.</p>

<p><img src="http://blog.wjlr.org.uk/images/sketching-onescreen.png" class="extrawide"></p>

<h2>Onwards</h2>

<p>So now I have a Trello board that makes it embarrassingly clear how unrealistic my game was for a 48 hour competition - where do I go from here? I think the best answer to that is to carry on with it, attack each task in the list and then realise the game is done (that sounds obvious but it&#8217;s impossible to realise when you&#8217;re ready to ship something unless you define up-front what you actually want to ship).</p>

<p>In Agile circles, we talk a bit about what it means to be Done. The &#8220;definition of done&#8221;, which can sound rather alien to somebody who&#8217;s not familiar with specifying that concretely. It&#8217;s essential to working progress otherwise you end up with tasks that linger on for days and weeks because they are quite simply never finished. Those aren&#8217;t tasks, they&#8217;re a waking nightmare. Sometimes there is no definition of done. This blog post doesn&#8217;t have a definition of done because I made an agreement with myself that I would write until I felt hungry then I would order some food. In Agile that&#8217;s usually called time-boxing and it usually isn&#8217;t hunger-based but might as well be. &#8220;Let&#8217;s time-box this for an hour&#8221; is a common phrase in Agile teams. It&#8217;s how you manage investigation tasks (otherwise they go on forever). We all need more, clearer definitions of done.</p>

<p><img src="http://blog.wjlr.org.uk/images/hunger-box.png" class="extrawide"></p>

<p>So Ludum Dare is over. I&#8217;m done with this blog post. I have plenty to practice and improve before next Ludum Dare and with any luck I&#8217;ll ship a game next time.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[JSON for Humans]]></title>
    <link href="http://blog.wjlr.org.uk/2014/03/27/json-for-humans.html"/>
    <updated>2014-03-27T12:58:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2014/03/27/json-for-humans</id>
    <content type="html"><![CDATA[<p>JSON is a fairly easy to read format but sometimes you need to deal with an
unreadable dump of the stuff and it&#8217;d be useful to quickly reformat or prettify
it. There are a number of ways to do this.</p>

<!--more-->


<h2>Javascript (Chrome, Firefox etc.)</h2>

<p>Javascript has the
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#space_argument">JSON.stringify</a>
function, which can pretty-print JSON according to the number of spaces you
pass as the 3rd parameter. For example:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">example</span> <span class="o">=</span> <span class="p">{</span><span class="nx">user</span><span class="o">:</span> <span class="p">{</span><span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;Ada Lovelace&quot;</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s2">&quot;ada@example.com&quot;</span><span class="p">},</span> <span class="nx">group</span><span class="o">:</span> <span class="s2">&quot;programmers&quot;</span><span class="p">}</span>
</span><span class='line'><span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">example</span><span class="p">,</span> <span class="kc">undefined</span><span class="p">,</span> <span class="mi">4</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you try the above in a console in a web browser (or a Node.js console), you
should see this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>{
</span><span class='line'>    "user": {
</span><span class='line'>        "name": "Ada Lovelace",
</span><span class='line'>        "email": "ada@example.com"
</span><span class='line'>    },
</span><span class='line'>    "group": "programmers"
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>


<h2>json_reformat</h2>

<p>That&#8217;s all well and good, but relying on a javascript console is not the most
efficient way to do things, so an easier and more scritable tool is the
json_reformat command (part of the <a href="http://lloyd.github.io/yajl/">Yajl</a>).
To get this tool installed, do the following:</p>

<ul>
<li><code>brew install yajl</code> - OS X</li>
<li><code>apt-get install yajl-tools</code> - Debian/Ubuntu Linux</li>
</ul>


<p>Now you can just pipe JSON text to <code>json_reformat</code> and it will pretty-print
it to standard output. This makes it very useful for use in other tools, such as
vim. Here is some vim config for using <code>json_reformat</code>:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='vim'><span class='line'><span class="k">au</span> <span class="nb">BufRead</span><span class="p">,</span><span class="nb">BufNewFile</span> *.json <span class="k">set</span> <span class="k">filetype</span><span class="p">=</span>json
</span><span class='line'><span class="k">au</span> <span class="nb">FileType</span> json <span class="k">setlocal</span> <span class="nb">equalprg</span><span class="p">=</span>json_reformat
</span></code></pre></td></tr></table></div></figure>


<p>Now let&#8217;s edit a file that was generated by <a href="http://berkshelf.com">Berkshelf</a>
and has been spat out on the filesystem in <code>~/.berkshelf/config.json</code>. Initially
it looks like this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'><span class="p">{</span><span class="nt">&quot;chef&quot;</span><span class="p">:{</span><span class="nt">&quot;chef_server_url&quot;</span><span class="p">:</span><span class="s2">&quot;https://api.opscode.com/organizations/acmecorp&quot;</span><span class="p">,</span><span class="nt">&quot;validation_client_name&quot;</span><span class="p">:</span><span class="s2">&quot;acmecorp-validator&quot;</span><span class="p">,</span><span class="nt">&quot;validation_key_path&quot;</span><span class="p">:</span><span class="s2">&quot;/Users/ada/Work/.chef/acmecorp-validator.pem&quot;</span><span class="p">,</span><span class="nt">&quot;client_key&quot;</span><span class="p">:</span><span class="s2">&quot;/Users/ada/Work/.chef/ada.pem&quot;</span><span class="p">,</span><span class="nt">&quot;node_name&quot;</span><span class="p">:</span><span class="s2">&quot;ada&quot;</span><span class="p">},</span><span class="nt">&quot;cookbook&quot;</span><span class="p">:{</span><span class="nt">&quot;copyright&quot;</span><span class="p">:</span><span class="s2">&quot;Ada Lovelace&quot;</span><span class="p">,</span><span class="nt">&quot;email&quot;</span><span class="p">:</span><span class="s2">&quot;ada@example.com&quot;</span><span class="p">,</span><span class="nt">&quot;license&quot;</span><span class="p">:</span><span class="s2">&quot;MIT&quot;</span><span class="p">},</span><span class="nt">&quot;allowed_licenses&quot;</span><span class="p">:[],</span><span class="nt">&quot;raise_license_exception&quot;</span><span class="p">:</span><span class="kc">false</span><span class="p">,</span><span class="nt">&quot;vagrant&quot;</span><span class="p">:{</span><span class="nt">&quot;vm&quot;</span><span class="p">:{</span><span class="nt">&quot;box&quot;</span><span class="p">:</span><span class="s2">&quot;Berkshelf-CentOS-6.3-x86_64-minimal&quot;</span><span class="p">,</span><span class="nt">&quot;box_url&quot;</span><span class="p">:</span><span class="s2">&quot;https://dl.dropbox.com/u/31081437/Berkshelf-CentOS-6.3-x86_64-minimal.box&quot;</span><span class="p">,</span><span class="nt">&quot;forward_port&quot;</span><span class="p">:{},</span><span class="nt">&quot;network&quot;</span><span class="p">:{</span><span class="nt">&quot;bridged&quot;</span><span class="p">:</span><span class="kc">false</span><span class="p">,</span><span class="nt">&quot;hostonly&quot;</span><span class="p">:</span><span class="s2">&quot;33.33.33.10&quot;</span><span class="p">},</span><span class="nt">&quot;provision&quot;</span><span class="p">:</span><span class="s2">&quot;chef_solo&quot;</span><span class="p">}},</span><span class="nt">&quot;ssl&quot;</span><span class="p">:{</span><span class="nt">&quot;verify&quot;</span><span class="p">:</span><span class="kc">false</span><span class="p">}}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This passive-aggressive attempt by the machines to subjugate us humans just will
not stand and thankfully we have the tools to take back control of our JSON
configs. Position the cursor anywhere on the single line of JSON and press &#8216;==&#8217;,
vim will pipe the buffer to <code>json_reformat</code> and replace it with the output of
that command resulting in this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="nt">&quot;chef&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class='line'>        <span class="nt">&quot;chef_server_url&quot;</span><span class="p">:</span> <span class="s2">&quot;https://api.opscode.com/organizations/acmecorp&quot;</span><span class="p">,</span>
</span><span class='line'>        <span class="nt">&quot;validation_client_name&quot;</span><span class="p">:</span> <span class="s2">&quot;acmecorp-validator&quot;</span><span class="p">,</span>
</span><span class='line'>        <span class="nt">&quot;validation_key_path&quot;</span><span class="p">:</span> <span class="s2">&quot;/Users/ada/Work/.chef/acmecorp-validator.pem&quot;</span><span class="p">,</span>
</span><span class='line'>        <span class="nt">&quot;client_key&quot;</span><span class="p">:</span> <span class="s2">&quot;/Users/ada/Work/.chef/ada.pem&quot;</span><span class="p">,</span>
</span><span class='line'>        <span class="nt">&quot;node_name&quot;</span><span class="p">:</span> <span class="s2">&quot;ada&quot;</span>
</span><span class='line'>    <span class="p">},</span>
</span><span class='line'>    <span class="nt">&quot;cookbook&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class='line'>        <span class="nt">&quot;copyright&quot;</span><span class="p">:</span> <span class="s2">&quot;Ada Lovelace&quot;</span><span class="p">,</span>
</span><span class='line'>        <span class="nt">&quot;email&quot;</span><span class="p">:</span> <span class="s2">&quot;ada@example.com&quot;</span><span class="p">,</span>
</span><span class='line'>        <span class="nt">&quot;license&quot;</span><span class="p">:</span> <span class="s2">&quot;MIT&quot;</span>
</span><span class='line'>    <span class="p">},</span>
</span><span class='line'>    <span class="nt">&quot;allowed_licenses&quot;</span><span class="p">:</span> <span class="p">[</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">],</span>
</span><span class='line'>    <span class="nt">&quot;raise_license_exception&quot;</span><span class="p">:</span> <span class="kc">false</span><span class="p">,</span>
</span><span class='line'>    <span class="nt">&quot;vagrant&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class='line'>        <span class="nt">&quot;vm&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class='line'>            <span class="nt">&quot;box&quot;</span><span class="p">:</span> <span class="s2">&quot;Berkshelf-CentOS-6.3-x86_64-minimal&quot;</span><span class="p">,</span>
</span><span class='line'>            <span class="nt">&quot;box_url&quot;</span><span class="p">:</span> <span class="s2">&quot;https://dl.dropbox.com/u/31081437/Berkshelf-CentOS-6.3-x86_64-minimal.box&quot;</span><span class="p">,</span>
</span><span class='line'>            <span class="nt">&quot;forward_port&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>            <span class="p">},</span>
</span><span class='line'>            <span class="nt">&quot;network&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class='line'>                <span class="nt">&quot;bridged&quot;</span><span class="p">:</span> <span class="kc">false</span><span class="p">,</span>
</span><span class='line'>                <span class="nt">&quot;hostonly&quot;</span><span class="p">:</span> <span class="s2">&quot;33.33.33.10&quot;</span>
</span><span class='line'>            <span class="p">},</span>
</span><span class='line'>            <span class="nt">&quot;provision&quot;</span><span class="p">:</span> <span class="s2">&quot;chef_solo&quot;</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">},</span>
</span><span class='line'>    <span class="nt">&quot;ssl&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class='line'>        <span class="nt">&quot;verify&quot;</span><span class="p">:</span> <span class="kc">false</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This trick with vim and json_reformat is one of the many answers to a
<a href="http://stackoverflow.com/a/6047998">StackOverflow question</a> about
pretty-printing JSON, there are so many more.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Tmux &amp; Autoenv Tip]]></title>
    <link href="http://blog.wjlr.org.uk/2014/02/01/tmux-and-autoenv-tip.html"/>
    <updated>2014-02-01T15:41:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2014/02/01/tmux-and-autoenv-tip</id>
    <content type="html"><![CDATA[<!--more-->


<p>On OS X, if I use <a href="http://tmux.sourceforge.net/">tmux</a> (or more usually,
<a href="https://github.com/zolrath/wemux">wemux</a> for pairing) then I end up with oddly
named windows like this:</p>

<p><img src="http://d14vbe8lasdppi.cloudfront.net/01_02_2014_15_45.jpg" alt="" /></p>

<p>This is because I&#8217;ve followed the awesome instructions from
<a href="http://www.drbunsen.org/the-text-triumvirate/">Dr Bunsen&#8217;s Text Triumvirate</a>
and in order to have the tmux and system (OS X) clipboards interacting, it
requires a hack called <code>reattach-to-user-namespace</code> which proxies the running of
zsh in tmux.</p>

<p>One quick way to get around this and save time renaming my tmux windows, was to
use <a href="https://github.com/kennethreitz/autoenv">autoenv</a> and add a file named
<code>.env</code> to projects with content similar to:</p>

<pre><code>tmux rename-window "project-name"
</code></pre>

<p>This is piling hacks on top of hacks, but at least it saves a bit of time for
frequently visited directories.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A Silver Searcher Gotcha]]></title>
    <link href="http://blog.wjlr.org.uk/2014/02/01/silver-searcher-gotcha.html"/>
    <updated>2014-02-01T15:34:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2014/02/01/silver-searcher-gotcha</id>
    <content type="html"><![CDATA[<p>There is a gotcha with <a href="https://github.com/ggreer/the_silver_searcher">the silver searcher</a> and how it treats
<code>.gitignore</code> files. The following pattern will work as expected with <code>git</code>:</p>

<pre><code>/log
</code></pre>

<p>I.e. <code>git</code> will ignore everything in that directory, however The Silver Searcher
doesn&#8217;t understand that and it&#8217;ll still show matching lines from files in
<code>log/</code>. For <code>git</code> and <code>ag</code> (The Silver Searcher) to both ignore that directory
and its contents, change <code>/log</code> to <code>log</code>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Ember transitionToRoute With New Model Hack]]></title>
    <link href="http://blog.wjlr.org.uk/2013/06/06/ember-transition-to-new-model-hack.html"/>
    <updated>2013-06-06T19:53:00+01:00</updated>
    <id>http://blog.wjlr.org.uk/2013/06/06/ember-transition-to-new-model-hack</id>
    <content type="html"><![CDATA[<p><aside class="update">
<strong>Update</strong>: None of this is required with current versions of Ember and
Ember-data.
</aside></p>

<!--more-->


<p>A common pattern with webapps in these exciting times is the
transition-to-new-thing-you-just-created pattern. The way it works is this: you
fill in a form to create a new thing (like, a widget or something) and then when
you hit &#8216;save&#8217;, you are transitioned to the page for that widget. This is so you
can confirm that it worked and so you could make further changes on that widget.
The assumption being, when you create a widget, it is the focus of your task.</p>

<p>Whatever, anyway, I had to make something along those lines in an EmberJS app at
work and hit an ember-data bug. Surprise!</p>

<p>The bug is that when you try to do this (code speaks louder than widget
metaphors):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">model</span> <span class="o">=</span> <span class="nx">App</span><span class="p">.</span><span class="nx">Widget</span><span class="p">.</span><span class="nx">createRecord</span><span class="p">({</span><span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;special_widget&quot;</span><span class="p">});</span>
</span><span class='line'><span class="nx">model</span><span class="p">.</span><span class="nx">one</span><span class="p">(</span><span class="s1">&#39;didCreate&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">this</span><span class="p">.</span><span class="nx">transitionToRoute</span><span class="p">(</span><span class="s1">&#39;widgets.show&#39;</span><span class="p">,</span> <span class="nx">model</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span><span class='line'><span class="nx">model</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;store&#39;</span><span class="p">).</span><span class="nx">commit</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>This doesn&#8217;t work with ember-data currently - what happens is the <code>model</code> object
that gets sent to the <code>didCreate</code> callback there won&#8217;t have an <code>id</code> from the
server (it hasn&#8217;t been filled in for some reason).</p>

<p>To work around this issue, you need to do something akin to this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">saveTheWidget</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">model</span> <span class="o">=</span> <span class="nx">App</span><span class="p">.</span><span class="nx">Widget</span><span class="p">.</span><span class="nx">createRecord</span><span class="p">({</span><span class="nx">name</span><span class="o">:</span> <span class="s2">&quot;very_special_widget&quot;</span><span class="p">});</span>
</span><span class='line'>  <span class="nx">model</span><span class="p">.</span><span class="nx">addObserver</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">showWidget</span><span class="p">);</span>
</span><span class='line'>  <span class="nx">model</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;store&#39;</span><span class="p">).</span><span class="nx">commit</span><span class="p">();</span>
</span><span class='line'><span class="p">},</span>
</span><span class='line'><span class="nx">showWidget</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">model</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">model</span><span class="p">.</span><span class="nx">removeObserver</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">showWidget</span><span class="p">);</span>
</span><span class='line'>  <span class="k">this</span><span class="p">.</span><span class="nx">transitionToRoute</span><span class="p">(</span><span class="s1">&#39;widgets.show&#39;</span><span class="p">,</span> <span class="nx">model</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>In that example we trigger the page transition off the change of value of <code>id</code>.
This works because for a short while, the <code>id</code> will be <code>null</code> until Ember-data
fills in the new <code>id</code> from the server. Then your callback is called and the page
transition happens. The difference in timing is not noticeable to the user - it&#8217;s
just a race condition within Ember-data itself. Other than that - in this
example, the observer is removed, which is good practice for observers and you
should always remove observers you&#8217;ve added (as I understand it).</p>

<p>Other alternative workarounds include wrapping the <code>transitionToRoute</code> call in a
<code>setTimeout()</code> block - to make it pause just long enough for the model&#8217;s <code>id</code> to
be filled in.</p>

<p>Most of the above came from
<a href="http://stackoverflow.com/questions/14981500/transition-after-saving-model-of-ember-data">Stackoverflow</a>
and the <a href="https://github.com/emberjs/data/issues/405#issuecomment-18891172">bug report and workaround</a> on
ember-data.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Imagemagick SVG Insanity]]></title>
    <link href="http://blog.wjlr.org.uk/2013/04/10/imagemagick-insanity.html"/>
    <updated>2013-04-10T00:00:00+01:00</updated>
    <id>http://blog.wjlr.org.uk/2013/04/10/imagemagick-insanity</id>
    <content type="html"><![CDATA[<!--more-->


<p>If you are trying to read SVGs and convert them (through STDIN/STDOUT, from a script for example), like this:</p>

<pre><code>cat test.svg | convert svg: png:- &gt; test.png
</code></pre>

<p>And you get this error:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>Error reading SVG:
</span><span class='line'>convert: delegate failed `"rsvg-convert" -o "%o" "%i"' @ error/delegate.c/InvokeDelegate/1058.
</span><span class='line'>convert: unable to open image `/tmp/magick-Oj094Z9n':  @ error/blob.c/OpenBlob/2587.
</span><span class='line'>convert: unable to open file `/tmp/magick-Oj094Z9n':  @ error/constitute.c/ReadImage/571.
</span><span class='line'>convert: missing an image filename `png:-' @ error/convert.c/ConvertImageCommand/3011.</span></code></pre></td></tr></table></div></figure>


<p>Then you need to install (on Ubuntu anyway):</p>

<pre><code>apt-get install libmagickcore4-extra
</code></pre>

<p>I had to strace the <code>convert</code> command to find out that it was looking for a missing file. This is from the strace output:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>stat("/usr/lib/ImageMagick-6.6.9/modules-Q16/coders/svg.la", 0x7fffe6cace90) = -1 ENOENT (No such file or directory)</span></code></pre></td></tr></table></div></figure>


<p>Words cannot describe how I feel about Imagemagick right now.</p>

<p>Note to self: install this required package (stupidly called &#8220;-extra&#8221;).</p>

<p>Also note to self: avoid Imagemagick like the plague.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Ember-rails and Handlebars_assets FIGHT!]]></title>
    <link href="http://blog.wjlr.org.uk/2013/02/18/ember-rails-handlebars-assets.html"/>
    <updated>2013-02-18T23:10:00+00:00</updated>
    <id>http://blog.wjlr.org.uk/2013/02/18/ember-rails-handlebars-assets</id>
    <content type="html"><![CDATA[<p>Just a quick note for Google and future-me. If you want to mix both <a href="https://github.com/emberjs/ember-rails">ember-rails</a> and <a href="https://github.com/leshill/handlebars_assets">handlebars_assets</a> in the same Rails project, you will encounter maddening issues with your Ember/Handlebars templates. Specifically, they will compile fine (probably) but Ember will not be able to see them.</p>

<h2>How to check whether Ember can ‘see’ your templates</h2>

<p>In your browsers console, type:</p>

<pre><code>Ember.TEMPLATES
</code></pre>

<p>You should see something like:</p>

<pre><code>Object {application: function, domains: function}
</code></pre>

<p>In the above example I have two templates defined: &#8216;application&#8217; and &#8216;domains&#8217;. If something is stopping ember-rails from putting your templates in that object, it will simply be a blank object.</p>

<h2>Why is this happening?</h2>

<p>This happened to me because both handlebars_assets and ember-rails were registering with the asset pipeline to process <code>.handlebars</code> files (and <code>.hbs</code> files). They fight and, at least in my case, handlebars_assets won.</p>

<h2>The fix</h2>

<p>The only way to get this to work reliably right now is to use a file extension for your Ember handlebars templates that isn’t recognised by handlebars_assets. I used <code>.hjs</code>. It’s not exactly a fix, but things are all working right now so that’s groovy.</p>

<h2>Why are you using both?</h2>

<p>There’s probably a way round this, but I’m using handlebars_assets to compile handlebars templates for some javascript (that ends up not being at all related to EmberJS - it’s embedded in other pages). I need those templates to be available in the <code>HandlebarsTemplates</code> object so I can run them in the javascript. I include <code>handlebars.runtime</code> in the javascript to run them. ember-rails includes that (I just looked) so I can probably ditch handlebars_assets. That’ll be the next thing to try.</p>

<p>This might not make a lot of sense, but if you’ve hit this problem (i.e. nothing renders in your EmberJS app but your templates are all compiled and you get no errors in the browser console) then this is one possible gotcha.</p>
]]></content>
  </entry>
  
</feed>
