<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.wordaligned.org/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
<channel>
<title>Word Aligned</title>
<link>http://wordaligned.org</link>
<description>tales from the code face</description>
<dc:creator>tag@wordaligned.org</dc:creator>
<language>en-gb</language>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.wordaligned.org/wordaligned" /><feedburner:info uri="wordaligned" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
<title>Angle brackets hurt my eyes</title>
<description>&lt;blockquote&gt;&lt;p&gt;&amp;#8220;All these angle brackets, they&amp;#8217;re hurting my eyes.&amp;#8221;
&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I&amp;#8217;m quoting a colleague who sits next to me and I sympathise. Perhaps a section from a build file was causing him pain?
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;&amp;lt;target name="all" description="Build product."&amp;gt;
  &amp;lt;mkdir dir="build" if="${not directory::exists('build')}" /&amp;gt;
   ....
&amp;lt;/target&amp;gt;

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Or maybe it was some XSL to generate XML by applying XML to XML?
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;&amp;lt;xsl:if test="$index-make or @index!='false'"&amp;gt;
  &amp;lt;xsl:if test="//index"&amp;gt;
    &amp;lt;fo:block text-align-last="justify" space-before="5pt"&amp;gt;
      &amp;lt;fo:basic-link internal-destination="index-page"&amp;gt;
        &amp;lt;xsl:choose&amp;gt;
          &amp;lt;xsl:when test="(/doc/@lang = 'ja') or (/doc/@lang = '')&amp;lt;/xsl:when&amp;gt;
          &amp;lt;xsl:otherwise&amp;gt;INDEX&amp;lt;/xsl:otherwise&amp;gt;
        &amp;lt;/xsl:choose&amp;gt;
      &amp;lt;/fo:basic-link&amp;gt;
      &amp;lt;fo:page-number-citation ref-id="index-page"/&amp;gt;
    &amp;lt;/fo:block&amp;gt;
  &amp;lt;/xsl:if&amp;gt;
&amp;lt;/xsl:if&amp;gt;

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Or perhaps he was wrestling with a C++ compile error.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;print.cpp: In function 'void print(const
std::map&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;,
std::allocator&amp;lt;char&amp;gt; &amp;gt;, int, std::less&amp;lt;std::basic_string&amp;lt;char,
std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &amp;gt; &amp;gt;,
std::allocator&amp;lt;std::pair&amp;lt;const std::basic_string&amp;lt;char,
std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &amp;gt;, int&amp;gt; &amp;gt; &amp;gt;&amp;amp;)':
print.cpp:7: error: no match for 'operator&amp;lt;&amp;lt;' in 'std::cout &amp;lt;&amp;lt;
details' /usr/include/c++/4.2.1/ostream:112: note: candidates are:
std::basic_ostream&amp;lt;_CharT, _Traits&amp;gt;&amp;amp; std::basic_ostream&amp;lt;_CharT,
_Traits&amp;gt;::operator&amp;lt;&amp;lt;(std::basic_ostream&amp;lt;_CharT, _Traits&amp;gt;&amp;amp;
(*)(std::basic_ostream&amp;lt;_CharT, _Traits&amp;gt;&amp;amp;)) [with _CharT = char,
_Traits = std::char_traits&amp;lt;char&amp;gt;]

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;What makes angle brackets so vexatious. Could it be their sharp corners?
&lt;/p&gt;
&lt;p&gt;Lisp is as elegant as XML is prickly, yet it too is famous for brackets, &lt;a href="http://en.wikipedia.org/wiki/S-expression"&gt;albeit round ones&lt;/a&gt; &amp;#8212; and lots of them.
&lt;/p&gt;
&lt;blockquote class="twitter-tweet"&gt;&lt;p&gt;It&amp;#8217;s not that Clojure/Lisp has a lot of parentheses. Its just that we removed everything else.&lt;/p&gt;&amp;mdash; Alex Miller (@puredanger) &lt;a href="https://twitter.com/puredanger/status/313507982623268865"&gt;March 18, 2013&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async src="http://wordaligned.org//platform.twitter.com/widgets.js" charset="utf-8"&gt;&lt;/script&gt;

Imagine a parallel universe with angle-bracket Lisp. I wonder if the language would have proved so enduring?



&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;Not so pretty now?&lt;/div&gt;

&lt;pre class="prettyprint"&gt;&amp;lt;define &amp;lt;euler-transform s&amp;gt;
  &amp;lt;let &amp;lt;&amp;lt;s0 &amp;lt;stream-ref s 0&amp;gt;&amp;gt;
        &amp;lt;s1 &amp;lt;stream-ref s 1&amp;gt;&amp;gt;    
        &amp;lt;s2 &amp;lt;stream-ref s 2&amp;gt;&amp;gt;&amp;gt;
    &amp;lt;cons-stream &amp;lt;- s2 &amp;lt;/ &amp;lt;square &amp;lt;- s2 s1&amp;gt;&amp;gt;
                          &amp;lt;+ s0 &amp;lt;* -2 s1&amp;gt; s2&amp;gt;&amp;gt;&amp;gt;
                 &amp;lt;euler-transform &amp;lt;stream-cdr s&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;

&lt;/pre&gt;

&lt;/div&gt;



Looking more closely at the first code snippet, the section from a build file, we can see the problem isn&amp;#8217;t so much the angle brackets as all the others.



&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;&amp;lt;mkdir dir="build" if="${not directory::exists('build')}" /&amp;gt;

&lt;/pre&gt;

&lt;/div&gt;



The braces and parentheses may be embedded in a double quoted string but that only makes things worse. The nested levels hurt my eyes and, if you can bear to look at the code long enough, you&amp;#8217;ll realise there&amp;#8217;s a simpler message trying to get out: &lt;tt&gt;mkdir -p build&lt;/tt&gt;.&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/wwvVTA1bfyE" height="1" width="1"/&gt;</description>
<dc:date>2013-05-01</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/angle-brackets-hurt-my-eyes</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/wwvVTA1bfyE/angle-brackets-hurt-my-eyes</link>
<category>Lisp</category>
<category>Syntax</category>
<feedburner:origLink>http://wordaligned.org/articles/angle-brackets-hurt-my-eyes</feedburner:origLink></item>

<item>
<title>“Solutions”</title>
<description>&lt;p&gt;I like working around enthusiasts and optimists but that doesn&amp;#8217;t mean I want to use chirpy or bombastic software.
&lt;/p&gt;
&lt;p&gt;These days I build programs using visual studio. Sure, it&amp;#8217;s a decent tool but part of me cringes every time I&amp;#8217;m asked to open a &lt;strong&gt;&amp;#8220;Solution&amp;#8221;&lt;/strong&gt; especially when what I get seems more like a &lt;strong&gt;&amp;#8220;Muddle&amp;#8221;&lt;/strong&gt;. A progress bar slides into action after my program links: &lt;strong&gt;&amp;#8220;Updating Intellisense &amp;#8230;&amp;#8221;&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;Who coined that galumphing portmanteau? It means auto-completion and I don&amp;#8217;t want to know it&amp;#8217;s going on &amp;#8212; especially since I edit code using &lt;a href="http://wordaligned.org/articles/accidental-emacs"&gt;Emacs&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;Perforce is new to me and I lean on a &lt;a href="http://www.perforce.com/product/components/perforce-visual-client" title="P4V"&gt;graphical client&lt;/a&gt; so heavily I sometimes trip over it. So when I&amp;#8217;m trying to dance round client workspaces and their half-baked integration with microsoft &lt;del&gt;muddles&lt;/del&gt; solutions, the last thing I want is to be asked to &lt;strong&gt;&amp;#8220;Choose a Favorite Connection&amp;#8221;&lt;/strong&gt;. When it comes to Perforce Servers I don&amp;#8217;t have favourites let alone favorites. Sorry.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/tpTlaJkECbs" height="1" width="1"/&gt;</description>
<dc:date>2013-04-17</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/solutions</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/tpTlaJkECbs/solutions</link>
<category>Windows</category>
<category>C++</category>
<category>Emacs</category>
<feedburner:origLink>http://wordaligned.org/articles/solutions</feedburner:origLink></item>

<item>
<title>ACCU 2013</title>
<description>&lt;img src="http://accu.org/content/images/conferences/2013/accu2013web.png" alt="ACCU 2013" /&gt;

&lt;p&gt;Brian Marick opened the second day of ACCU 2013 with a keynote presentation entitled: &lt;a href="http://accu.org/index.php/conferences/accu_conference_2013/accu2013_sessions#cheating_decline:acting_now_to_let_you_program_well_for_a_really_long_time"&gt;&amp;#8220;Cheating Decline: Acting now to let you program well for a really long time&amp;#8221;&lt;/a&gt;. He drew a distinction between effortful and automatic thinking. For example, we can drive a car along a clear road automatically but it requires considerable concentration to parallel park that same car. By tuning out unwanted signals crickets can locate their mates using minimal brainpower, and cricket players have no need of Newtonian dynamics to track the trajectory of a ball &amp;#8212; they apply a simple visual self-calibrating algorithm to catch out batsmen. Tango dancers disturb and re-establish invariants. A robot can walk for hours without thinking about what it&amp;#8217;s doing. Actually, if it&amp;#8217;s your job to park cars, you can probably do &lt;strong&gt;that&lt;/strong&gt; without thinking; and this was Brian Marick&amp;#8217;s main cheat &amp;#8212; find the work practices which allow you to lean on your perceptions and so avoid effortful thinking.
&lt;/p&gt;
&lt;iframe width="560" height="315" src="http://www.youtube.com/embed/rhu2xNIpgDE?rel=0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;p&gt;Anthony Williams&amp;#8217; talk on &lt;a href="http://accu.org/index.php/conferences/accu_conference_2013/accu2013_sessions#c_11_features_and_real-world_code"&gt;C++11 Features and Real World code&lt;/a&gt; did require effortful thinking but that was what I&amp;#8217;d hoped for. He provided a concise and expert summary of the new language features in action, focusing on the biggest early winners. Leading with &lt;code&gt;auto&lt;/code&gt;, &lt;code&gt;lambda&lt;/code&gt;, &lt;code&gt;range-for&lt;/code&gt;, he went on to talk about concurrency and move semantics. I learned that &lt;code&gt;lambda&lt;/code&gt; functions can have a mutable qualifier. Ha!
&lt;/p&gt;
&lt;p&gt;I couldn&amp;#8217;t resist &lt;a href="http://www.pvv.org/~oma/"&gt;Olve Maudal&amp;#8217;s&lt;/a&gt; C++11 pub quiz, appropriately held in the Marriot Hotel bar, for which we formed teams and mentally compiled and executed dodgy code, capturing standard output on an answer sheet. Some of the answers may well have have been implementation dependent but Olve specified an implementation: our answers should match &lt;strong&gt;this&lt;/strong&gt; laptop running &lt;strong&gt;this&lt;/strong&gt; software. I was simultaneously appalled by the limits of my knowledge on fundamental subjects such as integral promotion and initialisation order, and surprised by my ability to correctly predict the behaviour of some esoteric and perverse code. I&amp;#8217;m chastened and will be studying the answers in the cold light of day. Brian Marick may have advocated programming after a beer or two in his morning session, but the afternoon pub quiz proved that coffee works better for me!
&lt;/p&gt;
&lt;p&gt;A programmer&amp;#8217;s dozen (13, which is 12 counting from zero!) lightning talks kept the day crackling with energy. Ewan Milne chaired the session expertly, adeptly dispatching a birthday cake as proceedings commenced. I wish I could describe all the talks but you really had to be there. Phil Nash&amp;#8217;s use of the little known &lt;a href="http://ideone.com/jWHxu2"&gt;left arrow operator&lt;/a&gt; &amp;larr; got a well deserved response from the audience. Sander Hoogendoorn stuck the boot into &lt;a href="http://sanderhoogendoorn.com/blog/?p=1059"&gt;&amp;#8220;Agile Fluffiness&amp;#8221;&lt;/a&gt;. &lt;a href="http://www.renaissancesoftware.net/blog/"&gt;James Grenning&amp;#8217;s&lt;/a&gt; talk on embedded development was a lightning keynote: hilarious, moving and, ultimately, tragic.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/6wdHGG4pu5M" height="1" width="1"/&gt;</description>
<dc:date>2013-04-11</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/accu-2013</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/6wdHGG4pu5M/accu-2013</link>
<category>ACCU</category>
<category>C++</category>
<category>Bristol</category>
<feedburner:origLink>http://wordaligned.org/articles/accu-2013</feedburner:origLink></item>

<item>
<title>An Exploration of the Phenomenology of Software Development</title>
<description>&lt;p&gt;I was lucky to be in the audience last week, when &lt;a href="http://charlestolman.com/"&gt;Charles Tolman&lt;/a&gt; visited Accu Bristol to preview his Accu 2013 conference talk: &lt;a href="http://www.meetup.com/ACCU-Bristol-Bath/events/105025942" title="An Exploration of the Phenomenology of Software Development"&gt;&amp;#8220;An Exploration of the Phenomenology of Software Development&amp;#8221;&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;The talk precis is entirely accurate &amp;#8212; but maybe not so helpful. What this talk actually delivers is a highly original and thoughtful examination of what the software development revolution is and how we can make sense of it.
&lt;/p&gt;
&lt;p&gt;Charles Tolman&amp;#8217;s central insight is that we have crossed a boundary. New tools and technologies have extended our powers. Just as  machinery developed in the industrial revolution extended our physical abilities, so software developed in the information technology revolution extends our capacity for thought.
&lt;/p&gt;
&lt;p&gt;Essentially, software development &lt;strong&gt;is&lt;/strong&gt; thinking, so to analyse it we need to think about thought. With this realisation we can view our industry as a continuation of the efforts of earlier thought workers &amp;#8212; philosophers such as Descartes and Goethe.
&lt;/p&gt;
&lt;p&gt;If this all sounds a bit heavy, Charles made space for anecdotes, humourous insights and pictures of gliders. I look forward to future episodes.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/uK-xuEvnVeg" height="1" width="1"/&gt;</description>
<dc:date>2013-04-02</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/an-exploration-of-the-phenomenology-of-software-development</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/uK-xuEvnVeg/an-exploration-of-the-phenomenology-of-software-development</link>
<category>ACCU</category>
<feedburner:origLink>http://wordaligned.org/articles/an-exploration-of-the-phenomenology-of-software-development</feedburner:origLink></item>

<item>
<title>Patience Sorted</title>
<description>&lt;p&gt;I gave a lightning talk today about patience sorting and its application to the longest increasing subsequence problem. It&amp;#8217;s a subject I&amp;#8217;ve &lt;a href="http://wordaligned.org/articles/patience-sort" title="Patience sort and the longest increasing subsequence"&gt;written about&lt;/a&gt; before. My computer has been put through several million simulations. I&amp;#8217;ve even coded up a &lt;a href="http://wordaligned.org/pages/psort"&gt;javascript demo&lt;/a&gt; which deals out virtual playing cards and sorts them at the click of a button.
&lt;/p&gt;
&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
var zz = 0;

var xtop  = ["330px", "302px", "275px", "255px", "234px", "209px", "187px", "158px", "128px", "99px", "70px", "43px", "15px"];
var ytop  = ["26px", "30px", "28px", "29px", "31px", "28px", "27px", "28px", "28px", "30px", "26px", "30px", "29px"];
var xpile = ["10px", "13px", "12px", "115px", "14px", "220px", "220px", "319px", "15px", "216px", "118px", "118px", "321px"];
var ypile = ["205px", "233px", "266px", "206px", "295px", "206px", "236px", "209px", "325px", "267px", "235px", "263px", "234px"];
var result = ["#card7", "#card6", "#card3", "#card2"];

function reset_cards() {
    jQuery("img").stop();
    zz += 13;
    for (var j = 0; j != 13; ++j, --zz) {
        jQuery("#card" + j)
        .css({"z-index":zz, "margin-left":xtop[j], "margin-top":ytop[j], "border-width":0});
    }
    zz += 13;
}

function lis(j) {
    var it = jQuery(result[j]);
    if (it.length &gt; 0) {
        it.animate({borderWidth : "5px"}, "slow", 0, function(){lis(++j);});
    }
}

function psort(j) {
    var it = jQuery("#card"+j);
    if (it.length != 0) {
        it
        .animate({marginLeft : xpile[j], marginTop: ypile[j]}, "slow", 0, function(){psort(++j);})
        .css({"z-index" : ++zz});
    } else {
        lis(0);
    }
}

function animate() {
    reset_cards();
    psort(0);
}
&lt;/script&gt;

&lt;div class="cardtable" style="background-color: #093; width:440px; height:480px;"&gt;
&lt;img id="card0" style="border: 0 orange solid; position: absolute; margin-left: 330px; margin-top: 27px; z-index: 13;" src="http://wordaligned.org/images/cards/1s.png" alt="Ace of Spades"/&gt;
&lt;img id="card1" style="border: 0 orange solid; position: absolute; margin-left: 302px; margin-top: 27px; z-index: 12;" src="http://wordaligned.org/images/cards/10s.png" alt="10 of Spades"/&gt;
&lt;img id="card2" style="border: 0 orange solid; position: absolute; margin-left: 275px; margin-top: 27px; z-index: 11;" src="http://wordaligned.org/images/cards/6s.png" alt="6 of Spades"/&gt;
&lt;img id="card3" style="border: 0 orange solid; position: absolute; margin-left: 255px; margin-top: 26px; z-index: 10;" src="http://wordaligned.org/images/cards/7s.png" alt="7 of Spades"/&gt;
&lt;img id="card4" style="border: 0 orange solid; position: absolute; margin-left: 234px; margin-top: 28px; z-index: 9;" src="http://wordaligned.org/images/cards/5s.png" alt="5 of Spades"/&gt;
&lt;img id="card5" style="border: 0 orange solid; position: absolute; margin-left: 209px; margin-top: 26px; z-index: 8;" src="http://wordaligned.org/images/cards/13s.png" alt="King of Spades"/&gt;
&lt;img id="card6" style="border: 0 orange solid; position: absolute; margin-left: 187px; margin-top: 31px; z-index: 7;" src="http://wordaligned.org/images/cards/9s.png" alt="9 of Spades"/&gt;
&lt;img id="card7" style="border: 0 orange solid; position: absolute; margin-left: 158px; margin-top: 27px; z-index: 6;" src="http://wordaligned.org/images/cards/12s.png" alt="Queen of Spades"/&gt;
&lt;img id="card8" style="border: 0 orange solid; position: absolute; margin-left: 128px; margin-top: 29px; z-index: 5;" src="http://wordaligned.org/images/cards/2s.png" alt="2 of Spades"/&gt;
&lt;img id="card9" style="border: 0 orange solid; position: absolute; margin-left: 99px; margin-top: 29px; z-index: 4;" src="http://wordaligned.org/images/cards/8s.png" alt="8 of Spades"/&gt;
&lt;img id="card10" style="border: 0 orange solid; position: absolute; margin-left: 70px; margin-top: 26px; z-index: 3;" src="http://wordaligned.org/images/cards/4s.png" alt="4 of Spades"/&gt;
&lt;img id="card11" style="border: 0 orange solid; position: absolute; margin-left: 43px; margin-top: 27px; z-index: 2;" src="http://wordaligned.org/images/cards/3s.png" alt="3 of Spades"/&gt;
&lt;img id="card12" style="border: 0 orange solid; position: absolute; margin-left: 15px; margin-top: 28px; z-index: 1;" src="http://wordaligned.org/images/cards/11s.png" alt="Knave of Spades"/&gt;
&lt;/div&gt;&lt;div&gt;&lt;p&gt;&lt;button onclick="reset_cards();"&gt;Reset&lt;/button&gt;&lt;button onclick="animate();"&gt;Play&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;Today I used real playing cards; a linen-finished standard deck. For any talk it&amp;#8217;s nice to have a prop.
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/thomasguest/8558343442/" title="Rock &amp;amp; Pop Legends by Thomas Guest, on Flickr"&gt;&lt;img src="http://farm9.staticflickr.com/8100/8558343442_0402f04e83.jpg" width="500" height="361" alt="Rock &amp;amp; Pop Legends"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;Now, I &lt;strong&gt;thought&lt;/strong&gt; I understood the patience sort algorithm but until yesterday I&amp;#8217;d never actually played it with real cards. I&amp;#8217;ve been surprised by how much this physical dimension has developed my understanding.
&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ll be testing my new cards on some other sorting algorithms; I have high hopes. It would be good to find a similarly simple prop for linked data structures so I can balance trees, flip lists and walk graphs.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/GQ0g4yhknas" height="1" width="1"/&gt;</description>
<dc:date>2013-03-14</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/patience-sorted</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/GQ0g4yhknas/patience-sorted</link>
<category>Algorithms</category>
<category>Puzzles</category>
<category>Self</category>
<feedburner:origLink>http://wordaligned.org/articles/patience-sorted</feedburner:origLink></item>

<item>
<title>Hosting for Life? TextDrive revived!</title>
<description>&lt;p&gt;Towards the end of last year I was emailed by Jason Hoffman from Joyent. &lt;a href="http://en.wikipedia.org/wiki/Joyent" title="Wikipedia on Joyent and TextDrive"&gt;Joyent owned TextDrive&lt;/a&gt;, the company which, 6 years ago, offered web hosting &lt;strong&gt;for life&lt;/strong&gt; in return for a single up-front payment. Jason&amp;#8217;s email said this lifetime hosting &amp;#8212; legacy service, he called it &amp;#8212; was nearing &lt;strong&gt;End of Life&lt;/strong&gt; (!) and would be sunsetted (!!) at the end of October 2012.
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://textdrive.com"&gt;&lt;img src="http://wordaligned.org/images/txd-banner.png" alt="TextDrive banner"/&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;As a TextDrive customer I reckon I&amp;#8217;d had good value from the original deal, but while six years may seem like forever on the internet it&amp;#8217;s surely not a lifetime. Happily TextDrive&amp;#8217;s original founder, Dean Allen, &lt;a href="http://techcrunch.com/2012/08/30/happy-ending-to-the-joyent-lifetime-subscription-story/" title="TechCrunch reports on Joyent/TextDrive"&gt;felt the same way&lt;/a&gt;: &lt;strong&gt;he&lt;/strong&gt; emailed me to say he&amp;#8217;d be relaunching &lt;a href="http://textdrive.com/"&gt;TextDrive&lt;/a&gt; on a new, modern, infrastructure.
&lt;/p&gt;
&lt;p&gt;Quite how this pans out remains to be seen but I&amp;#8217;m sticking it out. If you&amp;#8217;re reading this page, great: it&amp;#8217;s been served up by the new, modern infrastructure which Dean mentioned. Whilst migrating &lt;a href="http://wordaligned.org/"&gt;wordaligned.org&lt;/a&gt; I&amp;#8217;ve had to fiddle with rewrite rules and tinker with DNS records and file permissions. Any glitches or oddities, please &lt;a href="mailto:tag@wordaligned.org?subject=Wordaligned.org%20glitches"&gt;let me know&lt;/a&gt;.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/zdpmzHBTlGs" height="1" width="1"/&gt;</description>
<dc:date>2013-03-06</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/hosting-for-life-textdrive-revived</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/zdpmzHBTlGs/hosting-for-life-textdrive-revived</link>
<category>Self</category>
<category>Web-applications</category>
<feedburner:origLink>http://wordaligned.org/articles/hosting-for-life-textdrive-revived</feedburner:origLink></item>

<item>
<title>More adventures in C++</title>
<description>&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;bool operator&amp;lt;(version const &amp;amp; v1, version const &amp;amp; v2)
{
    if (v1.major != v2.major)
        return v1.major &amp;lt; v2.major;
    if (v1.minor != v2.minor)
        return v1.minor &amp;lt; v2.minor;
    if (v1.patch != v2.patch)
        return v1.patch &amp;lt; v2.patch;
    return v1.build &amp;lt; v2.build;
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;C++ programmers are sticklers for tradition and unlikely to be swayed by &lt;a href="http://www.zemanta.com/blog/i-bet-you-over-engineered-your-startup/#comment-685047168" title="unlike web developers"&gt;what&amp;#8217;s in fashion&lt;/a&gt;. C++ suits those who want to control the machine, and who respect the rigour and discipline this imposes. C++ programmers are generally a conservative bunch.
&lt;/p&gt;
&lt;p&gt;Some history: C++ was standardized in 1998. The next major revision of the language was developed under the working title of C++0x, where the &amp;#8220;0x&amp;#8221; stood for the year the job would be finished. The X gave the standardizers some slack, but not enough. C++0x became C++11 which is now, thankfully, simply C++.
&lt;/p&gt;
&lt;p&gt;Although the language&amp;#8217;s development has been painstakingly slow the developments themselves have been extensive and radical. What&amp;#8217;s more, users are rushing to use the new features &amp;#8212; even before they have access to compilers which support them! I&amp;#8217;ve seen answers to C++ topics on Q&amp;amp;A sites which use aspects of the language the contributors cheerfully admit they have no access to. I&amp;#8217;ve worked on a project which used elaborate shims to hide the fact that GCC 4.6 couldn&amp;#8217;t compile C++ as well as GCC 4.7 does, and this despite the fact that &lt;a href="http://gcc.gnu.org/projects/cxx0x.html" title="Important: GCC's support for C++11 is still experimental"&gt;GCC&amp;#8217;s C++11 support remains, officially, &amp;#8220;experimental&amp;#8221;&lt;/a&gt;.
&lt;/p&gt;
&lt;span id="continue-reading"/&gt;

&lt;p&gt;At home, I&amp;#8217;m downloading compiler and library updates I&amp;#8217;m in no position to use at work; and at work, I&amp;#8217;ve already been sent on a C++11 training course.  I&amp;#8217;ve streamed high quality &lt;a href="http://channel9.msdn.com/Events/GoingNative/GoingNative-2012" title="Going Native 2012 - good stuff here!"&gt;videos&lt;/a&gt; starring C++&amp;#8217;s big hitters which promote the new C++, explaining its principles, its foundations, and even where it&amp;#8217;s going next.
&lt;/p&gt;
&lt;p&gt;What exactly is it about C++11 that&amp;#8217;s roused such a normally phlegmatic audience?
&lt;/p&gt;
&lt;p&gt;Before I try and answer that, I&amp;#8217;ll venture to suggest new C++ isn&amp;#8217;t going to win many new recruits. I don&amp;#8217;t even think it will persuade those who have abandoned the language to return. C++11 contains all of C++98, a notoriously complex and subtle language, then adds &lt;a href="http://www.stroustrup.com/C++11FAQ.html#learn" title="Is C++11 hard to learn? Stroustrup C++11 FAQ"&gt;a whole lot more&lt;/a&gt;. Yes, it &lt;strong&gt;is&lt;/strong&gt; possible to write new C++ which is more compact and efficient than traditional C++, but you&amp;#8217;ll also need to maintain old C++ code and build new expertise. And the language update fails to address some of C++&amp;#8217;s worst characteristics: slow compile times and impenetrable compiler diagnostics.
&lt;/p&gt;
&lt;p&gt;No, C++11 is primarily a win for existing C++ programmers; those of us who already have a fair understanding of the language and its trade-offs, and who can appreciate the rationale behind the changes. For traditionalists and pragmatists, the transition isn&amp;#8217;t hard &amp;#8212; at least, no harder than any port between compiler revisions. For progressives, there are several immediate wins: the &lt;code&gt;auto&lt;/code&gt; keyword has been repurposed, reducing repetition and making code more flexible; lambdas enable functions to be plugged directly into algorithms; smart pointers are standard, allowing accurate memory management; and on the subject of memory, rvalues and move semantics mean you&amp;#8217;ll waste less of it on temporaries.
&lt;/p&gt;
&lt;p&gt;I could go on.
&lt;/p&gt;
&lt;p&gt;Rather than risk more generalisations, here&amp;#8217;s a specific example of C++11 in action. Consider an object with multiple fields, a four part version number, say.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;struct version
{
    unsigned major, minor, patch, build;
};

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To compare version numbers, or sort them, or put them in a &lt;code&gt;std::set&lt;/code&gt;, we&amp;#8217;ll need &lt;code&gt;operator&amp;lt;()&lt;/code&gt;. This operator must model a &lt;a href="http://www.sgi.com/tech/stl/StrictWeakOrdering.html"&gt;strict weak ordering&lt;/a&gt;. The canonical form looks something like.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;bool operator&amp;lt;(version const &amp;amp; v1, version const &amp;amp; v2)
{
    if (v1.major != v2.major)
        return v1.major &amp;lt; v2.major;
    if (v1.minor != v2.minor)
        return v1.minor &amp;lt; v2.minor;
    if (v1.patch != v2.patch)
        return v1.patch &amp;lt; v2.patch;
    return v1.build &amp;lt; v2.build;
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It&amp;#8217;s not so hard to write this code for the &lt;code&gt;version&lt;/code&gt; struct, where we have a clear idea of what it means for one version number to be less than another. It would be rather more tricky if we were dealing with points, for example, &lt;code&gt;struct point { int x, y; };&lt;/code&gt;. Ordering points makes little sense but we might well want them as keys in an associative container, and we&amp;#8217;d better have a suitable &lt;code&gt;operator&amp;lt;()&lt;/code&gt;.
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;No, no, no!&lt;/div&gt;

&lt;pre class="prettyprint"&gt;bool operator&amp;lt;(point const &amp;amp; p1, point const &amp;amp; p2)
{
    return p1.x &amp;lt; p2.x &amp;amp;&amp;amp; p1.y &amp;lt; p2.y;
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With C++11 &amp;#8212; &lt;strong&gt;with the current version of C++&lt;/strong&gt; &amp;#8212; we can use &lt;code&gt;std::tie()&lt;/code&gt; to create a tuple of references, recasting &lt;code&gt;operator&amp;lt;()&lt;/code&gt; into a form that&amp;#8217;s easy to read and hard to get wrong.
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;Yes, yes, yes!&lt;/div&gt;

&lt;pre class="prettyprint"&gt;bool operator&amp;lt;(version const &amp;amp; v1, version const &amp;amp; v2)
{
    return std::tie(v1.major, v1.minor, v1.patch, v1.build)
         &amp;lt; std::tie(v2.major, v2.minor, v2.patch, v2.build);
}

bool operator&amp;lt;(point const &amp;amp; p1, point const &amp;amp; p2)
{
    return std::tie(p1.x, p1.y) &amp;lt; std::tie(p2.x, p2.y);
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p style="text-align:center"&gt;&amp;sect;&lt;/p&gt;

&lt;p&gt;My thanks to Jonathan Wakely for sharing the &lt;code&gt;std::tie()&lt;/code&gt; recipe on the &lt;a href="http://accu.org/index.php/mailinglists"&gt;accu-general mailing list&lt;/a&gt; and for letting me use it here.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/X40fxGMN4xE" height="1" width="1"/&gt;</description>
<dc:date>2013-02-21</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/more-adventures-in-c++</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/X40fxGMN4xE/more-adventures-in-c++</link>
<category>C++</category>
<category>Accu</category>
<feedburner:origLink>http://wordaligned.org/articles/more-adventures-in-c++</feedburner:origLink></item>

<item>
<title>Singly Linked Lists in C++</title>
<description>&lt;p&gt;In a &lt;a href="http://wordaligned.org/articles/two-star-programming"&gt;recent post&lt;/a&gt; I wrote about removing items from a singly linked list. I presented a couple of alternative implementations, and in the comments section readers suggested yet more versions.
&lt;/p&gt;
&lt;p&gt;My implementations were written in C: the post was inspired by something &lt;a href="http://meta.slashdot.org/story/12/10/11/0030249/linus-torvalds-answers-your-questions"&gt;Linus Torvalds had said&lt;/a&gt; about low-level programming skills, and I&amp;#8217;m guessing he meant C programming skills. The fact is, C programmers are condemned to reimplement these basic functions on this basic structure because the C standard library has nothing to say about singly linked lists. Until recently the C++ standard library was similarly silent on the subject, only offering a doubly linked list.
&lt;/p&gt;

&lt;h3&gt;C++ introduces &amp;#8230; the linked list!&lt;/h3&gt;
&lt;p&gt;That&amp;#8217;s all changed now with the introduction of &lt;code&gt;std::forward_list&lt;/code&gt;. The &lt;a href="http://en.cppreference.com/w/cpp/container/forward_list"&gt;class interface&lt;/a&gt; doesn&amp;#8217;t mention links or pointers but a quick glance through its contents makes it clear that if you imagine the container to be a templated version of a classic singly-linked list, you won&amp;#8217;t go far wrong.
&lt;/p&gt;
&lt;p&gt;This gives &lt;code&gt;forward_list&lt;/code&gt; some members you won&amp;#8217;t find elsewhere in the &lt;code&gt;std::&lt;/code&gt; namespace. For example, &lt;code&gt;std::forward_list::before_begin()&lt;/code&gt;, which returns an iterator just before the beginning of the list &amp;#8212; much as the more familiar &lt;code&gt;end()&lt;/code&gt; is just past the end.
&lt;/p&gt;
&lt;p&gt;Why is &lt;code&gt;before_begin()&lt;/code&gt; necessary? You can&amp;#8217;t dereference it and you can&amp;#8217;t reverse through a forward list till you get to it. Well, since forward list iterators can only go forwards, instead of the familiar sequence &lt;code&gt;insert()&lt;/code&gt;, &lt;code&gt;erase()&lt;/code&gt; and &lt;code&gt;emplace()&lt;/code&gt; methods we have &lt;code&gt;insert_after()&lt;/code&gt;, &lt;code&gt;erase_after()&lt;/code&gt; and &lt;code&gt;emplace_after()&lt;/code&gt;, not to mention &lt;code&gt;splice_after()&lt;/code&gt;. The before-the-beginning iterator allows you to use these operations to modify the node at the head of the list.
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Quick question&lt;/strong&gt;: what&amp;#8217;s the complexity of &lt;code&gt;std::list::size()&lt;/code&gt;?
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Trick question&lt;/strong&gt;: and how about &lt;code&gt;std::forward_list::size()&lt;/code&gt;?
&lt;/p&gt;
&lt;span id="continue-reading"/&gt;


&lt;h3&gt;Remove_if for forward lists&lt;/h3&gt;
&lt;p&gt;Using pointers-to-pointers to modify linked lists gives this elegant and compact C implementation of &lt;code&gt;remove_if()&lt;/code&gt;, which de-lists all nodes which match a supplied predicate.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;void remove_if(node ** head, remove_fn rm)
{
    for (node** curr = head; *curr; )
    {
        node * entry = *curr;
        if (rm(entry))
        {
            *curr = entry-&amp;gt;next;
            free(entry);
        }
        else
            curr = &amp;amp;entry-&amp;gt;next;
    }
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;How does the C++ standard library support this algorithm?
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://en.cppreference.com/w/cpp/algorithm/remove"&gt;&lt;code&gt;Std::remove_if()&lt;/code&gt;&lt;/a&gt; operates on an iterator range, &lt;code&gt;remove_if(first, last, pred)&lt;/code&gt;. All it requires is that the iterators are forward iterators so it should just work on a &lt;code&gt;forward_list&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;Hang on though: what if &lt;code&gt;pred(*first)&lt;/code&gt; is true? How can a node be removed from a linked list without reference to its predecessor? Actually, the node isn&amp;#8217;t removed &amp;#8212; the value it contains gets overwritten by the value in the first node (if any!) for which the predicate returns false. In fact, &lt;code&gt;remove_if&lt;/code&gt; &lt;strong&gt;doesn&amp;#8217;t remove anything&lt;/strong&gt; from the container! What it does is return an iterator, call it &lt;code&gt;new_last&lt;/code&gt;, such that the range &lt;code&gt;(first, new_last)&lt;/code&gt; holds the elements which have been kept, and &lt;code&gt;(new_last, last)&lt;/code&gt; holds those which have been &amp;#8220;removed&amp;#8221;, which is to say they can still be dereferenced but their value is implementation dependent.
&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Remove_if&lt;/code&gt; usually pairs up with erase:
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;container.erase(remove_if(first, last, pred), last);

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;There is no &lt;code&gt;std::forward_list::erase(iterator)&lt;/code&gt; &amp;#8212; remember, we can only erase &lt;strong&gt;after&lt;/strong&gt; &amp;#8212; so the usual remove_if algorithm is of little use for forward lists.
&lt;/p&gt;

&lt;h3&gt;Forward_list::remove_if()&lt;/h3&gt;
&lt;p&gt;As usual, we should &lt;a href="http://www.informit.com/articles/article.aspx?p=21851" title="Scott Meyers, Effective STL"&gt;prefer member functions to algorithms with the same names&lt;/a&gt;. C++&amp;#8217;s &lt;code&gt;forward_list&lt;/code&gt; has its very own &lt;code&gt;remove_if&lt;/code&gt; which manipulates pointers rather than moves values, and which really does remove and destroy items.
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://en.cppreference.com/w/cpp/container/forward_list/remove"&gt;&lt;code&gt;Forward_list::remove_if()&lt;/code&gt;&lt;/a&gt; operates on the list as a whole, not an iterator range &amp;#8212; as we&amp;#8217;ve seen, an iterator cannot remove itself. I took a look at a couple of implementations of this function to see how it&amp;#8217;s done.
&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s LLVM&amp;#8217;s libc++ &lt;a href="http://llvm.org/svn/llvm-project/libcxx/trunk/include/forward_list"&gt;implementation&lt;/a&gt;:
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;template &amp;lt;class _Tp, class _Alloc&amp;gt;
template &amp;lt;class _Predicate&amp;gt;
void
forward_list&amp;lt;_Tp, _Alloc&amp;gt;::remove_if(_Predicate __pred)
{
    iterator __e = end();
    for (iterator __i = before_begin(); __i.__ptr_-&amp;gt;__next_ != nullptr;)
    {
        if (__pred(__i.__ptr_-&amp;gt;__next_-&amp;gt;__value_))
        {
            iterator __j = _VSTD::next(__i, 2);
            for (; __j != __e &amp;amp;&amp;amp; __pred(*__j); ++__j)
                ;
            erase_after(__i, __j);
            if (__j == __e)
                break;
            __i = __j;
        }
        else
            ++__i;
    }
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;There&amp;#8217;s no need for any special treatment of the first list node here, since we have its predecessor, the &lt;code&gt;before_begin()&lt;/code&gt; node. The function does double back though, figuring out the next range to erase before going back to erase it &amp;#8212; and the erase function isn&amp;#8217;t pretty.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;template &amp;lt;class _Tp, class _Alloc&amp;gt;
typename forward_list&amp;lt;_Tp, _Alloc&amp;gt;::iterator
forward_list&amp;lt;_Tp, _Alloc&amp;gt;::erase_after(const_iterator __f, const_iterator __l)
{
    __node_pointer __e = const_cast&amp;lt;__node_pointer&amp;gt;(__l.__ptr_);
    if (__f != __l)
    {
        __node_pointer __p = const_cast&amp;lt;__node_pointer&amp;gt;(__f.__ptr_);
        __node_pointer __n = __p-&amp;gt;__next_;
        if (__n != __e)
        {
            __p-&amp;gt;__next_ = __e;
            __node_allocator&amp;amp; __a = base::__alloc();
            do
            {
                __p = __n-&amp;gt;__next_;
                __node_traits::destroy(__a, _VSTD::addressof(__n-&amp;gt;__value_));
                __node_traits::deallocate(__a, __n, 1);
                __n = __p;
            } while (__n != __e);
        }
    }
    return iterator(__e);
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For comparison, here&amp;#8217;s how GCC&amp;#8217;s &lt;a href="http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/include/bits/forward_list.tcc?view=markup"&gt;libstdc++ does the same thing&lt;/a&gt;.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;template&amp;lt;typename _Tp, typename _Alloc&amp;gt;
    template&amp;lt;typename _Pred&amp;gt;
      void
      forward_list&amp;lt;_Tp, _Alloc&amp;gt;::
      remove_if(_Pred __pred)
      {
	_Node* __curr = static_cast&amp;lt;_Node*&amp;gt;(&amp;amp;this-&amp;gt;_M_impl._M_head);
        while (_Node* __tmp = static_cast&amp;lt;_Node*&amp;gt;(__curr-&amp;gt;_M_next))
          {
            if (__pred(*__tmp-&amp;gt;_M_valptr()))
              this-&amp;gt;_M_erase_after(__curr);
            else
              __curr = static_cast&amp;lt;_Node*&amp;gt;(__curr-&amp;gt;_M_next);
          }
      }

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, erasing (after a) node reads:
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;template&amp;lt;typename _Tp, typename _Alloc&amp;gt;
    _Fwd_list_node_base*
    _Fwd_list_base&amp;lt;_Tp, _Alloc&amp;gt;::
    _M_erase_after(_Fwd_list_node_base* __pos)
    {
      _Node* __curr = static_cast&amp;lt;_Node*&amp;gt;(__pos-&amp;gt;_M_next);
      __pos-&amp;gt;_M_next = __curr-&amp;gt;_M_next;
      _Tp_alloc_type __a(_M_get_Node_allocator());
      allocator_traits&amp;lt;_Tp_alloc_type&amp;gt;::destroy(__a, __curr-&amp;gt;_M_valptr());
      __curr-&amp;gt;~_Node();
      _M_put_node(__curr);
      return __pos-&amp;gt;_M_next;
    }

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This version walks through the list removing nodes which match the predicate as it finds them. Don&amp;#8217;t be confused by &lt;code&gt;&amp;amp;this-&amp;gt;_M_impl._M_head&lt;/code&gt;: it&amp;#8217;s not the node at the head of the list, it&amp;#8217;s the one before.
&lt;/p&gt;

&lt;h3&gt;Lessons from C++&lt;/h3&gt;
&lt;p&gt;Maybe this code wouldn&amp;#8217;t persaude Linus Torvalds to rethink &lt;a href="http://harmful.cat-v.org/software/c++/linus" title="C++ is a horrible language"&gt;his opinion of C++&lt;/a&gt;, but if you can see past the angle brackets, underscores and allocators, it&amp;#8217;s simple enough.
&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s also:
&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;
     subtle, so I&amp;#8217;m glad someone else has written and checked it
 &lt;/li&gt;

 &lt;li&gt;
     generic, so I can put what I want in a list without casting or indirection
 &lt;/li&gt;

 &lt;li&gt;
     standard, so I know what to expect
 &lt;/li&gt;

 &lt;li&gt;
     supported
 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The before-begin node idea serves &lt;a href="http://wordaligned.org/articles/two-star-programming#comment-760751047"&gt;equally well in C&lt;/a&gt;, enabling list modifiers which have no need of double indirection or special case code for the list head.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;void remove_after(node * prev, remove_fn rm)
{
    while (prev-&amp;gt;next != NULL)
    {
        node * curr = prev-&amp;gt;next;
        if (rm(curr))
        {
            prev-&amp;gt;next = curr-&amp;gt;next;
            free(curr);
        }
        else
            prev = curr;
    }
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Pass this function the before-begin node to remove all items from the list which match the predicate. 
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/cTHkiZ0N1ls" height="1" width="1"/&gt;</description>
<dc:date>2013-02-07</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/singly-linked-lists-in-c++</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/cTHkiZ0N1ls/singly-linked-lists-in-c++</link>
<category>C</category>
<category>C++</category>
<category>Algorithms</category>
<feedburner:origLink>http://wordaligned.org/articles/singly-linked-lists-in-c++</feedburner:origLink></item>

<item>
<title>Folded files and rainbow code</title>
<description>&lt;p&gt;In one of my first programming jobs I worked at a software house which grew its own tools. We had a home made version control system, build system, test harness and programming language. We even had our own editor!
&lt;/p&gt;
&lt;p&gt;The language was C, lightly extended to support the primitive types of our particular problem domain. The editor was more esoteric. You drove it using the numeric keypad and it modeled source code as nested blocks:
&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;files contained blocks of:&lt;/li&gt;
&lt;ul&gt;&lt;li&gt;includes&lt;/li&gt;
&lt;li&gt;documentation&lt;/li&gt;
&lt;li&gt;data&lt;/li&gt;
&lt;li&gt;functions&lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;functions contained groups of:&lt;/li&gt;
&lt;ul&gt;&lt;li&gt;parameters:&lt;/li&gt;
&lt;ul&gt;&lt;li&gt;in parameters&lt;/li&gt;
&lt;li&gt;out parameters&lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;initialisation blocks&lt;/li&gt;
&lt;li&gt;assertions&lt;/li&gt;
&lt;li&gt;code blocks&lt;/li&gt;
&lt;li&gt;loops&lt;/li&gt;
&lt;li&gt;machine dependent sections&lt;/li&gt;
&lt;li&gt;returns&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;

&lt;p&gt;The editor facilitated navigation of this nested structure, with keypresses to &lt;a href="http://en.wikipedia.org/wiki/Code_folding"&gt;fold and unfold&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;You don&amp;#8217;t need to write your own editor to get the benefits of code folding: most editors support it to some degree. With this particular editor, however, folding reached its apotheosis. You could crimp and tuck and pleat, nesting layer upon layer within a single source file.
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/cobbphoto/4534419314/" title="Origami Dragon by g cobb, on Flickr"&gt;&lt;img src="http://farm5.staticflickr.com/4001/4534419314_705521d064.jpg" width="500" height="334" alt="Origami Dragon"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;span id="continue-reading"/&gt;

&lt;p&gt;The house editor wasn&amp;#8217;t mandated but the fold tokens it automatically placed in special comments made it challenging to work with anything else &lt;a id="fn1link" href="http://wordaligned.org/articles/folded-files-and-rainbow-code#fn1"&gt;&lt;sup&gt;[1]&lt;/sup&gt;&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;Folding made it easy to work with large files and long, complex functions. Looking back, I&amp;#8217;d say it made it &lt;strong&gt;too&lt;/strong&gt; easy &amp;#8212; I&amp;#8217;ve come to prefer other ways of organising code &amp;#8212; but at the time I saw things differently. We all did. What interests me now is the effect an editor has on the shape of your code.
&lt;/p&gt;
&lt;p&gt;A wide editing pane renders long lines comfortably. Code completion makes forgettable names workable. Smart indentation keeps your code legal even as you type. Cut and paste allows you to replicate an existing function and tweak it for a new use case. Undo, redo and autosave are like version control made simple.
&lt;/p&gt;
&lt;p&gt;The folding editor failed to seduce everyone. One colleague grumbled it lacked syntax highlighting. Well, it probably came from an age before coloured pixels, and, similarly dated, at the time, I couldn&amp;#8217;t see the need.
&lt;/p&gt;
&lt;p&gt;These days I find it hard to operate without syntax highlighting: code doesn&amp;#8217;t even look like code if imports, literals, comments etc. aren&amp;#8217;t visually distinct. Yet there are &lt;a href="http://en.wikipedia.org/wiki/Rob_Pike"&gt;elite programmers&lt;/a&gt; who write their own programming languages using their &lt;a href="http://acme.cat-v.org/faq"&gt;own editors&lt;/a&gt; who find syntax highlighting a distraction.
&lt;/p&gt;
&lt;iframe width="500" height="281" src="http://www.youtube.com/embed/dP1xVpMPn8M?rel=0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

&lt;p&gt;Walking round the office where I work I see a diversity of editors. There are split screens and floating windows, stacked up toolbars, icons piled high. Helicopter views expose the jagged outlines of source code seen from a distance. Lenses zoom in on regions of interest. Tokens glow like plankton in deep sea colour schemes.
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/76798465@N00/4169369269/" title="More phytoplankton zooplankton by willapalens, on Flickr"&gt;&lt;img src="http://farm3.staticflickr.com/2663/4169369269_ac9a8bf641.jpg" width="500" height="375" alt="More phytoplankton zooplankton"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s good practice to develop code on multiple platforms even if your product targets just one. Different compilers and hardware exercise your software in different ways, making it resilient and portable. I wonder too if multiple editors scrutinize your software from different perspectives, keeping it flexible and clean.
&lt;/p&gt;
&lt;p style="text-align:center"&gt;&amp;sect;&lt;/p&gt;

&lt;p&gt;My thanks to &lt;a href="http://www.flickr.com/photos/cobbphoto"&gt;Gary Cobb&lt;/a&gt; for permission to use his origami dragon snap, and to &lt;a href="http://www.flickr.com/photos/76798465@N00"&gt;willapalens&lt;/a&gt; for the plankton. The video clip is by &lt;a href="http://swtch.com/~rsc"&gt;Russ Cox&lt;/a&gt;, acme fanatic and &lt;a href="http://golang.org"&gt;go developer&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;&lt;hr /&gt;
   &lt;a id="fn1" href="http://wordaligned.org/articles/folded-files-and-rainbow-code#fn1link"&gt;[1]&lt;/a&gt;: Programmers relish challenges and one seasoned emacs user had put together an emacs mode to cope with the folds.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/9mAzS3be0jQ" height="1" width="1"/&gt;</description>
<dc:date>2013-01-23</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/folded-files-and-rainbow-code</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/9mAzS3be0jQ/folded-files-and-rainbow-code</link>
<category>Editors</category>
<category>Syntax</category>
<feedburner:origLink>http://wordaligned.org/articles/folded-files-and-rainbow-code</feedburner:origLink></item>

<item>
<title>C++ Concurrency in Action</title>
<description>&lt;p&gt;&lt;span style="text-align:center;font-size:72px;color:gold;"&gt;&amp;#9733;&amp;#9733;&amp;#9733;&amp;#9733;&amp;#9733;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.amazon.co.uk/C-Concurrency-Action-Practical-Multithreading/dp/1933988770" title="C++ Concurrency in Action, on Amazon"&gt;C++ Concurrency in Action&lt;/a&gt; is an excellent book. You should buy it if you want to use the support for concurrency added by the new C++ standard, C++11; and if you&amp;#8217;re using C++11 you&amp;#8217;ll deepen your understanding of the various language enhancements and how they work together.
&lt;/p&gt;
&lt;div class="amazon"&gt;&lt;a href="http://www.amazon.co.uk/C-Concurrency-Action-Practical-Multithreading/dp/1933988770"&gt;&lt;img src="http://www.justsoftwaresolutions.co.uk/images/ccia.jpg" alt="C++ Concurrency in Action cover"&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Who&amp;#8217;s the author? What makes him qualified to write this book?
&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;a href="http://www.justsoftwaresolutions.co.uk/" title="Anthony Willams' website"&gt;Anthony Williams&lt;/a&gt; is a UK-based developer and consultant with many years experience in C++. He has been an active member of the BSI C++ Standards Panel since 2001, and is author or coauthor of many of the C++ Standards Committee papers that led up to the inclusion of the thread library in the new C++ Standard, known as C++11 or C++0x. He has been the maintainer of the Boost Thread library since 2006, and is the developer of the &lt;a href="http://www.stdthread.co.uk/" title="just::thread, a C++ Standard Thread Library implementation"&gt;just::thread&lt;/a&gt; implementation of the C++11 thread library from Just Software Solutions Ltd. Anthony lives in the far west of Cornwall, England. &amp;#8212; &lt;a href="http://www.manning.com/williams/" title="Williams' author page on Manning website"&gt;About the Author, Manning website&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;It&amp;#8217;s clear the experience of writing papers for the standards committee has paid off. The book is well organised and clearly written. Accurate and thorough, it&amp;#8217;s also a pleasure to read. The examples are practical, and range from launching threads through to lock-free message queues. The largest case study &amp;#8212; a message passing framework and an ATM application built on this framework &amp;#8212; shows the expert use of modern C++ to write elegant and compact code.
&lt;/p&gt;
&lt;p&gt;The clarity of the text is matched by the book&amp;#8217;s clean and functional design. It looks good. I bought the dead-tree version which gave me free access to the ebook and I&amp;#8217;ve made use of both formats.
&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m new to C++11 and compilers are still catching up with the standard. This book is steeped in C++11. Reading through it, I came to realise that a close look at the standard thread library helps explain the evolution of the language as a whole: so &lt;strong&gt;that&amp;#8217;s&lt;/strong&gt; why variadic templates are needed, and move semantics work &lt;strong&gt;there&lt;/strong&gt;, and &amp;#8212; &lt;strong&gt;I get it!&lt;/strong&gt; &amp;#8212; lambda functions fit nicely with condition variables.
&lt;/p&gt;
&lt;p&gt;Recommended++
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/_lzk-qV4MQg" height="1" width="1"/&gt;</description>
<dc:date>2013-01-21</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/c++-concurrency-in-action</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/_lzk-qV4MQg/c++-concurrency-in-action</link>
<category>C++</category>
<category>Reviews</category>
<feedburner:origLink>http://wordaligned.org/articles/c++-concurrency-in-action</feedburner:origLink></item>

<item>
<title>Python’s lesser known loop control</title>
<description>&lt;p&gt;I&amp;#8217;ll break out of a loop if I have to but generally prefer to recast code so no &lt;code&gt;break&lt;/code&gt; is needed. It&amp;#8217;s not about avoiding the keyword; but rather that the loop control expression should tell readers when and why the loop exits.
&lt;/p&gt;
&lt;p&gt;In C and C++ such recasting is rarely a problem. Python separates statements and expressions which makes things more difficult. You can&amp;#8217;t assign to a variable in a loop control expression, for example. Consider a function which processes a file one chunk at a time, until the file is exhausted.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;while True:
    data = fp.read(4096)
    if not data:
        break
    ...

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The control expression, &lt;code&gt;while True&lt;/code&gt;, suggests an infinite loop, which isn&amp;#8217;t what actually happens, but readers must poke around in the loop body to find the actual termination condition.
&lt;/p&gt;
&lt;p&gt;As already mentioned, an assignment statement isn&amp;#8217;t an expression, so we can&amp;#8217;t write this:
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;Syntax error!&lt;/div&gt;

&lt;pre class="prettyprint"&gt;while data = fp.read(4096):
    ...

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You could implement a file reader &lt;a href="http://docs.python.org/3/howto/functional.html#generators"&gt;generator function&lt;/a&gt; which yields chunks of data, allowing clients to write:
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;for data in chunked_file_reader(fp):
    ...

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This at least localises the problem to &lt;code&gt;chunked_file_reader()&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;Another solution is to use the two argument flavour of &lt;a href="http://docs.python.org/3.3/library/functions.html#iter"&gt;iter&lt;/a&gt;, &lt;code&gt;iter(object, sentinel)&lt;/code&gt;. Here, &lt;code&gt;object&lt;/code&gt; is a callable and &lt;code&gt;sentinel&lt;/code&gt; is a terminal value. &lt;code&gt;Object&lt;/code&gt; is called with no arguments: use &lt;code&gt;&lt;a href="http://docs.python.org/3/library/functools.html#functools.partial"&gt;functools.partial&lt;/a&gt;&lt;/code&gt; to set the chunk size passed to &lt;code&gt;file.read&lt;/code&gt;; and stop when this function returns the empty string.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;import functools

chunked_file_reader = functools.partial(fp.read, 4096)

for data in iter(chunked_file_reader, ''):
    ...

&lt;/pre&gt;

&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/dINHq8QCx0s" height="1" width="1"/&gt;</description>
<dc:date>2013-01-14</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/pythons-lesser-known-loop-control</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/dINHq8QCx0s/pythons-lesser-known-loop-control</link>
<category>Python</category>
<feedburner:origLink>http://wordaligned.org/articles/pythons-lesser-known-loop-control</feedburner:origLink></item>

<item>
<title>Two star programming</title>
<description>&lt;p&gt;A few weeks ago &lt;a href="http://meta.slashdot.org/story/12/10/11/0030249/linus-torvalds-answers-your-questions"&gt;Linus Torvalds answered some questions&lt;/a&gt; on slashdot. All his responses make good reading but one in particular caught my eye. Asked to describe his favourite kernel hack, Torvalds grumbles he rarely looks at code these days &amp;#8212; unless it&amp;#8217;s to sort out someone else&amp;#8217;s mess. He then pauses to admit he&amp;#8217;s proud of the kernel&amp;#8217;s fiendishly cunning filename lookup cache before continuing to moan about incompetence.
&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;At the opposite end of the spectrum, I actually wish more people understood the really core low-level kind of coding. Not big, complex stuff like the lockless name lookup, but simply good use of pointers-to-pointers etc. For example, I&amp;#8217;ve seen too many people who delete a singly-linked list entry by keeping track of the &lt;code&gt;prev&lt;/code&gt; entry, and then to delete the entry, doing something like
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if (prev)
    prev-&amp;gt;next = entry-&amp;gt;next;
else
    list_head = entry-&amp;gt;next;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and whenever I see code like that, I just go &amp;#8220;This person doesn&amp;#8217;t understand pointers&amp;#8221;. And it&amp;#8217;s sadly quite common.
&lt;/p&gt;
&lt;p&gt;People who understand pointers just use a &amp;#8220;pointer to the entry pointer&amp;#8221;, and initialize that with the address of the list_head. And then as they traverse the list, they can remove the entry without using any conditionals, by just doing a &lt;code&gt;*pp = entry-&amp;gt;next&lt;/code&gt;.
&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Well I &lt;em&gt;thought&lt;/em&gt; I understood pointers but, sad to say, if asked to implement a list removal function I too would have kept track of the previous list node. Here&amp;#8217;s a sketch of the code:
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;This person doesn&amp;#8217;t understand pointers&lt;/div&gt;

&lt;pre class="prettyprint"&gt;typedef struct node
{
    struct node * next;
    ....
} node;

typedef bool (* remove_fn)(node const * v);

// Remove all nodes from the supplied list for which the 
// supplied remove function returns true.
// Returns the new head of the list.
node * remove_if(node * head, remove_fn rm)
{
    for (node * prev = NULL, * curr = head; curr != NULL; )
    {
        node * const next = curr-&amp;gt;next;
        if (rm(curr))
        {
            if (prev)
                prev-&amp;gt;next = next;
            else
                head = next;
            free(curr);
        }
        else
            prev = curr;
        curr = next;
    }
    return head;
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The linked list is a simple but perfectly-formed structure built from nothing more than a pointer-per-node and a sentinel value, but the code to modify such lists can be subtle. No wonder linked lists feature in so many &lt;a href="https://www.google.com/search?q=linked+list+interview+questions" title="Search for linked list interview questions"&gt;interview questions&lt;/a&gt;!
&lt;/p&gt;
&lt;p&gt;The subtlety in the implementation shown above is the conditional required to handle any nodes removed from the head of the list.
&lt;/p&gt;
&lt;p&gt;Now let&amp;#8217;s look at the implementation Linus Torvalds had in mind. In this case we pass in a pointer to the list head, and the list traversal and modification is done using a pointer to the next pointers.
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;Two star programming&lt;/div&gt;

&lt;pre class="prettyprint"&gt;void remove_if(node ** head, remove_fn rm)
{
    for (node** curr = head; *curr; )
    {
        node * entry = *curr;
        if (rm(entry))
        {
            *curr = entry-&amp;gt;next;
            free(entry);
        }
        else
            curr = &amp;amp;entry-&amp;gt;next;
    }
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Much better! The key insight is that the links in a linked list are pointers and so &lt;strong&gt;pointers to pointers&lt;/strong&gt; are the prime candidates for modifying such a list.
&lt;/p&gt;
&lt;p style="text-align:center"&gt;&amp;sect;&lt;/p&gt;

&lt;p&gt;The improved version of &lt;code&gt;remove_if()&lt;/code&gt; is an example of two star programming: the doubled-up asterisks indicate two levels of indirection. A &lt;a href="http://c2.com/cgi/wiki?ThreeStarProgrammer"&gt;third star&lt;/a&gt; would be one too many.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/b4N5BTrsOf4" height="1" width="1"/&gt;</description>
<dc:date>2013-01-08</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/two-star-programming</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/b4N5BTrsOf4/two-star-programming</link>
<category>C</category>
<category>Torvalds</category>
<category>Algorithms</category>
<feedburner:origLink>http://wordaligned.org/articles/two-star-programming</feedburner:origLink></item>

<item>
<title>ACCU Bristol and Bath</title>
<description>&lt;p&gt;Lightning talks. In a pub. Me first! I hadn&amp;#8217;t actually practised but I knew what I wanted to say and had picked a subject so trivial I couldn&amp;#8217;t possibly overrun.
&lt;/p&gt;
&lt;p&gt;Yes, it was time, at last, for the first &lt;a href="https://twitter.com/accuBristol" title="Follow ACCU Bristol &amp;amp; Bath on twitter"&gt;ACCU Bristol &amp;amp; Bath&lt;/a&gt; meeting, to be held in an upstairs room at the Cornubia. We&amp;#8217;d reconnoitred the venue a few weeks earlier. Although the room was dingy and we couldn&amp;#8217;t work out where to put a screen, and despite disturbance from the increasingly raucous CAMRA meeting next door, the location was ideal and the beer superb. I looked forward to returning.
&lt;/p&gt;
&lt;p&gt;Plans change. In an agile last minute switch the meeting relocated to the Marriot &amp;#8212; which, coincidentally, had just been announced as the host of next year&amp;#8217;s &lt;a href="http://accu.org/index.php/conferences" title="ACCU 2013 comes to Bristol!"&gt;ACCU conference&lt;/a&gt;. I shuffled through revolving doors into the hotel&amp;#8217;s vacant lobby rehearsing my talk in my head. Where was everyone? It took some backtracking and interrogation to locate the subterranean room but fortunately they hadn&amp;#8217;t started without me.
&lt;/p&gt;
&lt;p&gt;Now &lt;strong&gt;this&lt;/strong&gt; was a proper meeting room. Panelled walls, no windows. A blank TV screen; green apples; red glasses; bottled water.
&lt;/p&gt;
&lt;img src="http://wordaligned.org/images/apple-display-adapter.jpg" alt="I carry one with me at all times"/&gt;

&lt;p&gt;Ewan welcomed me. &amp;#8220;Have you got a macbook display adapter?&amp;#8221;
&lt;/p&gt;
&lt;p&gt;No. I didn&amp;#8217;t even have the slides to my own presentation &amp;#8212; I&amp;#8217;d emailed them ahead to be merged into a single deck.
&lt;/p&gt;
&lt;p&gt;The screen flicked to life. Nine talks, five minutes each. We&amp;#8217;d be done in an hour. After a brief welcome my slides were on screen and I was off.
&lt;/p&gt;
&lt;p&gt;Unfortunately I ran out of time, laughing too long at my own lightning anecdote which framed a talk about ellipses, the triple-dots &amp;#8230; which mean different things in different places in different programming languages. Next up was Dan Towner who walked us through the algorithm used by compilers for allocating registers. It&amp;#8217;s a greedy colouring of a planar map, he said, wrapped in a bail-and-retry loop. Dan Tallis spoke about the single committer model which works so well on open source projects. Developers don&amp;#8217;t have write access to the repository and must submit patches to the committer for review, a protocol which encourages incremental and considered changes to a codebase. &lt;a href="http://curbralan.com"&gt;Kevlin Henney&lt;/a&gt; needed just a single slide to clear up some misconceptions in exactly five minutes. Chris Simons didn&amp;#8217;t need any slides to describe where designs come from. Pacing the floor and waving his fingers, he explained that &lt;span /&gt;computer systems are punchlines; design is a matter of figuring out the joke. Attack the solution space with ants! No ACCU meeting would be complete without a discourse on C++ test frameworks and Malcolm Noyes duly dazzled us developing a C++ mocking library before our very eyes. Jim Thomson compared before and after binaries to prove his source code rearrangements hadn&amp;#8217;t caused any damage. Ewan Milne, who&amp;#8217;d not only organised and chaired the meeting, also contributed a talk on (guess what?) planning, subtitled how agile can Kanban be (say it!)
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.jaggersoft.com"&gt;Jon Jagger&lt;/a&gt; postponed his closing talk. Macs just work if you&amp;#8217;ve got the right connectors. We hadn&amp;#8217;t. The audience wanted more but that&amp;#8217;s no bad thing. We regathered in the hotel bar to crunch apples and chew over the evening. The ACCU Bristol &amp;amp; Bath launch had been a success! The price of a pint and anodyne surroundings discouraged lingering. We drank up and headed off towards trains, homes, and, for a select few, the &lt;a href="http://www.travelswithbeer.com/2010/07/09/the-cornubia-bristol/" title="CAMRA members can get pints at a reduced prices depending on the strength of the ale"&gt;Cornubia&lt;/a&gt;.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/9gFxCwgsZ2g" height="1" width="1"/&gt;</description>
<dc:date>2012-08-31</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/accu-bristol-and-bath</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/9gFxCwgsZ2g/accu-bristol-and-bath</link>
<category>ACCU</category>
<category>Self</category>
<feedburner:origLink>http://wordaligned.org/articles/accu-bristol-and-bath</feedburner:origLink></item>

<item>
<title>Life goes on</title>
<description>&lt;p&gt;&lt;a href="http://www.flickr.com/photos/anachrocomputer/6951474448/" title="Testing the Wincor-Nixdorf Customer Display by anachrocomputer, on Flickr"&gt;&lt;img src="http://farm6.staticflickr.com/5340/6951474448_f83069b7cf.jpg" width="500" height="333" alt="Testing the Wincor-Nixdorf Customer Display"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;In my &lt;a href="http://wordaligned.org/articles/life-on-canvas" title="Game of life"&gt;previous post&lt;/a&gt; I described John Conway&amp;#8217;s Game of Life as a &lt;a href="http://programmer.97things.oreilly.com/wiki/index.php/Learn_to_say_Hello_World" title="Learn to say 'Hello, World'"&gt;hello world&lt;/a&gt; for computer graphics. Actually, it goes beyond that. Hello world is a complete program, a proof the toolchain works, but it&amp;#8217;s not a particularly interesting program. An implementation of the game of life does more than demonstrate you can put pixels on screens: the evolution of those pixel colonies turns out to be a subject &lt;a href="http://pentadecathlon.com/lifeNews/2010/02/prime_numbers.html" title="A Life pattern to calculate prime numbers"&gt;worth studying&lt;/a&gt; in itself.
&lt;/p&gt;
&lt;p&gt;That said, I&amp;#8217;m primarily interested in Life as an exercise in learning new things. I&amp;#8217;ve continued to develop my &lt;a href="http://diveintohtml5.info/canvas.html" title="Mark Pilgrim dives into HTML5's canvas element"&gt;canvas&lt;/a&gt; implementation, adding some &lt;a href="http://jqueryui.com" title="The official jQuery user interface library"&gt;jQuery UI&lt;/a&gt; effects. The code is on &lt;a href="https://github.com/wordaligned/game-of-life.git" title="Wordaligned's game-of-life repository on Github"&gt;Github&lt;/a&gt; &amp;#8212; yes, it&amp;#8217;s high time I learned about that too &amp;#8212; though note there are dependencies on jQuery, jQuery UI, Imagemagick, and on pattern files downloaded from &lt;a href="http://www.conwaylife.com/wiki/Main_Page"&gt;conwaylife.com&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;A working version can be found at &lt;a href="http://wordaligned.org/life" title="Life on Canvas"&gt;wordaligned.org/life&lt;/a&gt;. Click the graphic below to give it a go. Your web client does support HTML5 Canvas, right?
&lt;/p&gt;
&lt;div style="border:1px solid #333"&gt;&lt;a href="http://wordaligned.org/life" title="Play the game of life"&gt;&lt;img src="http://wordaligned.org/images/life-on-canvas.png" alt="Play the game of life"/&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;&lt;hr /&gt;
   Thanks, &lt;a href="http://www.flickr.com/photos/anachrocomputer" title="anachrocomputer's photostream"&gt;anachrocomputer&lt;/a&gt;, for the delightful hello world photo
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/gBwrJErT7Jw" height="1" width="1"/&gt;</description>
<dc:date>2012-06-29</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/life-goes-on</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/gBwrJErT7Jw/life-goes-on</link>
<category>Algorithms</category>
<category>Graphics</category>
<feedburner:origLink>http://wordaligned.org/articles/life-goes-on</feedburner:origLink></item>

<item>
<title>Life on Canvas</title>
<description>&lt;p&gt;I was lucky enough to be taught maths by &lt;a href="http://www.math.princeton.edu/directory/john-conway" title="John Conway, now at Princeton University"&gt;John Horton Conway&lt;/a&gt;, if only for an hour a week for a single term. He was lucky enough to be teaching number theory: a subject packed with treasures picked from the full history of mathematics.
&lt;/p&gt;
&lt;p&gt;I remember him as animated and unkempt. He bustled into that first lecture with a carrier bag and started pulling out groceries one by one. How many items were in the bag, he wondered? Could he count them all &amp;#8212; it was a &lt;em&gt;very&lt;/em&gt; large bag &amp;#8212; and what exactly did &lt;a href="http://mathworld.wolfram.com/Aleph-0.html" title="The 'small' infinite set of integers"&gt;infinity&lt;/a&gt; mean? Later I would see him pacing along Kings Parade dragging a shopping tolley, lost in thought.
&lt;/p&gt;
&lt;p&gt;Number theory may be as old as mathematics but it remains very much alive. Some 40 years ago Professor Conway discovered a primitive and novel mathematical life form: the Game of Life. 
&lt;/p&gt;
&lt;p&gt;The rules are simple. A colony of cells inhabits a two dimensional grid. At any one time each cell is either alive or dead, and as time advances the fate of a cell is determined entirely by its immediate 8 neighbours:
&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;
     reproduction: a dead cell with exactly 3 live neighbours becomes alive
 &lt;/li&gt;

 &lt;li&gt;
     survival: a live cell with 2 or 3 live neighbours lives on
 &lt;/li&gt;

 &lt;li&gt;
     overcrowding or loneliness: in all other cases the cell dies or stays dead
 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In code, we might represent the colony as a two dimensional array filled with &lt;code&gt;1&lt;/code&gt;&amp;#8217;s and &lt;code&gt;0&lt;/code&gt;&amp;#8217;s for live and dead cells and code up the rules like this:
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;// Determine the fate of cell i, j in the next generation.
// Return 1 for "lives", 0 for "dies"
function fate(cells, i, j) {
    var neighbours = [[-1,-1],[-1,0],[-1,1],
                      [0,-1],        [0,1],
                      [1,-1], [1,0], [1,1]];
        live_neighbours = 0;
    neighbours.forEach(function(n) { 
        live_neighbours += cells[i + n[0]][j + n[1]];
    });
    return (live_neighbours == 3 || 
            cells[i][j] == 1 &amp;amp;&amp;amp; live_neighbours == 2) ? 1 : 0;
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It turns out these simple rules generate an astonishing ecosystem. Simple patterns flourish, evolving into complex patterns which, many generations later may decay into stable forms and repeating patterns, or, occasionally, become extinct. 
&lt;/p&gt;
&lt;p&gt;Can a finite pattern grow indefinitely? Conway originally conjectured no, this was impossible, offering $50 to the first person who could prove or disprove the his conjecture before the end of 1970. In November of that year a team from MIT led by Bill Gosper claimed the prize, disproving the conjecture with a glider gun pattern which spits out a new spaceship every 30 generations.
&lt;/p&gt;
&lt;img src=http://upload.wikimedia.org/wikipedia/commons/e/e5/Gospers_glider_gun.gif alt="Gosper's glider gun, Wikimedia commons" style="border:1px solid black;"&gt;

&lt;span id="continue-reading"/&gt;

&lt;p&gt;A 2 dimensional array can naturally be represented on the most basic computer screen &amp;#8212; think raster graphics, pixels &amp;#8212; and the parallel emergence and development of the personal computer helps explain life&amp;#8217;s continuing appeal&lt;a id="fn1link" href="http://wordaligned.org/articles/life-on-canvas#fn1"&gt;&lt;sup&gt;[1]&lt;/sup&gt;&lt;/a&gt;. The game of life has become the &lt;a href="http://programmer.97things.oreilly.com/wiki/index.php/Learn_to_Say_%22Hello%2C_World%22" title="Laern to say Hello, world"&gt;hello world&lt;/a&gt; of graphics frameworks. In 2012 we can use the HTML5 canvas, a 2 dimensional surface for drawing on.
&lt;/p&gt;
&lt;p&gt;&lt;canvas id="lifecanvas" width="480" height="320" style="border:1px solid black;"&gt;Unfortunately your client does not support the &lt;a href="http://www.w3.org/TR/html5/the-canvas-element.html#the-canvas-element"&gt;HTML5 canvas element&lt;/a&gt;. If you are using a feed reader, try visiting the &lt;a href="http://wordaligned.org/articles/life-on-canvas"&gt;original page&lt;/a&gt;.&lt;/canvas&gt;
   &lt;div&gt;
   &lt;button type="button" id="random" title="Random pattern" style="vertical-align:top;width:50px;height:50px;"&gt;&lt;img src="http://wordaligned.org/images/glyphicons_322_playing_dices.png" /&gt;&lt;/button&gt;
   &lt;button type="button" id="clear" title="Clear screen" style="vertical-align:top;width:50px;height:50px;"&gt;&lt;img src="http://wordaligned.org/images/glyphicons_192_circle_remove.png" /&gt;&lt;/button&gt;
   &lt;button type="button" id="toggle" title="Play/Pause" style="vertical-align:top;width:50px;height:50px;"&gt;&lt;img src="http://wordaligned.org/images/glyphicons_174_pause.png" /&gt;&lt;/button&gt;
   &lt;button type="button" id="step" title="Single step" style="vertical-align:top;width:50px;height:50px;"&gt;&lt;img src="http://wordaligned.org/images/glyphicons_178_step_forward.png" /&gt;&lt;/button&gt;
   &lt;button type="button" id="info" title="Help" style="vertical-align:top;width:50px;height:50px;"&gt;&lt;img src="http://wordaligned.org/images/glyphicons_195_circle_info.png" /&gt;&lt;/button&gt;
   &lt;/div&gt;
   &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"&gt;&lt;/script&gt;
   &lt;script type="text/javascript" src="http://wordaligned.org/scripts/simple-life.js"&gt;&lt;/script&gt;
&lt;/p&gt;
&lt;p&gt;After a little experimentation you&amp;#8217;re sure to uncover life&amp;#8217;s three main domains: still lives, which remain unchanged; oscillators, which repeat; and spaceships, which move across the board. Then there are methuselahs, puffers, guns &amp;#8230;
&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s a pulsar, an oscillator with period 3.
&lt;/p&gt;
&lt;img src="http://wordaligned.org/images/pulsar.gif" alt="Pulsar"/&gt;

&lt;p&gt;And here&amp;#8217;s a glider, classed as a diagonal spaceship, &lt;a href="http://www.catb.org/hacker-emblem/" title="When you put the glider emblem on your web page, or wear it on clothing, or display it in some other way, you are visibly associating yourself with the hacker culture"&gt;a pattern chosen&lt;/a&gt; by Eric S Raymond as a hackers&amp;#8217; emblem.
&lt;/p&gt;
&lt;img src="http://wordaligned.org/images/glider.gif" alt="Glider, an orthogonal spaceship"/&gt;

&lt;p&gt;Here&amp;#8217;s another 5 cell pattern &amp;#8212; the R-pentomino. Clear the canvas, pick out an R-pentomino cluster of cells with the mouse, click play, and watch what evolves!
&lt;/p&gt;
&lt;img src="http://wordaligned.org/images/Rpentomino.png" alt="Rpentomino"/&gt;

&lt;p&gt;&lt;a href="http://www.conwaylife.com/wiki/Turtle" title="An orthogonal c/3 spaceship"&gt;Turtle&lt;/a&gt;, &lt;a href="http://www.conwaylife.com/wiki/Garden_of_Eden" title="A pattern that has no parents and thus can only occur in generation 0"&gt;garden&lt;/a&gt;, &lt;a href="http://www.conwaylife.com/wiki/Unicycle" title="Unicycle is a period 6 oscillator made up of four copies of unix"&gt;unicycle&lt;/a&gt;: animal, vegetable, mineral. The kingdom of life is inclusive and extensive, a delight for naturalists and taxonomists alike. It&amp;#8217;s seduced logophiles and lexicographers too: reading through Stephen A. Silver&amp;#8217;s comprehensive &lt;a href="http://www.argentum.freeserve.co.uk/lex.htm" title="A lexicon of terms relating to John Horton Conway's Game of Life, by Stephen Silver"&gt;Life Lexicon&lt;/a&gt;, I learned that an anteater consumes ants, a &lt;a href="http://www.argentum.freeserve.co.uk/lex_c.htm#caterpillar"&gt;caterpillar&lt;/a&gt; works by laying tracks at its front end, a baker&amp;#8217;s dozen is a loaf &lt;a href="http://www.argentum.freeserve.co.uk/lex_h.htm#hassler"&gt;hassled&lt;/a&gt; by two blocks and two caterers, and an &lt;a href="http://www.argentum.freeserve.co.uk/lex_a.htm#ak47reaction"&gt;AK47&lt;/a&gt; reaction occurs when a honey farm predecessor, catalysed by an eater and a block, reappears at another location 47 generations later, having produced a glider and a traffic light. I could go on&amp;#8230;
&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;a id="fn1" href="http://wordaligned.org/articles/life-on-canvas#fn1link"&gt;[1]&lt;/a&gt;: I&amp;#8217;m glossing over details here. The true game of life is played out on an infinite grid, and patterns such as &lt;a href="http://en.wikipedia.org/wiki/Gun_(cellular_automaton)"&gt;Gosper&amp;#8217;s glider gun&lt;/a&gt; show that it really does have to be infinite. Computers have trouble with such an unconstrained requirement, and a computer animation might try and adapt the screen as the pattern grows, or limit the region of interest. Another approach is to wrap the canvas, left to right, top to bottom, so the game of life is played out on what&amp;#8217;s topologically a toriodal surface. That&amp;#8217;s what the canvas shown here does.
&lt;/p&gt;
&lt;p style="text-align:center"&gt;&amp;sect;&lt;/p&gt;

&lt;p&gt;The icons used on this page were downloaded from &lt;a href="http://glyphicons.com"&gt;http://glyphicons.com&lt;/a&gt; and are licensed under the &lt;a href=http://creativecommons.org/licenses/by/3.0/deed.en&gt;CC BY 3.0&lt;/a&gt; terms. The animated gifs were placed in the public domain by their authors.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/f_nT9JImkiQ" height="1" width="1"/&gt;</description>
<dc:date>2012-06-27</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/life-on-canvas</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/f_nT9JImkiQ/life-on-canvas</link>
<category>Algorithms</category>
<category>Graphics</category>
<category>Self</category>
<feedburner:origLink>http://wordaligned.org/articles/life-on-canvas</feedburner:origLink></item>

<item>
<title>Desktop preferences</title>
<description>&lt;p&gt;Paul grumbled about the lack of new content on &lt;a href="http://wordaligned.org" title="Once popular, now dormant programming blog"&gt;wordaligned.org&lt;/a&gt; and since he&amp;#8217;d just bought me lunch I thought I&amp;#8217;d better do something about it. He also said he didn&amp;#8217;t like the stuff about &lt;a href="http://wordaligned.org/tag/algorithms" title="Wordaligned articles about algorithms"&gt;algorithms&lt;/a&gt; and maths. Well, that makes my life easier too.
&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m moving jobs &amp;#8212; the &lt;a href="http://www.wagamama.com/locations/showlocation/555/" title="Yaki Udon, fat noodles, filling"&gt;lunch&lt;/a&gt; he paid for was a leaving do &amp;#8212; and my employers kindly allowed me to buy this macbook pro at a reasonable price. Of course I had to wipe and reinstall it. It took an hour and a half to fill the hard drive with zeros and a similar amount of time to load a few ones back on&lt;a id="fn1link" href="http://wordaligned.org/articles/desktop-preferences#fn1"&gt;&lt;sup&gt;[1]&lt;/sup&gt;&lt;/a&gt;.
&lt;/p&gt;
&lt;img src="http://upload.wikimedia.org/wikipedia/en/0/07/Itchyscratchy.png" alt="Itchy" /&gt;

&lt;p&gt;Installation done, I set about customisation. The official term is &lt;a href="http://support.apple.com/kb/HT2490" title="Set your preferences"&gt;preferences&lt;/a&gt; but &lt;strong&gt;annoyances&lt;/strong&gt; would be more accurate: you fix things which irk you before they cause real pain or, worse, you become immune to them. Typing hurt until I set key repeat to the max. Thomas Guest&amp;#8217;s macbook pro is a lousy name. I&amp;#8217;ve used &lt;a href="http://wordaligned.org/articles/oberon-cromarty-lisa-waggledance-ariel" title="Computer names"&gt;Itchy&lt;/a&gt; before and I like it so I&amp;#8217;m using it again. I booted Safari off the dock and stood Chrome in its place. I undocked some other things which I didn&amp;#8217;t recognise by their icons and were obviously of no use to me. By now I&amp;#8217;d seen the galactic login backdrop too many times. I&amp;#8217;ve never found a customisable preference for it but &lt;a href="https://www.google.co.uk/search?q=os+x+change+login+background" title="2nd hit worked for me"&gt;google&lt;/a&gt; told me what to do.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;$ sudo cp Downloads/matt-burns-pylon.jpg \
          /System/Library/CoreServices/DefaultDesktop.jpg

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="http://www.flickr.com/photos/floater81/4433735546/" title="IMG_3738 by mattburns.co.uk, on Flickr"&gt;&lt;img src="http://farm5.staticflickr.com/4050/4433735546_42b2bf1ce7.jpg" width="500" height="333" alt="IMG_3738"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;Actually, that&amp;#8217;s pretty much all I needed to fix. Now it really was about preferences I prefer. Aquamacs. Skype. I chose two pictures for my desktop backgrounds. What you want on a desktop &amp;#8212; no, what &lt;strong&gt;I&lt;/strong&gt; want on &lt;strong&gt;my&lt;/strong&gt; desktop &amp;#8212; is something gorgeous but not distracting, something personal but not too personal, something which works in widescreen, something of a &lt;a href="http://www.flickr.com/photos/rbrwr/308905101" title="Too grainy for my desktop"&gt;high enough resolution&lt;/a&gt;, something which &lt;a href="http://www.flickr.com/photos/thomasguest/5017113982" title="Which sadly rules out this picture, my favourite of the sketches I made at Bristol museum"&gt;doesn&amp;#8217;t get spoiled&lt;/a&gt; when application windows are slathered over it. I went with a couple more &lt;a href="http://www.flickr.com/photos/floater81/4432979527/in/photostream" title="Superb pictures of the second severn crossing"&gt;Burnsy&amp;#8217;s&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/floater81/4432979527/" title="The Second Severn Crossing by mattburns.co.uk, on Flickr"&gt;&lt;img src="http://farm3.staticflickr.com/2723/4432979527_aaac753da6.jpg" width="500" height="333" alt="The Second Severn Crossing"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;I remember the second Severn crossing being built. I&amp;#8217;d just moved to Bristol, joining a company which did virtual reality. The hardware was too expensive and the software too slow but we were a good team. Summer lunchtimes, we would play frisbee on the grassy hill by the Almondsbury garden centre. You could see the estuary, the new concrete pylons stepping into it, and, rising to the East, the towers of the original suspension bridge. Anyone who cycles around Bristol knows this view well. Interval training with &lt;a href="https://twitter.com/drbyrne255/status/187257262748876800"&gt;Owen&lt;/a&gt;, we spin out along Passage Road and catch our breath before turning back.
&lt;/p&gt;
&lt;iframe width="540" height="256" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.co.uk/maps?f=q&amp;amp;source=embed&amp;amp;hl=en&amp;amp;geocode=&amp;amp;q=aust&amp;amp;aq=&amp;amp;sll=51.587465,-4.195722&amp;amp;sspn=0.019305,0.035963&amp;amp;ie=UTF8&amp;amp;hq=&amp;amp;hnear=Aust,+South+Gloucestershire,+United+Kingdom&amp;amp;t=m&amp;amp;layer=c&amp;amp;cbll=51.593919,-2.632873&amp;amp;panoid=b9ap7_VnelTDzEUhtGFcPQ&amp;amp;cbp=13,259.11,,0,0&amp;amp;ll=51.590016,-2.632856&amp;amp;spn=0.013651,0.046349&amp;amp;z=14&amp;amp;output=svembed"&gt;&lt;/iframe&gt;

&lt;p&gt;More recently I&amp;#8217;ve moved to Swansea but kept my job in Bristol, crossing from and to Wales &lt;a href="http://en.wikipedia.org/wiki/Severn_Tunnel" title="The tunnel was built by the Great Western Railway between 1873 and 1886"&gt;under the river&lt;/a&gt; twice a week, Tuesdays and Thursdays, working from home on Mondays and Fridays. Now that too has come to an end. We were a good team but &amp;#8230; anyway. My new job will be home-based. Next I&amp;#8217;ll be partitioning the hard drive and booting into Linux.
&lt;/p&gt;
&lt;p style="text-align:center"&gt;&amp;sect;&lt;/p&gt;

&lt;p&gt;My thanks, again, to &lt;a href="http://www.mattburns.co.uk/mattburns.html"&gt;Matt Burns&lt;/a&gt; for allowing me to use his superb photos.
&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;a id="fn1" href="http://wordaligned.org/articles/desktop-preferences#fn1link"&gt;[1]&lt;/a&gt; Which reminds me, I should write about a cunning data structure which lazily initialises a sparse vector. &lt;a href="http://wordaligned.org" title="Once popular, now dormant programming blog"&gt;Wordaligned.org&lt;/a&gt; &lt;strong&gt;is&lt;/strong&gt; a programming blog, honest.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/8QtqVg8k7Yc" height="1" width="1"/&gt;</description>
<dc:date>2012-06-21</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/desktop-preferences</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/8QtqVg8k7Yc/desktop-preferences</link>
<category>Apple</category>
<category>Self</category>
<category>Pictures</category>
<feedburner:origLink>http://wordaligned.org/articles/desktop-preferences</feedburner:origLink></item>

<item>
<title>Knuth visited, Brains Limited</title>
<description>&lt;p&gt;Lucky me, I &lt;strong&gt;did&lt;/strong&gt; get a ticket to see Professor Donald Knuth deliver the &lt;a href="http://www.bcs.org/content/ConWebDoc/38048"&gt;2011 Turing Lecture&lt;/a&gt; at Cardiff University. He started out by saying he&amp;#8217;d been wanting to come here ever since seeing Yellow Submarine in the 60s because of some &lt;a href="http://www.imdb.com/title/tt0063823/quotes"&gt;dialogue&lt;/a&gt; which goes something like:
&lt;/p&gt;
&lt;p&gt;&amp;#8220;What do you call a big school of whales?&amp;#8221;
&lt;/p&gt;
&lt;p&gt;&amp;#8220;A University of Wales.&amp;#8221;
&lt;/p&gt;
&lt;p&gt;What a fine country Wales is for a computer scientist, he continued, you have a place called &lt;a href="http://maps.google.co.uk/maps?f=q&amp;amp;source=s_q&amp;amp;hl=en&amp;amp;geocode=&amp;amp;q=login&amp;amp;aq=&amp;amp;sll=53.800651,-4.064941&amp;amp;sspn=17.130747,46.538086&amp;amp;ie=UTF8&amp;amp;hq=&amp;amp;hnear=Login,+Whitland,+United+Kingdom&amp;amp;ll=51.837475,-4.745407&amp;amp;spn=0.279173,0.727158&amp;amp;z=11"&gt;Login&lt;/a&gt;. A village in Pembrokeshire, someone in the audience chipped in, it&amp;#8217;s very beautiful. Wonderful, all the more reason to visit!
&lt;/p&gt;
&lt;p&gt;Openings over, the lecture turned into a question and answer session. If I&amp;#8217;m honest, I&amp;#8217;d have preferred something more formal, but having just completed &lt;a href="http://www-cs-faculty.stanford.edu/~uno/taocp.html"&gt;TAOCP4A&lt;/a&gt;, all 900 pages of it, &lt;strong&gt;and&lt;/strong&gt; Volume 8 of his collected papers, the &amp;#8220;dessert&amp;#8221; edition of the series on &lt;a href="http://www-cs-faculty.stanford.edu/~uno/fg.html"&gt;fun and games&lt;/a&gt;, Professor Knuth was in a holiday mood, and it was a delight to hear him field questions on the future of computer science, P = NP, literate programming, analog computers, brute force vs. elegance, and whether the increase in computing power diminishes the art of programming.
&lt;/p&gt;
&lt;p&gt;Yes, computers grow exponentially more powerful &amp;#8212; but human appetites grow yet more quickly. And even if you have all of that power, can you be sure the computers are getting the right answers? This afternoon, Professor Knuth said, while strolling through Cardiff, he&amp;#8217;d seen the famous brewery &lt;strong&gt;Brains &amp;amp; Co. Ltd.&lt;/strong&gt; and thought, yes, that&amp;#8217;s about right. Brains limited.
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/arkadyevna/253441813/" title="BRAINS BRAINS BRAINS by Arkadyevna, on Flickr"&gt;&lt;img src="http://farm1.static.flickr.com/106/253441813_b1673d90a8.jpg" width="500" height="334" alt="BRAINS BRAINS BRAINS" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;My thanks to Arkadyevna for permission to use her wonderful &lt;a href="http://www.flickr.com/photos/arkadyevna/253441813/"&gt;photo.&lt;/a&gt;
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/p_UycUgCXL0" height="1" width="1"/&gt;</description>
<dc:date>2011-02-03</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/knuth-visited-brains-limited</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/p_UycUgCXL0/knuth-visited-brains-limited</link>
<category>Knuth</category>
<feedburner:origLink>http://wordaligned.org/articles/knuth-visited-brains-limited</feedburner:origLink></item>

<item>
<title>Set.insert or set.add?</title>
<description>&lt;h2&gt;Get set, go!&lt;/h2&gt;
&lt;p&gt;Suppose you have an element &lt;code&gt;e&lt;/code&gt; to put in a set &lt;code&gt;S&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;Should you:
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;S.add(e)

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;or:
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;S.insert(e)

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;?
&lt;/p&gt;
&lt;p&gt;It depends on which language you&amp;#8217;re using. I use C++ and Python and I usually get it wrong.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;&amp;gt;&amp;gt;&amp;gt; S.insert(e)
Traceback (most recent call last):
  File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;
AttributeError: 'set' object has no attribute 'insert'

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Try again!
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;error: 'class std::set&amp;lt;int, std::less&amp;lt;int&amp;gt;, std::allocator&amp;lt;int&amp;gt; &amp;gt;' 
has no member named 'add'

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Maybe my &lt;a href="http://wordaligned.org/articles/accidental-emacs" title="Emacs of course!"&gt;IDE&lt;/a&gt; should auto-complete the correct member function but it doesn&amp;#8217;t, or at least I haven&amp;#8217;t configured it to, so instead I&amp;#8217;ve worked out how to remember.
&lt;/p&gt;
&lt;p&gt;Now, neither C++ nor Python pins down how a set should be implemented &amp;#8212; read the language standard and reference manual respectively and all you&amp;#8217;ll get is an interface and some hints. Read between the lines of these references, though, or study &lt;a href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01064_source.html" title="G++ stl_tree.h, on which std::sets and std::multisets are based"&gt;the&lt;/a&gt; &lt;a href="http://svn.python.org/view/python/trunk/Objects/setobject.c?view=markup" title="setobject.c, from CPython"&gt;implementations&lt;/a&gt;, and you&amp;#8217;ll soon realise a Python set is an unordered container designed for fast membership, union, intersection, and differencing operations &amp;#8212; much like the mathematical sets I learned about at school &amp;#8212; whereas a C++ set is an ordered container, featuring logarithmic access times and persistent iterators. 
&lt;/p&gt;
&lt;p&gt;Think: C++ set &amp;asymp; binary tree; Python set &amp;asymp; hashed array.
&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s apparent which method is correct for which language now. To put something into a binary tree you must recurse down the tree and find where to &lt;strong&gt;insert&lt;/strong&gt; it. Hence &lt;code&gt;std::set::insert()&lt;/code&gt; is correct C++. To put something into a hashed array you hash it and &lt;strong&gt;add&lt;/strong&gt; it right there. Hence &lt;code&gt;set.add()&lt;/code&gt; is proper Python.
&lt;/p&gt;

&lt;h2&gt;How long is a string?&lt;/h2&gt;
&lt;p&gt;I&amp;#8217;m suggesting programmers should know at least some of what goes on in their standard language library implementations. Appreciating an API isn&amp;#8217;t always enough. You &lt;strong&gt;insert&lt;/strong&gt; into trees and &lt;strong&gt;add&lt;/strong&gt; to hashes: so if your set is a tree, call &lt;code&gt;S.insert()&lt;/code&gt;, and if it&amp;#8217;s a hash, &lt;code&gt;S.add()&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;Such logical arguments don&amp;#8217;t always deliver.
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; Suppose now that &lt;code&gt;S&lt;/code&gt; is a string and you&amp;#8217;re after its length. Should you use &lt;code&gt;S.length()&lt;/code&gt; or &lt;code&gt;S.size()&lt;/code&gt;?
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Neither or both.
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/the-g-uk/3867089043/" title="string [how long?] by the|G|, on Flickr"&gt;&lt;img src="http://farm3.static.flickr.com/2538/3867089043_2f2b3f5fa6.jpg" width="485" height="149" alt="string [how long?]" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;In Python a string is a standard sequence and as for all other sequences &lt;code&gt;len(S)&lt;/code&gt; does the trick. In C++ a string is a standard container and as for all other containers &lt;code&gt;S.size()&lt;/code&gt; returns the number of elements; &lt;strong&gt;but&lt;/strong&gt;, being &lt;code&gt;std::string&lt;/code&gt;, &lt;code&gt;S.length()&lt;/code&gt; does too.
&lt;/p&gt;
&lt;p&gt;Oh, and the next revision of C++ features an &lt;code&gt;unordered_set&lt;/code&gt; (available now as &lt;code&gt;std::tr1::unordered_set&lt;/code&gt;) which is a hashed container. I think &lt;code&gt;unordered_set&lt;/code&gt; is a poor name for something which models a set better than &lt;code&gt;std::set&lt;/code&gt; does but that&amp;#8217;s the price it pays for coming late to the party. And you don&amp;#8217;t &lt;code&gt;std::unordered_set::add&lt;/code&gt; elements to it, you &lt;code&gt;std::unordered_set::insert&lt;/code&gt; them.
&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;My thanks to &lt;a href="http://www.flickr.com/photos/the-g-uk"&gt;the|G|&amp;trade;&lt;/a&gt; for permission to use his &lt;a href="http://www.flickr.com/photos/the-g-uk/3867089043" title="string [how long?] on Flickr"&gt;string&lt;/a&gt;.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/pGPcHZlX1NY" height="1" width="1"/&gt;</description>
<dc:date>2010-11-17</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/setinsert-or-setadd</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/pGPcHZlX1NY/setinsert-or-setadd</link>
<category>C++</category>
<category>Python</category>
<feedburner:origLink>http://wordaligned.org/articles/setinsert-or-setadd</feedburner:origLink></item>

<item>
<title>Define pedantic</title>
<description>&lt;p&gt;My dictionary &lt;span id="definition"&gt;defines a pedant&lt;/span&gt; as:
&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;pedant&lt;/strong&gt; &lt;em&gt;n.&lt;/em&gt; &lt;strong&gt;1.&lt;/strong&gt; A person who relies too much on academic learning or who is concerned chiefly with academic detail.
&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Apparently the word derives from the Italian, &lt;em&gt;pedante&lt;/em&gt;, meaning teacher. During my career as a computer programmer a number of my colleagues have been surprisingly pedantic about the proper use of English.
&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&amp;#8220;I refuse to join a supermarket queue marked &lt;strong&gt;10 items or less&lt;/strong&gt;.&amp;#8221;
&lt;/p&gt;
&lt;p&gt;&amp;#8220;I do wish people would stop using &lt;strong&gt;target&lt;/strong&gt; as a verb. You &lt;strong&gt;aim&lt;/strong&gt; at a target, you don&amp;#8217;t &lt;strong&gt;target&lt;/strong&gt; it.&amp;#8221;
&lt;/p&gt;
&lt;p&gt;&amp;#8220;I am an exceptionally &lt;strong&gt;skilled grammarian&lt;/strong&gt; in English &amp;#8230; Take that rule and shove it!&amp;#8221;
&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Some of this fussiness may well be a reaction against &lt;a href="http://news.bbc.co.uk/1/hi/7457287.stm"&gt;corporate double-speak&lt;/a&gt;. Still, I wouldn&amp;#8217;t have expected programmers to be 1) so particular and 2) so certain they&amp;#8217;re right. Maybe this attitude comes from all those years of writing code. Programming languages are strict about what they&amp;#8217;ll accept: after all, they have standards!
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/thomasguest/5142421926/" title="The C++ Standard vs Perl in a Nutshell by Thomas Guest, on Flickr"&gt;&lt;img src="http://farm2.static.flickr.com/1072/5142421926_6b76c52749_m.jpg" width="240" height="183" alt="The C++ Standard vs Perl in a Nutshell" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;span id="continue-reading"/&gt;

&lt;p&gt;Some programming languages are more pedantic than others. Paul Graham memorably characterises C++ as a pernickety aunt&lt;a id="fn1link" href="http://wordaligned.org/articles/define-pedantic#fn1"&gt;&lt;sup&gt;[1]&lt;/sup&gt;&lt;/a&gt;. By contrast, Perl won&amp;#8217;t pick nits. Designed by Larry Wall to be his software &lt;a href="http://www.wall.org/~larry/pm.html" title="Perl, the first postmodern computer language. Larry Wall"&gt;butler&lt;/a&gt;, Perl interprets your ill-expressed wishes with discretion and assurance. Hence you can end up with a Perl program which gets on with its job but which no-one fully understands.
&lt;/p&gt;
&lt;p&gt;Pedants, by definition, take things too far, but pedantry in programming isn&amp;#8217;t all bad. GCC has a useful &lt;a href="http://gcc.gnu.org/onlinedocs/gcc/Standards.html"&gt;&lt;code&gt;-pedantic&lt;/code&gt;&lt;/a&gt; flag. It helps you write portable programs. Perl has a &lt;a href="http://perldoc.perl.org/strict.html"&gt;&lt;code&gt;use strict&lt;/code&gt;&lt;/a&gt; pragma which recasts the butler as a personal trainer. 
&lt;/p&gt;
&lt;p&gt;When it comes to correctness, attention to detail matters. What if this input parameter goes negative? Will that file be closed when an exception is thrown? Can your algorithm handle an empty container?
&lt;/p&gt;
&lt;p&gt;I recently fixed a defect in some (of my own) code which assumed conformant input. When faced with garbage-in this code failed even to generate garbage-out, instead getting caught in an infinite loop. Should a pedantic program insist on correct inputs or should it consider how to handle all possible inputs? &lt;a href="http://wordaligned.org/articles/define-pedantic#definition"&gt;Define pedantic&lt;/a&gt;.
&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;a id="fn1" href="http://wordaligned.org/articles/define-pedantic#fn1link"&gt;[1]&lt;/a&gt;: It turns out my memory is at fault here. When I checked the reference I discovered Paul Graham makes no explicit mention of C++. 
&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;We need a language that lets us scribble and smudge and smear, not a language where you have to sit with a teacup of types balanced on your knee and make polite conversation with a strict old aunt of a compiler. &amp;#8212; Paul Graham, &lt;a href="http://www.paulgraham.com/hp.html" title="Hackers and Painters"&gt;Hackers and Painters&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;You might have guessed which language he&amp;#8217;s promoting for scribbling but there&amp;#8217;s no mention of Lisp either in this particular essay. In fact only one programming language earns a name-check. I&amp;#8217;ll let you find out which one for yourselves.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/ZwpJK28-YBM" height="1" width="1"/&gt;</description>
<dc:date>2010-11-02</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/define-pedantic</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/ZwpJK28-YBM/define-pedantic</link>
<category>C++</category>
<category>Perl</category>
<feedburner:origLink>http://wordaligned.org/articles/define-pedantic</feedburner:origLink></item>

<item>
<title>Hiding iterator boilerplate behind a Boost facade</title>
<description>&lt;div class="toc"&gt;
&lt;h2&gt;Contents&lt;/h2&gt;
&lt;ul&gt;
 &lt;li&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#tocfilling-in-missing-methods-python" name="toc0" id="toc0"&gt;Filling in missing methods. Python&lt;/a&gt;
 &lt;/li&gt;

 &lt;li&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#tocfilling-in-missing-methods-c" name="toc1" id="toc1"&gt;Filling in missing methods. C++&lt;/a&gt;
 &lt;/li&gt;

 &lt;li&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#tocenter-boost-iterators" name="toc2" id="toc2"&gt;Enter Boost iterators&lt;/a&gt;
 &lt;/li&gt;

 &lt;li&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#tocusing-boostiteratorfacade" name="toc3" id="toc3"&gt;Using boost::iterator_facade&lt;/a&gt;
 &lt;/li&gt;

 &lt;li&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#toctemplates-and-traits" name="toc4" id="toc4"&gt;Templates and Traits&lt;/a&gt;
 &lt;/li&gt;

 &lt;li&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#tocconstructors-destructors-and-operators" name="toc5" id="toc5"&gt;Constructors, destructors and operators&lt;/a&gt;
 &lt;/li&gt;

 &lt;li&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#tocwrinkles" name="toc6" id="toc6"&gt;Wrinkles&lt;/a&gt;
 &lt;/li&gt;

 &lt;li&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#tocless-code-more-software" name="toc7" id="toc7"&gt;Less code, more software&lt;/a&gt;
 &lt;/li&gt;

 &lt;li&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#tocperformance" name="toc8" id="toc8"&gt;Performance&lt;/a&gt;
 &lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.flickr.com/photos/davehamster/2336911145/" title="SS Great Britain by Dave Hamster, on Flickr"&gt;&lt;img src="http://farm3.static.flickr.com/2379/2336911145_5275811ec0_m.jpg" width="240" height="160" alt="SS Great Britain"/&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#toc0" name="tocfilling-in-missing-methods-python" id="tocfilling-in-missing-methods-python"&gt;Filling in missing methods. Python&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Here&amp;#8217;s another wholesome &lt;a href="http://code.activestate.com/recipes/576685" title="Total ordering class decorator, by Raymond Hettinger"&gt;recipe&lt;/a&gt; served up by Raymond Hettinger.
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;Total ordering class decorator&lt;/div&gt;

&lt;pre class="prettyprint"&gt;def total_ordering(cls):
    'Class decorator that fills-in missing ordering methods'    
    convert = {
        '__lt__': [('__gt__', lambda self, other: other &amp;lt; self),
                   ('__le__', lambda self, other: not other &amp;lt; self),
                   ('__ge__', lambda self, other: not self &amp;lt; other)],
        '__le__': [('__ge__', lambda self, other: other &amp;lt;= self),
                   ('__lt__', lambda self, other: not other &amp;lt;= self),
                   ('__gt__', lambda self, other: not self &amp;lt;= other)],
        '__gt__': [('__lt__', lambda self, other: other &amp;gt; self),
                   ('__ge__', lambda self, other: not other &amp;gt; self),
                   ('__le__', lambda self, other: not self &amp;gt; other)],
        '__ge__': [('__le__', lambda self, other: other &amp;gt;= self),
                   ('__gt__', lambda self, other: not other &amp;gt;= self),
                   ('__lt__', lambda self, other: not self &amp;gt;= other)]
    }
    roots = set(dir(cls)) &amp;amp; set(convert)
    assert roots, 'must define at least one ordering operation: &amp;lt; &amp;gt; &amp;lt;= &amp;gt;='
    root = max(roots)       # prefer __lt__ to __le__ to __gt__ to __ge__
    for opname, opfunc in convert[root]:
        if opname not in roots:
            opfunc.__name__ = opname
            opfunc.__doc__ = getattr(int, opname).__doc__
            setattr(cls, opname, opfunc)
    return cls

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you have a class, &lt;code&gt;X&lt;/code&gt;, which implements one or more of the ordering operators, &lt;code&gt;&amp;lt;, &amp;lt;=, &amp;gt;, &amp;gt;=&lt;/code&gt; then &lt;code&gt;total_ordering(X)&lt;/code&gt; adapts and returns the class with the missing operators filled-in. Alternatively, use standard decorator syntax to adapt a class. If we apply &lt;code&gt;@total_ordering&lt;/code&gt; to a &lt;code&gt;Point&lt;/code&gt; class
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;@total_ordering
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __lt__(self, other):
        return (self.x, self.y) &amp;lt; (other.x, other.y)

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;then we can compare points however we like
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;&amp;gt;&amp;gt;&amp;gt; p = Point(1,2)
&amp;gt;&amp;gt;&amp;gt; q = Point(1,3)
&amp;gt;&amp;gt;&amp;gt; p &amp;lt; q, p &amp;gt; q, p &amp;gt;= q, p &amp;lt;= q
(True, False, False, True)

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here&amp;#8217;s a nice touch: the freshly-baked methods even have documentation!
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;&amp;gt;&amp;gt;&amp;gt; help(Point)
Help on class Point in module __main__:

class Point
 |  Methods defined here:
 |  
 |  __ge__(self, other)
 |      x.__ge__(y) &amp;lt;==&amp;gt; x&amp;gt;=y
 |  
 |  __gt__(self, other)
 |      x.__gt__(y) &amp;lt;==&amp;gt; x&amp;gt;y
 |  
 |  __init__(self, x, y)
 |  
 |  __le__(self, other)
 |      x.__le__(y) &amp;lt;==&amp;gt; x&amp;lt;=y
 |  
 |  __lt__(self, other)

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Writing class decorators may not be the first thing a new Python programmer attempts, but once you&amp;#8217;ve discovered the relationship between Python&amp;#8217;s special method names and the more familiar operator symbols, I think this recipe is remarkably straightforward.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;convert = {
    '__lt__': [('__gt__', lambda self, other: other &amp;lt; self),
               ('__le__', lambda self, other: not other &amp;lt; self),
               ('__ge__', lambda self, other: not self &amp;lt; other)],
    '__le__': [('__ge__', lambda self, other: other &amp;lt;= self),
               ('__lt__', lambda self, other: not other &amp;lt;= self),
               ('__gt__', lambda self, other: not self &amp;lt;= other)],
    '__gt__': [('__lt__', lambda self, other: other &amp;gt; self),
               ('__ge__', lambda self, other: not other &amp;gt; self),
               ('__le__', lambda self, other: not self &amp;gt; other)],
    '__ge__': [('__le__', lambda self, other: other &amp;gt;= self),
               ('__gt__', lambda self, other: not other &amp;gt;= self),
               ('__lt__', lambda self, other: not self &amp;gt;= other)]
}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Before moving on to something more challenging, look again at one of the recipe&amp;#8217;s key ingredients, the &lt;code&gt;convert&lt;/code&gt; dict, which helps create the missing ordering functions from existing ones. As you can see, there&amp;#8217;s much repetition here, and plenty of opportunities for cut-and-paste errors.
&lt;/p&gt;
&lt;p&gt;This block of code is an example of what programmers term &lt;a href="http://en.wikipedia.org/wiki/Boilerplate_(text)#Boilerplate_code"&gt;boilerplate&lt;/a&gt;. By using the total ordering decorator, we can avoid boilerplating our own code.&lt;a id="fn1link" href="http://wordaligned.org/articles/boost-iterator-facade#fn1"&gt;&lt;sup&gt;[1]&lt;/sup&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#toc1" name="tocfilling-in-missing-methods-c" id="tocfilling-in-missing-methods-c"&gt;Filling in missing methods. C++&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Python is dynamic and self-aware, happy to expose its internals for this kind of tinkering.  It takes real wizardry to achieve similar results with a &lt;a href="http://sites.google.com/site/steveyegge2/tour-de-babel" title="C++ is the dumbest language on earth ... doesn't know about itself. It is not introspective"&gt;less flexible language, such as C++&lt;/a&gt; &amp;#8212; but it can be done.
&lt;/p&gt;
&lt;span id="continue-reading"/&gt;

&lt;p&gt;In a &lt;a href="http://wordaligned.org/articles/binary-search-revisited"&gt;previous article&lt;/a&gt; we developed a random access file iterator in C++. At its heart, this iterator simply repositioned itself using file-seeks and dereferenced itself using file-reads. There wasn&amp;#8217;t much to it.
&lt;/p&gt;
&lt;p&gt;Unfortunately we had to fill-out the iterator with the various members required to make it comply with the standard random access iterator requirements (which was the whole point, since we wanted something we could use with standard binary search algorithms).
&lt;/p&gt;
&lt;p&gt;We had to expose standard typedefs:
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;typedef std::random_access_iterator_tag iterator_category;
typedef item value_type;
typedef std::streamoff difference_type;
typedef item * pointer;
typedef item &amp;amp; reference;

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Worse, we had to implement a full set of comparison, iteration, step and access functions. Please, page down past the following code block! I only include it here so you can see how long it goes on for.
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;Iterator boilerplate&lt;/div&gt;

&lt;pre class="prettyprint"&gt;public: // Comparison
    bool operator&amp;lt;(iter const &amp;amp; other) const
    {
        return pos &amp;lt; other.pos;
    }
    
    bool operator&amp;gt;(iter const &amp;amp; other) const
    {
        return pos &amp;gt; other.pos;
    }
    
    bool operator==(iter const &amp;amp; other) const
    {
        return pos == other.pos;
    }
    
    bool operator!=(iter const &amp;amp; other) const
    {
        return pos != other.pos;
    }
    
public: // Iteration
    iter &amp;amp; operator++()
    {
        return *this += 1;
    }
    
    iter &amp;amp; operator--()
    {
        return *this -= 1;
    }
    
    iter operator++(int)
    {
        iter tmp(*this);
        ++(*this);
        return tmp;
    }
    
    iter operator--(int)
    {
        iter tmp(*this);
        --(*this);
        return tmp;
    }
    
public: // Step
    iter &amp;amp; operator+=(difference_type n)
    {
        advance(n);
        return *this;
    }
    
    iter &amp;amp; operator-=(difference_type n)
    {
        advance(-n);
        return *this;
    }
    
    iter operator+(difference_type n)
    {
        iter result(*this);
        return result += n;
    }
    
    iter operator-(difference_type n)
    {
        iter result(*this);
        return result -= n;
    }
    
public: // Distance
    difference_type operator-(iter &amp;amp; other)
    {
        return pos - other.pos;
    }
    
public: // Access
    value_type operator*()
    {
        return (*this)[0];
    }

value_type operator[](difference_type n)
    {
        std::streampos restore = getpos();
        advance(n);
        value_type const result = read();
        setpos(restore);
        return result;
    }
    ....

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;How tiresome! Most of these member functions are directly and unsurprisingly implemented in a standard way. It would be nice if we could write (and test!) what we actually needed to and have a decorator fill in the rest.
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/chr1sp/3997724676/" title="Library - Ephesus by Chris. P, on Flickr"&gt;&lt;img src="http://farm3.static.flickr.com/2554/3997724676_bf73106637.jpg" width="500" height="334" alt="Library - Ephesus"/&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#toc2" name="tocenter-boost-iterators" id="tocenter-boost-iterators"&gt;Enter Boost iterators&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Actually, we can! I&amp;#8217;m grateful to proggitor dzorz for &lt;a href="http://www.reddit.com/r/programming/comments/c8fsk/binary_search_revisited/c0quxr0"&gt;telling me how&lt;/a&gt;.
&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;A nicer solution would use boost::iterator_facade and just implement dereference, equal, increment, decrement, advance and distance_to.
&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Like many programmers I have mixed feelings about C++ &amp;#8212; when it&amp;#8217;s good it&amp;#8217;s very very good, but when it&amp;#8217;s bad it&amp;#8217;s horrid &amp;#8212; and these feelings are only amplified by the &lt;a href="http://www.boost.org" title="Boost library home page"&gt;Boost&lt;/a&gt; library. Boost is superb, so long as you stick to the good parts.
&lt;/p&gt;
&lt;p&gt;So which parts are good? It depends. On you, who you work with, and the platforms you&amp;#8217;re working on.
&lt;/p&gt;
&lt;p&gt;In my previous article I used an ingenious iterator adaptor from the &lt;a href="http://www.boost.org/doc/libs/release/libs/spirit/index.html"&gt;Boost.Spirit&lt;/a&gt; parser library to disastrous effect. If only I&amp;#8217;d looked a little more carefully I&amp;#8217;d have discovered something more useful in a more obvious place. &lt;a href="http://www.boost.org/doc/libs/release/libs/iterator/"&gt;Boost.Iterator&lt;/a&gt; could have helped.
&lt;/p&gt;
&lt;p&gt;As dzorz points out, &lt;code&gt;boost::iterator_facade&lt;/code&gt; can work with any C++ iterable. Implement whatever subset of 
&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;
     dereference
 &lt;/li&gt;

 &lt;li&gt;
     equal
 &lt;/li&gt;

 &lt;li&gt;
     increment 
 &lt;/li&gt;

 &lt;li&gt;
     decrement
 &lt;/li&gt;

 &lt;li&gt;
     advance
 &lt;/li&gt;

 &lt;li&gt;
     distance_to
 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;is appropriate and &lt;code&gt;iterator_facade&lt;/code&gt; will fill in the boilerplate required to standardise your iterator.
&lt;/p&gt;
&lt;p&gt;In our case, we&amp;#8217;ll need the full set. That&amp;#8217;s because we&amp;#8217;re after a random access iterator. Other iterators need rather less. Here&amp;#8217;s a &lt;a href="http://www.boost.org/doc/libs/1_43_0/libs/iterator/doc/iterator_facade.html#iterator-facade-requirements"&gt;table&lt;/a&gt; showing the relationship between core operations and iterator concepts.
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.boost.org/doc/libs/1_43_0/libs/iterator/doc/iterator_facade.html#iterator-facade-requirements"&gt;&lt;img src="http://wordaligned.org/images/iterator-facade.png" alt="iterator_facade Core Operations"/&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#toc3" name="tocusing-boostiteratorfacade" id="tocusing-boostiteratorfacade"&gt;Using boost::iterator_facade&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;a href="http://www.boost.org/doc/libs/release/libs/iterator/"&gt;Boost.Iterator&lt;/a&gt; documentation is well-written but daunting. Read it from top-to bottom and you&amp;#8217;ll get:
&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;
     rationale and theory
 &lt;/li&gt;

 &lt;li&gt;
     plans for standardisation (which don&amp;#8217;t seem correct &lt;a id="fn2link" href="http://wordaligned.org/articles/boost-iterator-facade#fn2"&gt;&lt;sup&gt;[2]&lt;/sup&gt;&lt;/a&gt;)
 &lt;/li&gt;

 &lt;li&gt;
     &lt;strong&gt;usage notes&lt;/strong&gt;
 &lt;/li&gt;

 &lt;li&gt;
     some subtle points on the implementation and its predecessor
 &lt;/li&gt;

 &lt;li&gt;
     a namecheck for the curiously recurring template pattern
 &lt;/li&gt;

 &lt;li&gt;
     a fat reference section detailing the boilerplate which this library allows you to forget
 &lt;/li&gt;

 &lt;li&gt;
     a &lt;strong&gt;tutorial&lt;/strong&gt;
 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you&amp;#8217;re tempted to skip to the end of the page, you&amp;#8217;ll see this code block.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;boost/type_traits/is_convertible.hpp&amp;gt;
#include &amp;lt;boost/utility/enable_if.hpp&amp;gt;
  
  ....
  
private:
  struct enabler {};
  
public:
  template &amp;lt;class OtherValue&amp;gt;
  node_iter(
      node_iter&amp;lt;OtherValue&amp;gt; const&amp;amp; other
    , typename boost::enable_if&amp;lt;
          boost::is_convertible&amp;lt;OtherValue*,Value*&amp;gt;
        , enabler
      &amp;gt;::type = enabler()
  )
    : m_node(other.m_node) {}

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;According to the surrounding documentation this is &amp;#8220;magic&amp;#8221;. I find it scary.
&lt;/p&gt;
&lt;p&gt;Luckily it turns out the library is straightforward to use. What you really want, as a newcomer, are the &lt;a href="http://www.boost.org/doc/libs/1_43_0/libs/iterator/doc/iterator_facade.html#usage"&gt;usage notes&lt;/a&gt; and the &lt;a href="http://www.boost.org/doc/libs/1_43_0/libs/iterator/doc/iterator_facade.html#tutorial-example"&gt;tutorial example&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;The tutorial walks through the process of skinning a singly-linked list with a forwards iterator facade. This is a different use case to ours: the tutorial shows a basic class which implements what it should, and the facade allows it to be treated as a forwards iterator. In our case we&amp;#8217;ve already created a full-blown random access iterator. We can retrospectively apply &lt;code&gt;iterator_facade&lt;/code&gt; to strip our class back to basics.
&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#toc4" name="toctemplates-and-traits" id="toctemplates-and-traits"&gt;Templates and Traits&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Where we had:
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;template &amp;lt;typename item&amp;gt;
class text_file_iter
{
public: // Traits typedefs, which make this class usable with
        // algorithms which need a random access iterator.
    typedef std::random_access_iterator_tag iterator_category;
    typedef item value_type;
    typedef std::streamoff difference_type;
    typedef item * pointer;
    typedef item &amp;amp; reference;
....
};

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We now need (my thanks here to Giuseppe for correcting the code I originally posted here):
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;template &amp;lt;typename value&amp;gt;
class text_file_iter
    : public boost::iterator_facade&amp;lt;
      text_file_iter&amp;lt;value&amp;gt;
    , value
    , std::random_access_iterator_tag
    , value
    , std::streamoff
    &amp;gt;
....
};

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;(Yes, the class accepts itself as a template parameter. That&amp;#8217;s the curious recursion.)
&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#toc5" name="tocconstructors-destructors-and-operators" id="tocconstructors-destructors-and-operators"&gt;Constructors, destructors and operators&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;We still need iterator constructors and destructors &amp;#8212; these are unchanged &amp;#8212; but &lt;strong&gt;we can eliminate every single operator&lt;/strong&gt; shown in the &amp;#8220;Iterator boilerplate&amp;#8221; code block above.
&lt;/p&gt;
&lt;p&gt;Here&amp;#8217;s what we need instead, to ensure &lt;code&gt;iterator_facade&lt;/code&gt; can do its job. The &lt;code&gt;read()&lt;/code&gt; member function we had before doesn&amp;#8217;t need changing.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;    ....
private: // Everything Boost's iterator facade needs
    friend class boost::iterator_core_access;
    
    value dereference() const
    {
        return read();
    }
    
    bool equal(iter const &amp;amp; other) const
    {
        return pos == other.pos;
    }
    
    void increment()
    {
        advance(1);
    }
    
    void decrement()
    {
        advance(-1);
    }
    
    void advance(std::streamoff n)
    {
        in.seekg(n, std::ios_base::cur);
        pos = in.tellg();
    }
    
    std::streamoff distance_to(iter const &amp;amp; other) const
    {
        return other.pos - pos;
    }

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And that really is all there is to it. I&amp;#8217;m impressed.
&lt;/p&gt;
&lt;p&gt;Notice, by the way, that &lt;code&gt;friend&lt;/code&gt; is used to expose the primitive, private member functions for use by the &lt;code&gt;boost::iterator_core_access&lt;/code&gt; class. This follows the example set by the tutorial. I&amp;#8217;ve written enough C and Python to question C++&amp;#8217;s sophisticated access rules &amp;#8212; you have &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;protected&lt;/code&gt; and &lt;code&gt;private&lt;/code&gt;, but that&amp;#8217;s &lt;strong&gt;still&lt;/strong&gt; not enough, so you need &lt;code&gt;friend&lt;/code&gt; declaration to cut through it all &amp;#8212; which tempts me to simply make &lt;code&gt;dereference()&lt;/code&gt;, &lt;code&gt;equal()&lt;/code&gt; etc. public, but then the facade wouldn&amp;#8217;t be a proper facade. Users should treat the final class exactly as they would any other random access iterator, and designating these members as &lt;code&gt;private&lt;/code&gt; means they&amp;#8217;ll have to.
&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#toc6" name="tocwrinkles" id="tocwrinkles"&gt;Wrinkles&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;You&amp;#8217;ll notice the &lt;code&gt;dereference()&lt;/code&gt; member function has a &lt;code&gt;const&lt;/code&gt; signature. However, the &lt;code&gt;read()&lt;/code&gt; member function is non-const.
&lt;/p&gt;
&lt;div class="typocode"&gt;

&lt;pre class="prettyprint"&gt;    // Return the item at the current position
    value_type read()
    {
        value_type n = 0;
        
        // Reverse till we hit whitespace or the start of the file
        while (in &amp;amp;&amp;amp; !isspace(in.peek()))
        {
            in.unget();
        }
        in.clear();
        
        in &amp;gt;&amp;gt; n;
        return n;
    }

&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, &lt;code&gt;in&lt;/code&gt; is a data member of type &lt;code&gt;std::ifstream&lt;/code&gt;, and clearly the read call modifies it. That&amp;#8217;s what this alarming compiler error is trying to tell us.
&lt;/p&gt;
&lt;pre&gt;
boost_binary_search_text_file.cpp: In member function 'value text_file_iter&amp;lt;value&amp;gt;::read() const [with value = long long int]':
boost_binary_search_text_file.cpp:90:   instantiated from 'value text_file_iter&amp;lt;value&amp;gt;::dereference() const [with value = long long int]'
/opt/local/include/boost/iterator/iterator_facade.hpp:516:   instantiated from 'static typename Facade::reference boost::iterator_core_access::dereference(const Facade&amp;amp;) [with Facade = text_file_iter&amp;lt;long long int&amp;gt;]'
/opt/local/include/boost/iterator/iterator_facade.hpp:634:   instantiated from 'Reference boost::iterator_facade&amp;lt;I, V, TC, R, D&amp;gt;::operator*() const [with Derived = text_file_iter&amp;lt;long long int&amp;gt;, Value = long long int, CategoryOrTraversal = boost::random_access_traversal_tag, Reference = long long int, Difference = long long int]'
/usr/include/c++/4.2.1/bits/stl_algo.h:4240:   instantiated from 'bool std::binary_search(_ForwardIterator, _ForwardIterator, const _Tp&amp;amp;) [with _ForwardIterator = text_file_iter&amp;lt;long long int&amp;gt;, _Tp = main::number]'
boost_binary_search_text_file.cpp:203:   instantiated from here
boost_binary_search_text_file.cpp:174: error: passing 'const std::basic_ifstream&amp;lt;char, std::char_traits&amp;lt;char&amp;gt; &amp;gt;' as 'this' argument of 'typename std::basic_istream&amp;lt;_CharT, _Traits&amp;gt;::int_type std::basic_istream&amp;lt;_CharT, _Traits&amp;gt;::peek() [with _CharT = char, _Traits = std::char_traits&amp;lt;char&amp;gt;]' discards qualifiers
boost_binary_search_text_file.cpp:176: error: passing 'const std::basic_ifstream&amp;lt;char, std::char_traits&amp;lt;char&amp;gt; &amp;gt;' as 'this' argument of 'std::basic_istream&amp;lt;_CharT, _Traits&amp;gt;&amp;amp; std::basic_istream&amp;lt;_CharT, _Traits&amp;gt;::unget() [with _CharT = char, _Traits = std::char_traits&amp;lt;char&amp;gt;]' discards qualifiers
boost_binary_search_text_file.cpp:178: error: passing 'const std::basic_ifstream&amp;lt;char, std::char_traits&amp;lt;char&amp;gt; &amp;gt;' as 'this' argument of 'void std::basic_ios&amp;lt;_CharT, _Traits&amp;gt;::clear(std::_Ios_Iostate) [with _CharT = char, _Traits = std::char_traits&amp;lt;char&amp;gt;]' discards qualifiers
boost_binary_search_text_file.cpp:180: error: passing 'const std::basic_ifstream&amp;lt;char, std::char_traits&amp;lt;char&amp;gt; &amp;gt;' as 'this' argument of 'std::basic_istream&amp;lt;_CharT, _Traits&amp;gt;&amp;amp; std::basic_istream&amp;lt;_CharT, _Traits&amp;gt;::operator&amp;gt;&amp;gt;(long long int&amp;amp;) [with _CharT = char, _Traits = std::char_traits&amp;lt;char&amp;gt;]' discards qualifiers
&lt;/pre&gt;

&lt;p&gt;Related to this, the &lt;code&gt;Reference&lt;/code&gt; template parameter (shown in bold in the listing below) is actually a &lt;code&gt;value&lt;/code&gt;, rather than the (default) &lt;code&gt;value &amp;amp;&lt;/code&gt;. As we originally implemented it, our file iterator reads values lazily, only when clients request them. We have no reference to return.
&lt;/p&gt;
&lt;pre&gt;
template &amp;lt;typename value&amp;gt;
class text_file_iter
    : public boost::iterator_facade&amp;lt;
      text_file_iter&amp;lt;value&amp;gt;
    , value
    , std::random_access_iterator_tag
    , &lt;strong&gt;value&lt;/strong&gt;
    , std::streamoff
    &amp;gt;
};
&lt;/pre&gt;

&lt;p&gt;I faced a dilemma here. Either I could modify my original file iterator, including a current value data member, which I would take care to update every time we repositioned the file read position. Then our references could be real references and &lt;code&gt;read()&lt;/code&gt; would naturally be &lt;code&gt;const&lt;/code&gt;, simply returning a reference to this member. Or, I could make the &lt;code&gt;in&lt;/code&gt; input stream data member &lt;code&gt;mutable&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Mutable&lt;/code&gt; makes me uneasy for the same reason that &lt;code&gt;friend&lt;/code&gt; does &amp;#8212; if you can shake off the rigours of const-correctness so easily, then why bother with it? &amp;#8212; and for this reason the first option appealed. However, a read-only file is an unusual container: we do not change it, but we cannot supply const references to its elements without reading them in, and that will mean changes to the file input stream. The easier option, involving the smallest code change, was to make &lt;code&gt;in&lt;/code&gt; mutable. So that&amp;#8217;s what I did.
&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#toc7" name="tocless-code-more-software" id="tocless-code-more-software"&gt;Less code, more software&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;By employing two of my least favourite C++ keywords I now had a class which provided the functions it should, and, thanks to the magic worked by Boost&amp;#8217;s iterator facade, I also had a class which I could use as a standard random access iterator. Most of the code changes were the deletion of boilerplate &amp;#8212; very satisfying. I added code too, since I decided to invest a little more effort in tests. I didn&amp;#8217;t have any doubts about the Boost library&amp;#8217;s correctness but I thought I might not have been using it correctly. Happily these tests all passed.
&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://wordaligned.org/articles/boost-iterator-facade#toc8" name="tocperformance" id="tocperformance"&gt;Performance&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Let&amp;#8217;s not forget why we originally wanted a random access file iterator: we had a large sorted file, too large to read into memory, and we wanted to test for the presence of the number in this file.
&lt;/p&gt;
&lt;p&gt;For test purposes I created a file just over 4GB in size. A simple linear search through this file took around 180 seconds on my (aging laptop) computer, and was light on memory use. By creating a random access file iterator, boilerplate and all, we took advantage of the standard binary search algorithm and reduced the time to around 4 milliseconds.
&lt;/p&gt;
&lt;p&gt;How would our version using Boost iterator facade do? I wasn&amp;#8217;t expecting it to be faster than the original, but I wouldn&amp;#8217;t have been surprised if it gave it a close run: using Boost doesn&amp;#8217;t usually involve compromise. In fact, over repeated runs of my performance tests there was no significant difference between the two iterator versions &amp;#8212; or at least there wasn&amp;#8217;t once a helpful reader had discovered and fixed a bug in my program, which was causing it to run correctly but slowly.
&lt;/p&gt;
&lt;p&gt;To trust a facade I guess you need some knowledge of what lies behind it.
&lt;/p&gt;
&lt;p style="text-align:center"&gt;&amp;sect;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: During my original performance tests, reported in the first version of this article, the Boost iterator performed woefully, far slower even than a linear search. By this time I&amp;#8217;d lost patience, and it was left up to a reader, Giuseppe, to &lt;a href="http://wordaligned.org/articles/boost-iterator-facade#comment-60988668"&gt;point out my mistake&lt;/a&gt;. I&amp;#8217;d been using a &lt;code&gt;boost::random_access_traversal_tag&lt;/code&gt; template parameter, with the result that &lt;code&gt;std::distance()&lt;/code&gt; was using repeated increments rather than calling &lt;code&gt;distance_to()&lt;/code&gt; to get an immediate result, and consequently ran very slowly. I should have used &lt;code&gt;std::random_access_iterator_tag&lt;/code&gt;. I modified my code accordingly and confirmed that the Boost version does indeed perform on a par with the original version.
&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;My thanks to &lt;a href="http://www.flickr.com/photos/chr1sp/" title="Chris. P on Flickr"&gt;Chris P&lt;/a&gt; for permission to use his &lt;a href="http://www.flickr.com/photos/chr1sp/3997724676"&gt;photograph&lt;/a&gt; of the &lt;a href="http://en.wikipedia.org/wiki/Library_of_Celsus" title="Library of Celsus, Wikipedia"&gt;Library of Celsus&lt;/a&gt; at Ephesus, or at rather its beautiful facade. Ephesus is famous for the Temple of Artemis, one of the seven wonders of the ancient world, of which only fragments remain. Thanks too to &lt;a href="http://www.flickr.com/photos/davehamster/"&gt;Dave Hamster&lt;/a&gt; for the boilerplate &lt;a href="http://www.flickr.com/photos/davehamster/2336911145/"&gt;photo&lt;/a&gt; &amp;#8212; actually a detail from the hull of the &lt;a href="http://www.ssgreatbritain.org"&gt;SS Great Britain&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;d like to continue this experiment the code and the tests I used are available via anonymous SVN access from &lt;a href="http://svn.wordaligned.org/svn/etc/search_text_file"&gt;http://svn.wordaligned.org/svn/etc/search_text_file&lt;/a&gt;.
&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;a id="fn1" href="http://wordaligned.org/articles/boost-iterator-facade#fn1link"&gt;[1]&lt;/a&gt;: As of Python 2.7 and 3.2, the standard library will include a version of this recipe. It&amp;#8217;s in the &lt;a href="http://docs.python.org/dev/py3k/library/functools.html#functools.total_ordering" title="functools.total_ordering decorator documentation"&gt;functools module&lt;/a&gt;. For some reason, your class &amp;#8220;should supply an __eq__()&amp;#8221; method.
&lt;/p&gt;
&lt;p&gt;&lt;a id="fn2" href="http://wordaligned.org/articles/boost-iterator-facade#fn2link"&gt;[2]&lt;/a&gt;: According to the Boost.Iterator documentation:
&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Both &lt;code&gt;iterator_facade&lt;/code&gt; and &lt;code&gt;iterator_adaptor&lt;/code&gt; as well as many of the specialized adaptors mentioned below have been proposed for standardization, and accepted into the first C++ technical report; see our [Standard Proposal For Iterator Facade and Adaptor (PDF)][tr1proposal] for more details.
&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I assumed this meant there&amp;#8217;d be &lt;code&gt;tr1::iterator_(facade|adaptor)&lt;/code&gt; classes, but I don&amp;#8217;t think that&amp;#8217;s the case. Unlike other (good) bits of Boost, the Iterator library doesn&amp;#8217;t seem likely to be part of the next C++ release.
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/wordaligned/~4/GpCyHa2q-xw" height="1" width="1"/&gt;</description>
<dc:date>2010-07-07</dc:date>
<guid isPermaLink="false">http://wordaligned.org/articles/boost-iterator-facade</guid>
<author>tag@wordaligned.org (Thomas Guest)</author>
<link>http://feeds.wordaligned.org/~r/wordaligned/~3/GpCyHa2q-xw/boost-iterator-facade</link>
<category>C++</category>
<category>Python</category>
<category>Boost</category>
<feedburner:origLink>http://wordaligned.org/articles/boost-iterator-facade</feedburner:origLink></item>

</channel>
</rss>
