<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Alice, Bob and Mallory</title>
    <link>http://alicebobandmallory.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>security and obscurity</description>
    <item>
      <title>JavaScript hash table keys</title>
      <description>&lt;p&gt;In JavaScript you can add properties to objects dynamically. You can access those properties both by &lt;code&gt;object.foo&lt;/code&gt; and &lt;code&gt;object['foo']&lt;/code&gt;. The later is commonly used to use JavaScript objects as &lt;a href="http://en.wikipedia.org/wiki/Hash_table"&gt;hash tables&lt;/a&gt; (associative arrays).&lt;/p&gt;

&lt;p&gt;While implementing &lt;a href="http://stackoverflow.com/questions/2380019/generate-8-unique-random-numbers-between-1-and-100/2380513#2380513"&gt;a simplistic unique random number generator&lt;/a&gt; I happened to use &lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/keys"&gt;keys(obj)&lt;/a&gt;. Unfortunately &lt;code&gt;keys(obj)&lt;/code&gt; is part of &lt;a href="http://en.wikipedia.org/wiki/ECMAScript"&gt;ECMAScript&lt;/a&gt; 5. See chapter 15.2.3.14 in &lt;a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf"&gt;ECMA-262&lt;/a&gt;. The web browsers of today mostly implements ECMAScript 3.&lt;/p&gt;

&lt;p&gt;Here's an implementation of &lt;code&gt;keys(obj)&lt;/code&gt; for ECMAScript 3 browsers (tested in Google Chrome, IE8 and Firefox 3.5). If the browser already has a keys function then nothing will be done.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (typeof keys == &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;undefined&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) &lt;tt&gt;
&lt;/tt&gt;{ &lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; keys = function(obj) &lt;tt&gt;
&lt;/tt&gt;  {&lt;tt&gt;
&lt;/tt&gt;    props=[];&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (k in ht) &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (ht.hasOwnProperty(k)) props.push(k);&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; props;&lt;tt&gt;
&lt;/tt&gt;  }&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;The simplistic unique random number generator looks like this&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;function uniqueRndNumbers(min, max, quantity) {&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; ht={}, i=quantity;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;while&lt;/span&gt; ( i&amp;gt;&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt; || keys(ht).length&amp;lt;quantity) &lt;tt&gt;
&lt;/tt&gt;    ht[Math.floor(Math.random()*(max-min+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;))+min]=i--;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; keys(ht);&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;This function has not undergone any serious testing. Also if the &lt;code&gt;quantity&lt;/code&gt; is more than a fraction of &lt;code&gt;(max-min)&lt;/code&gt; then another algorithm like the &lt;a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle"&gt;Fisher–Yates shuffle&lt;/a&gt; might be a better choice.&lt;/p&gt;</description>
      <pubDate>Fri, 05 Mar 2010 17:42:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:27e04911-d089-4122-8a96-ad529a9b8b3b</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2010/03/05/javascript-hash-table-keys</link>
      <category>JavaScript</category>
    </item>
    <item>
      <title>Ira Glass on Storytelling</title>
      <description>&lt;p&gt;I don't live on that same continent as &lt;a href="http://en.wikipedia.org/wiki/Ira_Glass"&gt;Ira Glass&lt;/a&gt; and because of that I can't listen to the wonderful &lt;em&gt;This American Life&lt;/em&gt; on the radio so instead I subscribe to the podcast. Every time a new episode shows up that is the first thing I listen to. There are a lot of great podcasts out there  but if I had to choose only one, I think I would go for &lt;a href="http://www.thisamericanlife.org/"&gt;This American Life&lt;/a&gt; (sorry &lt;a href="http://www.wnyc.org/shows/radiolab/"&gt;RadioLab&lt;/a&gt;, I love you). Recently I learned that the host and producer of the show, Ira Glass, can be found on YouTube talking about storytelling. 
It's amazing how he covers how he thinks you should tell a story for a radio/TV show. I recognize how the show executes that narrative but it also doesn't take anything away from the fact the show is excellent and that there are a lot of workmanship and talent put into it.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.youtube.com/watch?v=n7KQ4vkiNUk"&gt;Ira Glass on Storytelling #1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.youtube.com/watch?v=3qmtwa1yZRM"&gt;Ira Glass on Storytelling #2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.youtube.com/watch?v=-hidvElQ0xE"&gt;Ira Glass on Storytelling #3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.youtube.com/watch?v=9blgOboiGMQ"&gt;Ira Glass on Storytelling #4&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 03 Feb 2010 23:48:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:e807b540-0372-4aaf-a685-b02cbcd21598</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2010/02/03/ira-glass-on-storytelling</link>
      <category>Blogging</category>
    </item>
    <item>
      <title>C# implicit string conversion</title>
      <description>&lt;p&gt;I know how it works and I think I can see why but I'm still not very fond of how eager C# is to perform implicit string conversion.&lt;/p&gt;

&lt;p&gt;Contrived example:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;string s = -&lt;span style="color:#00D;font-weight:bold"&gt;42&lt;/span&gt; + &lt;span style="color:#04D"&gt;'+'&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;+&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + -&lt;span style="color:#60E;font-weight:bold"&gt;0&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.1&lt;/span&gt; / -&lt;span style="color:#60E;font-weight:bold"&gt;0&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.1&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;=&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + (&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt; ^ &lt;span style="color:#00D;font-weight:bold"&gt;5&lt;/span&gt;) + &lt;tt&gt;
&lt;/tt&gt;      &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt; is &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt; and not &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + AddressFamily.Unknown;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;s will be set to &lt;font color="white"&gt;"1+1=2 is True and not Unknown"&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;The answer is in white text above, select the text to see it.&lt;/p&gt;

&lt;p&gt;A more real problem is something like this&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;string str = &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt; + &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;!=&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt; + &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;code&gt;str&lt;/code&gt; will be set to "3!=12".&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red"&gt;Edit 2010-02-08&lt;/font&gt;&lt;br/&gt;
This wouldn't be much of a problem if all objects in .NET always returned a decent string representation of their current state/value with ToString() but that's not the case. &lt;a href="http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx"&gt;Instead&lt;/a&gt; "The default implementation returns the fully qualified name of the type of the Object.".&lt;br/&gt;
I don't like the inconsistency. It's way too late now but I think it would have been much better if only objects that really produces a human readable output of the data in the object should implement ToString(). If you want the name of the type of the Object there should be another way.&lt;/p&gt;</description>
      <pubDate>Wed, 03 Feb 2010 17:32:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:da6a1f72-b81c-4ee7-b5d1-8f7a821595e3</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2010/02/03/c-implicit-string-conversion</link>
      <category>C#</category>
    </item>
    <item>
      <title>String concatenation in Ruby</title>
      <description>&lt;p&gt;&lt;img src="http://upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Knit-schematic.png/200px-Knit-schematic.png" style="display: inline-block; float:right"/&gt;
There's no &lt;code&gt;StringBuilder&lt;/code&gt; class in Ruby because the &lt;a href="http://ruby-doc.org/core/classes/String.html"&gt;String&lt;/a&gt; class has the &lt;a href="http://ruby-doc.org/core/classes/String.html#M000807"&gt;&amp;lt;&amp;lt;&lt;/a&gt; for appending. The problem is that not every Ruby programmer seems to be aware of it. Recently I've seen &lt;code&gt;+=&lt;/code&gt; being used to append to strings where &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; would have been a much better choice.&lt;/p&gt;

&lt;p&gt;The problem with using &lt;code&gt;+=&lt;/code&gt; is that it creates a new String instance and if you do that in a loop you can get really horrible performance.&lt;/p&gt;

&lt;p&gt;If you are dealing with an array you don't even have to use &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; because &lt;a href="http://ruby-doc.org/core/classes/Array.html#M002182"&gt;Array#join&lt;/a&gt; is even faster and shows intent in a nice way.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;require &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;benchmark&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;array_of_random_strings=(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;...&lt;span style="color:#00D;font-weight:bold"&gt;262144&lt;/span&gt;).map{&lt;span style="color:#00D;font-weight:bold"&gt;65&lt;/span&gt;.+(rand(&lt;span style="color:#00D;font-weight:bold"&gt;25&lt;/span&gt;)).chr}.join.scan(&lt;span style="background-color:#fff0ff"&gt;&lt;span style="color:#404"&gt;/&lt;/span&gt;&lt;span style="color:#808"&gt;.{1,8}&lt;/span&gt;&lt;span style="color:#404"&gt;/&lt;/span&gt;&lt;span style="color:#C2C"&gt;m&lt;/span&gt;&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#036;font-weight:bold"&gt;Benchmark&lt;/span&gt;.bm &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt; |benchmark|&lt;tt&gt;
&lt;/tt&gt;  benchmark.report &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    str=array_of_random_strings.join&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  benchmark.report &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    str2=&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    array_of_random_strings.each &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt; |s|&lt;tt&gt;
&lt;/tt&gt;      str2&amp;lt;&amp;lt;s&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  benchmark.report &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    str3=&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    array_of_random_strings.each &lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt; |s|&lt;tt&gt;
&lt;/tt&gt;      str3+=s&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;


The array_of_random_strings is an array of 32768 8 characters long random strings.
&lt;br/&gt;
&lt;br/&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;
&lt;td&gt;&lt;b&gt;user&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;system&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;total&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;real&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;    
&lt;td&gt;0.030000&lt;/td&gt;&lt;td&gt;0.000000&lt;/td&gt;&lt;td&gt;0.030000&lt;/td&gt;&lt;td&gt;(  0.027184)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;0.160000&lt;/td&gt;&lt;td&gt;0.010000&lt;/td&gt;&lt;td&gt;0.170000&lt;/td&gt;&lt;td&gt;(  0.190277)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;106.020000&lt;/td&gt;&lt;td&gt;0.300000&lt;/td&gt;&lt;td&gt;106.320000&lt;/td&gt;&lt;td&gt;(113.457793)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;&lt;/p&gt;

&lt;p&gt;The performance of &lt;code&gt;+=&lt;/code&gt; was even &lt;strong&gt;worse&lt;/strong&gt; than I imagined!&lt;/p&gt;</description>
      <pubDate>Tue, 02 Feb 2010 00:04:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:08c4064d-9cef-4bee-8988-ea89961d87a2</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2010/02/02/string-concatenation-in-ruby</link>
      <category>Ruby</category>
    </item>
    <item>
      <title>Finding primes in parallel</title>
      <description>&lt;p&gt;&lt;a href="http://www.codethinked.com/page/About-Me.aspx"&gt;Justin Etheredge&lt;/a&gt; has been blogging about his &lt;a href="http://www.codethinked.com/post/2010/01/08/TekPubs-Mastering-LINQ-Challenge.aspx"&gt;challenge&lt;/a&gt; to find prime numbers with LINQ. He &lt;a href="http://www.codethinked.com/post/2010/01/10/The-TekPub-LINQ-Challenge-Part-2-Faster-Algorithms.aspx"&gt;later&lt;/a&gt; used &lt;code&gt;AsParallel()&lt;/code&gt; (coming in .NET 4) to speed things up and then followed that up with &lt;a href="http://www.codethinked.com/post/2010/01/12/The-TekPub-LINQ-Challenge-And-The-Sieve-Of-Eratosthenes.aspx"&gt;a post&lt;/a&gt; about using &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes"&gt;The Sieve Of Eratosthenes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As you can see in the comments of those posts I tried to speed the Sieve of Eratosthenes up by using &lt;code&gt;Parallel.For&lt;/code&gt; in the inner loop. I also tried AsParallel() in the LINQ expression but it made no difference in either case. At most it got 5% faster. I'm not sure but it could be that because SoE is very memory intense we could have a scaling issue and maybe also memory bandwidth exhaustion. This is mere speculation.&lt;/p&gt;

&lt;p&gt;I then searched for other algorithms and found &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Atkin"&gt;The Sieve of Atkin&lt;/a&gt;. It uses less memory than SoE so I thought I'd give it a try.&lt;/p&gt;

&lt;p&gt;I set the limit to 20,000,000 and then benchmarked it. It timed in on 2.48s so actually worse than the 2.2s that SoE took. Not good!
Then I added &lt;code&gt;Parallel.For&lt;/code&gt; in the loop that did most of the work and  lo and behold, it scaled! I have two cores in my machine (T7200@2.0GHz) and the average runtime went down to 1.26s. That's almost linear and surprisingly good! If you happen have a quad core (or more) and feel like trying it out then please contact me. It would be interesting to see if it scales further. &lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;33&lt;tt&gt;
&lt;/tt&gt;34&lt;tt&gt;
&lt;/tt&gt;35&lt;tt&gt;
&lt;/tt&gt;36&lt;tt&gt;
&lt;/tt&gt;37&lt;tt&gt;
&lt;/tt&gt;38&lt;tt&gt;
&lt;/tt&gt;39&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;40&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;41&lt;tt&gt;
&lt;/tt&gt;42&lt;tt&gt;
&lt;/tt&gt;43&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; List&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; FindPrimesBySieveOfAtkins(&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; max)&lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; isPrime = new BitArray((&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)max+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#038;font-weight:bold"&gt;false&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; sqrt = (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)Math.Sqrt(max);&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    Parallel.For(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, sqrt, x =&amp;gt;&lt;tt&gt;
&lt;/tt&gt;    {&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; xx = x * x;&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; y = &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;; y &amp;lt;= sqrt; y++)&lt;tt&gt;
&lt;/tt&gt;        {&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; yy = y * y;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; n = &lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt; * xx + yy;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (n &amp;lt;= max &amp;amp;&amp;amp; (n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt; || n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;5&lt;/span&gt;))&lt;tt&gt;
&lt;/tt&gt;                isPrime[n] = !isPrime[n];&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            n = &lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt; * xx + yy;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (n &amp;lt;= max &amp;amp;&amp;amp; n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;                isPrime[n] = !isPrime[n];&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            n = &lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt; * xx - yy;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (x &amp;gt; y &amp;amp;&amp;amp; n &amp;lt;= max &amp;amp;&amp;amp; n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;11&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;                isPrime[n] = !isPrime[n];&lt;tt&gt;
&lt;/tt&gt;        }&lt;tt&gt;
&lt;/tt&gt;    });&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; primes = new List&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt;() { &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt; };&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n = &lt;span style="color:#00D;font-weight:bold"&gt;5&lt;/span&gt;; n &amp;lt;= sqrt; n++)&lt;tt&gt;
&lt;/tt&gt;    {&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (isPrime[n])&lt;tt&gt;
&lt;/tt&gt;        {&lt;tt&gt;
&lt;/tt&gt;            primes.Add(n);&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; nn = n * n;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; k = nn; k &amp;lt;= max; k += nn)&lt;tt&gt;
&lt;/tt&gt;                isPrime[k] = &lt;span style="color:#038;font-weight:bold"&gt;false&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;        }&lt;tt&gt;
&lt;/tt&gt;    }&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n = sqrt + &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;; n &amp;lt;= max; n++)&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (isPrime[n])&lt;tt&gt;
&lt;/tt&gt;            primes.Add(n);&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; primes;&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;This is C# 4.0 code, compiled in Visual C# 2010 Express Beta 2.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit 2010-01-20&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Indications are that this does in fact not scale very good on a quad core. It's even worse, it seems it scales good on my old T7200 but not on a dual core E6320. I don't know why but of course the shared state of the &lt;strong&gt;isPrime&lt;/strong&gt; &lt;code&gt;BitArray&lt;/code&gt; is a huge problem and maybe it could be that differences in CPU architecture (FSB speed, caches and so on) in the E6320 is an explanation. Average execution time on the E6320 was 1290ms in a single thread and 1064ms in two.&lt;/p&gt;

&lt;p&gt;If you want to try this in an older version of C# than 4.0 then check out &lt;a href="http://coding-time.blogspot.com/2008/03/implement-your-own-parallelfor-in-c.html"&gt;this post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A reader asked how I timed the executions. Here's how.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; steps = new List&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;long&lt;/span&gt;&amp;gt;();&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; watch = new Stopwatch();&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; i = &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;; i &amp;lt; &lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;; i++) &lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;    watch.Reset();&lt;tt&gt;
&lt;/tt&gt;    watch.Start();&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; primes = FindPrimesBySieveOfAtkins(&lt;span style="color:#00D;font-weight:bold"&gt;20000000&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;    watch.Stop();&lt;tt&gt;
&lt;/tt&gt;    Console.WriteLine(watch.ElapsedMilliseconds.ToString());&lt;tt&gt;
&lt;/tt&gt;    steps.Add(watch.ElapsedMilliseconds);&lt;tt&gt;
&lt;/tt&gt;}&lt;tt&gt;
&lt;/tt&gt;Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Average: &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + steps.Average().ToString());&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;</description>
      <pubDate>Thu, 14 Jan 2010 22:55:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:2c932b6d-8c9c-4532-9b8f-85ea02fdd193</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel</link>
      <category>C#</category>
      <category>Java</category>
    </item>
    <item>
      <title>Comparing instance variables in Ruby</title>
      <description>&lt;p&gt;Say you have two objects of the same class and you want to know what differs between them. Well actually you just want to know the instance variables in object &lt;em&gt;&lt;strong&gt;b&lt;/strong&gt;&lt;/em&gt; that differs from the ones in object &lt;em&gt;&lt;strong&gt;a&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To begin with, we need a class. I like cheese.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;class&lt;/span&gt; &lt;span style="color:#B06;font-weight:bold"&gt;Cheese&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  attr_accessor &lt;span style="color:#A60"&gt;:name&lt;/span&gt;, &lt;span style="color:#A60"&gt;:weight&lt;/span&gt;, &lt;span style="color:#A60"&gt;:expire_date&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;def&lt;/span&gt; &lt;span style="color:#06B;font-weight:bold"&gt;initialize&lt;/span&gt;(name, weight, expire_date)&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#33B"&gt;@name&lt;/span&gt;, &lt;span style="color:#33B"&gt;@weight&lt;/span&gt;, &lt;span style="color:#33B"&gt;@expire_date&lt;/span&gt; = name, weight, expire_date&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;Then we need some &lt;strike&gt;cheese&lt;/strike&gt; objects.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;stilton=&lt;span style="color:#036;font-weight:bold"&gt;Cheese&lt;/span&gt;.new(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;Stilton&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;250&lt;/span&gt;, &lt;span style="color:#036;font-weight:bold"&gt;Date&lt;/span&gt;.parse(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;2009-11-02&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;))&lt;tt&gt;
&lt;/tt&gt;gorgonzola=&lt;span style="color:#036;font-weight:bold"&gt;Cheese&lt;/span&gt;.new(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;Gorgonzola&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;250&lt;/span&gt;, &lt;span style="color:#036;font-weight:bold"&gt;Date&lt;/span&gt;.parse(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;2009-11-17&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;))&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;With only &lt;em&gt;name&lt;/em&gt;, &lt;em&gt;weight&lt;/em&gt; and an &lt;em&gt;expiration date&lt;/em&gt; it would be easy to compare those but imagine that these two objects has 42 properties. It does not stop there, you are being asked to compare 24 different classes in this way. Are you cringing yet?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Object#instance_variables&lt;/code&gt; to the rescue! Well, that and a small hack by me. Below I add a new method called &lt;code&gt;instance_variables_compare&lt;/code&gt; to &lt;code&gt;Object&lt;/code&gt;. The long method name is because I wanted to follow the naming already in place. Usually I prefer to do these kind of things as a &lt;code&gt;module&lt;/code&gt; and then &lt;code&gt;include&lt;/code&gt; them where appropriate but in this case I find that a &lt;a href="http://en.wikipedia.org/wiki/Monkey_patch"&gt;monkey patch&lt;/a&gt; will do.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;class&lt;/span&gt; &lt;span style="color:#B06;font-weight:bold"&gt;Object&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;def&lt;/span&gt; &lt;span style="color:#06B;font-weight:bold"&gt;instance_variables_compare&lt;/span&gt;(o)&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#036;font-weight:bold"&gt;Hash&lt;/span&gt;[*&lt;span style="color:#038;font-weight:bold"&gt;self&lt;/span&gt;.instance_variables.map {|v|&lt;tt&gt;
&lt;/tt&gt;      &lt;span style="color:#038;font-weight:bold"&gt;self&lt;/span&gt;.instance_variable_get(v)!=o.instance_variable_get(v) ? &lt;tt&gt;
&lt;/tt&gt;      [v,o.instance_variable_get(v)] : []}.flatten]&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;It returns the instance variables that differs as a &lt;a href="http://ruby-doc.org/core/classes/Hash.html"&gt;hash&lt;/a&gt; because it's handy and because I like it that way.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&amp;gt;&amp;gt; stilton.instance_variables_compare(gorgonzola)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; {&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;@name&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;=&amp;gt;&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Gorgonzola&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;@expire_date&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;=&amp;gt;&lt;span style="color:#666"&gt;#&amp;lt;Date: 4910305/2,0,2299161&amp;gt;}&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&amp;gt;&amp;gt; gorgonzola.instance_variables_compare(stilton)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; {&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;@name&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;=&amp;gt;&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Stilton&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;@expire_date&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;=&amp;gt;&lt;span style="color:#666"&gt;#&amp;lt;Date: 4910275/2,0,2299161&amp;gt;}&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&amp;gt;&amp;gt; stilton.expire_date=gorgonzola.expire_date&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; &lt;span style="color:#666"&gt;#&amp;lt;Date: 4910305/2,0,2299161&amp;gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&amp;gt;&amp;gt; stilton.instance_variables_compare(gorgonzola)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; {&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;@name&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;=&amp;gt;&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Gorgonzola&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;}&lt;tt&gt;
&lt;/tt&gt;&amp;gt;&amp;gt; stilton.instance_variables_compare(stilton)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; {}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;If you ever think of using this code you should be aware of two things.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This code is very untested and comes with no guarantees.&lt;/li&gt;
&lt;li&gt;Since instance variables &lt;strong&gt;spring into life the first time they are assigned to&lt;/strong&gt; you either have to work with objects that always initialize everything or you have to change &lt;code&gt;instance_variables_compare&lt;/code&gt; to handle this.&lt;/li&gt;
&lt;/ol&gt;</description>
      <pubDate>Mon, 02 Nov 2009 22:50:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:655f1e0f-877a-4696-8057-8efd315ed457</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2009/11/02/comparing-instance-variables-in-ruby</link>
      <category>Ruby</category>
    </item>
    <item>
      <title>Infinite ranges in C#</title>
      <description>&lt;p&gt;I &lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/10/15/as-timeless-as-infinity.aspx"&gt;recently learned&lt;/a&gt; that C# is compliant with the &lt;a href="http://en.wikipedia.org/wiki/IEEE_754-1985"&gt;IEEE 754-1985&lt;/a&gt; for floating point arithmetics. That wasn't a big surprise but that &lt;a href="http://en.wikipedia.org/wiki/Division_by_zero#In_computer_arithmetic"&gt;division by zero&lt;/a&gt; is defined as &lt;code&gt;Infinity&lt;/code&gt; in it was! It actually kind of bothers me that I didn't know this.&lt;/p&gt;

&lt;p&gt;In mathematics division by zero is &lt;a href="http://en.wikipedia.org/wiki/Defined_and_undefined"&gt;undefined&lt;/a&gt; for real numbers but I guess &lt;code&gt;Infinity&lt;/code&gt; is a more pragmatic result. Or as a friend put it &lt;em&gt;"IEEE stands for Institute of Electrical and Electronics Engineers not Institute of Mathematics"&lt;/em&gt;&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt; n = &lt;span style="color:#60E;font-weight:bold"&gt;1&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;n = n / &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (n &amp;gt; &lt;span style="color:#00D;font-weight:bold"&gt;636413622384679305&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;  System.Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Yes it certainly is!&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;This C# code does not throw an exception, it simply leaves n defined as Infinity and a line written to the console.&lt;/p&gt;

&lt;p&gt;Ruby is &lt;a href="http://weblog.jamisbuck.org/2007/2/7/infinity"&gt;also&lt;/a&gt; IEEE 754-1985 compliant. It even lets you define &lt;a href="http://banisterfiend.wordpress.com/2009/10/02/wtf-infinite-ranges-in-ruby/"&gt;infinite ranges&lt;/a&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;=&lt;span style="color:#60E;font-weight:bold"&gt;1.0&lt;/span&gt;/&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;=&amp;gt;&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;).include?(&lt;span style="color:#00D;font-weight:bold"&gt;162259276829213363391578010288127&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;..&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;).step(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;).take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).inject(&amp;amp;&lt;span style="color:#A60"&gt;:+&lt;/span&gt;) &lt;span style="color:#666"&gt;# 7+14+21&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; &lt;span style="color:#00D;font-weight:bold"&gt;42&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;I can't say I see very much &lt;a href="http://www.michaelharrison.ws/weblog/?p=163"&gt;use&lt;/a&gt; of this but it brings a kind of completeness to the handling of infinities. Unfortunately it seems we don't get that in C# out of the box because &lt;code&gt;Enumerable.Range&lt;/code&gt; takes &lt;code&gt;&amp;lt;int&amp;gt;,&amp;lt;int&amp;gt;&lt;/code&gt; as parameters and there's no &lt;code&gt;Infinity&lt;/code&gt; definition for &lt;code&gt;int&lt;/code&gt;.  That's unless someone wrote a generic Range class. Turns out &lt;a href="http://stackoverflow.com/users/22656/jon-skeet"&gt;none other&lt;/a&gt; than &lt;a href="http://www.yoda.arachsys.com/csharp/"&gt;Jon Skeet&lt;/a&gt; did in his &lt;a href="http://www.yoda.arachsys.com/csharp/miscutil/"&gt;MiscUtil&lt;/a&gt;. Dowload MiscUtil and then by &lt;code&gt;using MiscUtil.Collections;&lt;/code&gt; you can:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt; n = &lt;span style="color:#60E;font-weight:bold"&gt;1&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; infinity = n / &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; r = new Range&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt;&amp;gt;(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;, infinity);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (r.Contains(&lt;span style="color:#00D;font-weight:bold"&gt;4711&lt;/span&gt;))&lt;tt&gt;
&lt;/tt&gt;  System.Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Yes it certainly does!&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; sum = r.Step(&lt;span style="color:#60E;font-weight:bold"&gt;7&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;).Take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).Sum();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;And guess what, it works like a charm! &lt;code&gt;4711&lt;/code&gt; is part of positive infinity and &lt;code&gt;sum&lt;/code&gt; is 42.0 and all is good.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;There's also a couple of predefined constants. Thanks to Eric for pointing that out.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; r = new Range&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt;&amp;gt;(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;,  System.Double.PositiveInfinity);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; sum = r.Step(&lt;span style="color:#60E;font-weight:bold"&gt;7&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;).Take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).Sum();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;</description>
      <pubDate>Tue, 20 Oct 2009 20:41:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:1337fd12-5058-4b35-bbf4-c9d0c4e9ade8</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2009/10/20/infinite-ranges-in-c</link>
      <category>Ruby</category>
      <category>C#</category>
      <category>Math</category>
    </item>
    <item>
      <title>Counting the number of Google Readers</title>
      <description>&lt;p&gt;I run this blog on a 9 year old laptop hidden in a cabinet in the living room. It's not a powerful machine but it has been up to the job since I turned it into a web server 7 years ago. This could maybe be one of the last HP Omnibook 4150b still in use, at least it has to be in a very exclusive club of laptops being switched on for the past 7.5 years. Recently I've seen an increase in traffic and especially from &lt;a href="http://www.google.com/feedfetcher.html#manycrawlers"&gt;Feedfetcher-Google&lt;/a&gt;. It so happens that Feedfetcher also shows the number of subscribers.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;[19/Oct/2009:22:01:19 +0200] "GET /xml/rss20/feed.xml HTTP/1.1" 304 0 "-" "Feedfetcher-Google; (+http://www.google.com/feedfetcher.html; 4 subscribers; feed-id=7686756599804593322)"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The above is only one out of five different feed-ids because I have both atom and rss and for a short while this blog was at another address. The fifth feed is actually myself subscribing to the comments.&lt;/p&gt;

&lt;p&gt;I'm not using &lt;a href="http://feedburner.google.com/fb/a/myfeeds"&gt;FeedBurner&lt;/a&gt; so I can't get my statistics from there but I still wanted to be able to see the number of Google Readers of my blog (as far as I can see I only have one other type of subscriber).&lt;/p&gt;

&lt;p&gt;Usually I script anything more advanced than a &lt;code&gt;grep&lt;/code&gt; in &lt;a href="http://www.ruby-lang.org/en/"&gt;Ruby&lt;/a&gt; but this time I made an exception and stayed in &lt;code&gt;Bash&lt;/code&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;tail &lt;span style="color:#00D;font-weight:bold"&gt;-1000&lt;/span&gt; /www/logs/access.log |&lt;tt&gt;
&lt;/tt&gt;grep &lt;span style="color:#036;font-weight:bold"&gt;Feedfetcher&lt;/span&gt; |&lt;tt&gt;
&lt;/tt&gt;cut -d &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;;&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; -f &lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt; | sort -u |&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;while&lt;/span&gt; &lt;span style="color:#036;font-weight:bold"&gt;IFS&lt;/span&gt;= read -r line&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;do&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;   tac &lt;span style="background-color:#fff0ff"&gt;&lt;span style="color:#404"&gt;/&lt;/span&gt;&lt;span style="color:#808"&gt;www&lt;/span&gt;&lt;span style="color:#404"&gt;/&lt;/span&gt;&lt;/span&gt;logs/access.log | grep -m &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt; &lt;span style="color:#d70;font-weight:bold"&gt;$line&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;done |&lt;tt&gt;
&lt;/tt&gt;sed &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;s/^.*html; &lt;/span&gt;&lt;span style=""&gt;\(&lt;/span&gt;&lt;span style=""&gt;[0-9]*&lt;/span&gt;&lt;span style=""&gt;\)&lt;/span&gt;&lt;span style=""&gt; subscribers.*/&lt;/span&gt;&lt;span style=""&gt;\1&lt;/span&gt;&lt;span style=""&gt;/&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt; |&lt;tt&gt;
&lt;/tt&gt;awk &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;span style=""&gt;{tot=tot+$1} END {print tot}&lt;/span&gt;&lt;span style="color:#710"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Most certainly this can be optimized in a number of ways. Don't be shy, just tell me!&lt;/p&gt;

&lt;p&gt;So what's going on there? Well, first I get the last 1000 rows from my access log and right now my traffic is so low that that is way more than I really would have to. Then I get all unique feeed-ids from the rows containing Feedfetcher. I pipe those to a loop that gets the very last access for each one of them. Then I parse out the number of subscribers with a regexp in &lt;code&gt;sed&lt;/code&gt; and count them with &lt;code&gt;awk&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;It turns out that I have a whopping number of &lt;strike&gt;14&lt;/strike&gt; 15 subscribers and I am one of them.&lt;/p&gt;</description>
      <pubDate>Mon, 19 Oct 2009 21:55:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:f06b0424-8326-4281-a812-953270e3b23d</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2009/10/19/counting-the-number-of-google-readers</link>
      <category>Blogging</category>
      <category>Bash</category>
    </item>
    <item>
      <title>The Thrush combinator in C#</title>
      <description>&lt;p&gt;Last year I read &lt;a href="http://github.com/raganwald/homoiconic/blob/master/README.markdown"&gt;Reg "Raganwald'" Braithwaite's&lt;/a&gt; excellent post &lt;a href="http://github.com/raganwald/homoiconic/blob/master/2008-10-30/thrush.markdown#readme"&gt;The Thrush&lt;/a&gt; and he  explains it as &lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;The thrush is written Txy = yx. It reverses evaluation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Back then I didn't even consider trying to implement it in C#. That was before I digged deeper into &lt;a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx"&gt;lambda expressions&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx"&gt;extension methods&lt;/a&gt; in C# 3.0 and way before last night when I read Debasish Ghosh's post on how to &lt;a href="http://debasishg.blogspot.com/2009/09/thrush-combinator-in-scala.html"&gt;implement the Thrush in Scala&lt;/a&gt;. After reading that my first thought was if it was possible to do the same in C#. Here's my attempt.&lt;/p&gt;

&lt;p&gt;At first I struggled with the static typing and headed for an easy way out using Object in the extension method of Object:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; object Into(this Object obj, Func&amp;lt;object, object&amp;gt; f)&lt;tt&gt;
&lt;/tt&gt;{  &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; f.Invoke(obj); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;My goal was to translate the Ruby example&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).select(&amp;amp;&lt;span style="color:#A60"&gt;:odd?&lt;/span&gt;).inject(&amp;amp;&lt;span style="color:#A60"&gt;:+&lt;/span&gt;).into { |x| x * x }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;in Raganwald's post to C#.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;Which reads "Take the numbers from 1 to 100, keep the odd ones, take the sum of those, and then answer the square of that number."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But with the Object based extension method I had to do some ugly casts.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; r = Enumerable.Range(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).Where(x =&amp;gt; Odd(x)).Sum().Into(x =&amp;gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)x * (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)x);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;With som added typing I could do: &lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; result = Enumerable.Range(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).Where(x =&amp;gt; Odd(x)).Sum().Into(x =&amp;gt; x * x);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;But that merely moved the cast to the ext. method and also made it work for integers only.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; Into(this Object obj, Func&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;, &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; f)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; f.Invoke((&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)obj); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;Then I remembered generics and method type inference.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; T Into&amp;lt;T&amp;gt;(this T obj, Func&amp;lt;T, T&amp;gt; f)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; f(obj); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;And now not only the casts were gone but I also got a thrush combinator almost as flexible as the one in Ruby.&lt;/p&gt;

&lt;p&gt;Contrived example follows:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; test = &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;ball&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; ball = test.Into(s =&amp;gt; &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Are we having a &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + s + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt; yet?&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt; &lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;The odd part&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Odd(x)&lt;/em&gt; method call in the calculation above is a plain static method.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;private &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;bool&lt;/span&gt; Odd(&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; (n % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; != &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;If you want an even more terse syntax you could try an ext. method on IEnumerable like this:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; Odd(this IEnumerable&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; en)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; en.Where(n =&amp;gt; n % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; != &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Gives:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; result = Enumerable.Range(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).Odd().Sum().Into(x =&amp;gt; x * x);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;Also as a general alternative to &lt;code&gt;.Sum()&lt;/code&gt; I could have used &lt;code&gt;.Aggregate((x, y) =&amp;gt; x + y))&lt;/code&gt; but I found it a bit verbose.&lt;/p&gt;

&lt;p&gt;In C# I don't think it's possible to pull off the Symbol#to_proc stuff that Ruby does. That's the &lt;em&gt;&amp;amp;:&lt;/em&gt; in the &lt;em&gt;select(&amp;amp;:odd?)&lt;/em&gt; and the &lt;em&gt;inject(&amp;amp;:+)&lt;/em&gt; in the Ruby example. Raganwald has a great &lt;a href="http://weblog.raganwald.com/2007/11/fun-with-symboltoproc.html"&gt;post&lt;/a&gt; on that too.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Check out &lt;a href="http://stackoverflow.com/questions/1528319/operators-as-method-parameters-in-c-and-the-thrush-combinator"&gt;Jon Skeet's nice answer&lt;/a&gt; on StackOverflow to my question on how to make this even more Ruby-like. I have to try out that Operator class later though.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit 2009-10-07&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;One thing I found a bit surprising is that by implementing the Into ext. method in this way it not only works for all objects based on &lt;code&gt;System.Object&lt;/code&gt; but it also works  for &lt;a href="http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx"&gt;value types&lt;/a&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n=&lt;span style="color:#00D;font-weight:bold"&gt;4711&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; oddOrZero = n.Into(x =&amp;gt; x % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; !=&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt; ? x : &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); &lt;span style="color:#666"&gt;// 4711&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;n = &lt;span style="color:#00D;font-weight:bold"&gt;4712&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;oddOrZero = n.Into(x =&amp;gt; x % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; != &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt; ? x : &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); &lt;span style="color:#666"&gt;// 0&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit 2009-10-12&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;My confusion did stem from my lack of understanding of &lt;a href="http://msdn.microsoft.com/en-us/library/bb383977(loband).aspx"&gt;extension methods&lt;/a&gt;. Ex. methods are in fact not extending &lt;code&gt;System.Object&lt;/code&gt; or any other type, they are "&lt;a href="http://blogs.msdn.com/ericlippert/about.aspx"&gt;nothing more than a pleasant syntax for calling a static method&lt;/a&gt;" in  case no instance method with the same name can be found.&lt;/p&gt;</description>
      <pubDate>Tue, 06 Oct 2009 20:35:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:b02ff8f0-44fb-44ca-a309-eb0fd074a495</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2009/10/06/the-thrush-combinator-in-c</link>
      <category>Ruby</category>
      <category>C#</category>
    </item>
    <item>
      <title>A case for using only three different digits in keypad codes</title>
      <description>&lt;p&gt;Keypads have obvious security problems and keypads accepting a stream of digits with no # or enter in between, while checking for the four digit long code, are even worse.&lt;/p&gt;

&lt;p&gt;The important part is to not leak the digits in the code &lt;a href="http://www.schneier.com/blog/archives/2009/07/information_lea_1.html"&gt;by wear&lt;/a&gt; or intentional markings because if they leak it's suddenly very far from 10000 combinations.&lt;/p&gt;

&lt;p&gt;If the "lock picker" only knows that the code contains four digits there are 10000 combinations. Keypads accepting a stream of digits can then be opened in a maximum of 10003 keystrokes using the &lt;a href="http://en.wikipedia.org/wiki/De_Bruijn_sequence#Uses"&gt;De
Bruijn sequence&lt;/a&gt;. That is still quite a lot.&lt;/p&gt;

&lt;p&gt;Below is a Ruby implementation of the &lt;a href="http://www.hakank.org/comb/debruijn.cgi"&gt;De
Bruijn sequence&lt;/a&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#666"&gt;# De Bruijn sequence&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#666"&gt;# Original implementation by Frank Ruskey (1994)&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#666"&gt;# translated to C by Joe Sawada&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#666"&gt;# and further translated to Ruby by Jonas Elfström (2009)&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@n&lt;/span&gt;=&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@k&lt;/span&gt;=&lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@a&lt;/span&gt;=[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#33B"&gt;@sequence&lt;/span&gt;=[]&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;def&lt;/span&gt; &lt;span style="color:#06B;font-weight:bold"&gt;debruijn&lt;/span&gt;(t, p, alike)&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; t&amp;gt;&lt;span style="color:#33B"&gt;@n&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; &lt;span style="color:#33B"&gt;@n&lt;/span&gt;%p==&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;      &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;.upto(p) {|j| &lt;span style="color:#33B"&gt;@sequence&lt;/span&gt;&amp;lt;&amp;lt;&lt;span style="color:#33B"&gt;@a&lt;/span&gt;[j]}&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;else&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#33B"&gt;@a&lt;/span&gt;[t]=&lt;span style="color:#33B"&gt;@a&lt;/span&gt;[t-p]&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; &lt;span style="color:#33B"&gt;@a&lt;/span&gt;[t]&amp;gt;&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;      debruijn(t+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;,p,alike+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;else&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;      debruijn(t+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;,p,alike)&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    (&lt;span style="color:#33B"&gt;@a&lt;/span&gt;[t-p]+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;).upto(&lt;span style="color:#33B"&gt;@k&lt;/span&gt;-&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;) {|j|&lt;tt&gt;
&lt;/tt&gt;      &lt;span style="color:#33B"&gt;@a&lt;/span&gt;[t]=j&lt;tt&gt;
&lt;/tt&gt;      debruijn(t+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;,t,alike+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;    }&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;debruijn(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;,&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;,&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;print &lt;span style="color:#33B"&gt;@sequence&lt;/span&gt;.join&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;

&lt;p&gt;It's not uncommon to find keypads with 4 of the 10 keys worn down and if you do you can be pretty sure that the code contains those four different digits. The number of possible combinations are 4! = 4x3x2x1 = 24. I got curious to see if there's a kind of &lt;a href="http://mathworld.wolfram.com/deBruijnSequence.html"&gt;De Bruijn sequence&lt;/a&gt; for this that brings down the 4*24=96 keystrokes. By scribbling in a &lt;a href="http://www.scintilla.org/SciTE.html"&gt;text editor&lt;/a&gt; I quickly realized there's not a clean sequence. Not clean in the way that a sequence following the rules can be created. Also it's probably even quite daunting to present it as mathematically dense and beautiful as the &lt;a href="http://www.stefangeens.com/2004/10/the-de-bruijn-c.html"&gt;De Bruijn&lt;/a&gt; but that could be my less than great &lt;a href="http://en.wikipedia.org/wiki/Combinatorics"&gt;combinatorics&lt;/a&gt; speaking.&lt;/p&gt;

&lt;p&gt;I made a quick and dirty brute force hack to try to find a shorter sequence.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;seq=[]&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;.upto(&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;) {|a| &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;.upto(&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;) {|b| &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;.upto(&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;) {|c| &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;.upto(&lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt;) {|d|&lt;tt&gt;
&lt;/tt&gt;  seq &amp;lt;&amp;lt; &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;%d%d%d%d&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; % [a,b,c,d] &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; !(a==b || a==c || a==d || b==c || b==d || c==d)&lt;tt&gt;
&lt;/tt&gt;}}}}&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;s=seq[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]&lt;tt&gt;
&lt;/tt&gt;seq.delete_at(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;while&lt;/span&gt; (seq.length&amp;gt;&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;  next_code=(seq.select {|c| c[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;]==s[&lt;span style="color:#00D;font-weight:bold"&gt;-3&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;-1&lt;/span&gt;]})&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; next_code.empty?&lt;tt&gt;
&lt;/tt&gt;    next_code=(seq.select {|c| c[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;]==s[&lt;span style="color:#00D;font-weight:bold"&gt;-2&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;-1&lt;/span&gt;]})&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; next_code.empty? &lt;tt&gt;
&lt;/tt&gt;      next_code=(seq.select {|c| c[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]==s[&lt;span style="color:#00D;font-weight:bold"&gt;-1&lt;/span&gt;]})&lt;tt&gt;
&lt;/tt&gt;      s+=next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;][&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;]&lt;tt&gt;
&lt;/tt&gt;      seq.delete_at(seq.index(next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]))&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;else&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;      s+=next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;][&lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;]&lt;tt&gt;
&lt;/tt&gt;      seq.delete_at(seq.index(next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]))&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;else&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    next_code=(seq.select {|c| c[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;]==s[&lt;span style="color:#00D;font-weight:bold"&gt;-3&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;-1&lt;/span&gt;]})&lt;tt&gt;
&lt;/tt&gt;    s+=next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;][&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;].chr&lt;tt&gt;
&lt;/tt&gt;    seq.delete_at(seq.index(next_code[&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;]))&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;The above code takes the first code "1234" of the 24 and then searches the rest of the array for a code beginning with "234". It finds "2341" and adds "1" to the end of &lt;em&gt;s&lt;/em&gt; and continues to look for "341" and so on. Relatively soon there is no three digit match and then it tries two digits and eventually even that fails and then it gets the first one digit match. The resulting sequence is: &lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;123412314231243121342132413214321&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From 96 to 33 keystrokes. Not as effective as De Bruijn but still significant. Unlike De Bruijn I have absolutely no proof that this is the shortest one possible but it seems likely. Also notice that in the middle of the sequence we find "3121" and "1213". Those break the criteria of four different digits but they seem to be necessary to be able to enter the reversed mode. Try reading the sequence forward and backwards to see what I mean.&lt;/p&gt;

&lt;p&gt;If the code only contains two digits it's gets even more trivial to try them all. There are 14 possible codes and by &lt;em&gt;compressing&lt;/em&gt; those to one sequence you get down to 20 keystrokes. &lt;/p&gt;

&lt;p&gt;Things get a little more interresting if three buttons are worn. It turns out that the repeated digits can be placed in the code in six different ways.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;0012,1002,1200,0102,0120,1020&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's 6x2x3=36 combinations and, maybe a little unintuitive, 12 more than if you are using four different digits. I compressed it down to 49 key strokes. Unlike the sequence for four different digits I can't find it with google and I know it's kind of security by obscurity but that could give a tiny amount of extra trouble to an attacker. I will not be the first one publishing it.&lt;/p&gt;

&lt;p&gt;Be aware that if an attacker knows you are using a 0012-like code he gets a smaller space to search. 6x8x9x10=4320 instead of 10000. You have to weight the risk of button leaks against a code protocol leak.&lt;/p&gt;</description>
      <pubDate>Sun, 27 Sep 2009 21:02:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:9e10d05b-1a1d-46a1-82da-0ba366d32035</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2009/09/27/a-case-for-using-only-three-different-digits-in-keypad-codes</link>
      <category>Security</category>
      <category>Ruby</category>
    </item>
  </channel>
</rss>
