<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>porges &#187; Cool</title>
	<atom:link href="http://porg.es/blog/tag/cool/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description></description>
	<lastBuildDate>Sun, 06 May 2012 22:13:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Fun(c) with C# 3.0</title>
		<link>http://porg.es/blog/func-with-c-30</link>
		<comments>http://porg.es/blog/func-with-c-30#comments</comments>
		<pubDate>Tue, 09 Oct 2007 02:03:31 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Cool]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://porg.es/blog/func-with-c-30</guid>
		<description><![CDATA[Looking through the list of predefined (or, in Microsoft’s parlance, standard) query operators defined in C# 3.0, there is one that stands out as missing: the ‘map’ function. However, with the new query expression syntax, this is trivial to define: public static IEnumerable&#60;T&#62; Map&#60;F,T&#62;&#40;Func&#60;F,T&#62; func, IEnumerable&#60;F&#62; source&#41; &#123; return from it in source select func&#40;it&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>Looking through the list of predefined (or, in Microsoft’s parlance, standard) <a href="http://download.microsoft.com/download/5/8/6/5868081c-68aa-40de-9a45-a3803d8134b8/standard_query_operators.doc">query operators defined in C# 3.0</a>, there is one that stands out as missing: the ‘map’ function. However, with the new query expression syntax, this is trivial to define:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Map<span style="color: #008000;">&lt;</span>F,T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>F,T<span style="color: #008000;">&gt;</span> func, IEnumerable<span style="color: #008000;">&lt;</span>F<span style="color: #008000;">&gt;</span> source<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span>
        <span style="color: #0600FF; font-weight: bold;">from</span> it <span style="color: #0600FF; font-weight: bold;">in</span> source
        <span style="color: #0600FF; font-weight: bold;">select</span> func<span style="color: #008000;">&#40;</span>it<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Actually, it’s trivial to implement without the new syntax (although I’ve left in <code>Func</code> because it’s nicer than defining a separate delegate):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Map<span style="color: #008000;">&lt;</span>F,T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>F,T<span style="color: #008000;">&gt;</span> func, IEnumerable<span style="color: #008000;">&lt;</span>F<span style="color: #008000;">&gt;</span> <span style="color: #0600FF; font-weight: bold;">from</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>F it <span style="color: #0600FF; font-weight: bold;">in</span> <span style="color: #0600FF; font-weight: bold;">from</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> func<span style="color: #008000;">&#40;</span>it<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>But why stop at <code>map</code>? We can also define a fold:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T FoldL<span style="color: #008000;">&lt;</span>F, T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T, F, T<span style="color: #008000;">&gt;</span> func, IEnumerable<span style="color: #008000;">&lt;</span>F<span style="color: #008000;">&gt;</span> source, T init<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    T last <span style="color: #008000;">=</span> init<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>F it <span style="color: #0600FF; font-weight: bold;">in</span> source<span style="color: #008000;">&#41;</span>
        last <span style="color: #008000;">=</span> func<span style="color: #008000;">&#40;</span>last, it<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> last<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Or the non-empty-list variant. This is made easier through the query operator <code>Skip</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T FoldL1<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T, T, T<span style="color: #008000;">&gt;</span> func, IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> source<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    T last <span style="color: #008000;">=</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>T it <span style="color: #0600FF; font-weight: bold;">in</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">Skip</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        last <span style="color: #008000;">=</span> func<span style="color: #008000;">&#40;</span>last, it<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> last<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Finally, a simple compose operator for sticking two functions together, again using the nicer syntax to make this simple:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Func<span style="color: #008000;">&lt;</span>X,Z<span style="color: #008000;">&gt;</span> Compose<span style="color: #008000;">&lt;</span>X,Y,Z<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Func<span style="color: #008000;">&lt;</span>Y,Z<span style="color: #008000;">&gt;</span> second, Func<span style="color: #008000;">&lt;</span>X,Y<span style="color: #008000;">&gt;</span> first<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> x <span style="color: #008000;">=&gt;</span> second<span style="color: #008000;">&#40;</span>first<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h4>Example</h4>
<p>A simple example including functionality from each part above (I put everything into an <code>FFC</code> utility class):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> numbers <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">4</span>, <span style="color: #FF0000;">5</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
Func<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span>, <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> addOne <span style="color: #008000;">=</span> n <span style="color: #008000;">=&gt;</span> n <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var k <span style="color: #0600FF; font-weight: bold;">in</span> FFC<span style="color: #008000;">.</span><span style="color: #0000FF;">Map</span><span style="color: #008000;">&#40;</span>addOne, numbers<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>k <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Func<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span>,<span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> addTwo <span style="color: #008000;">=</span> addOne<span style="color: #008000;">.</span><span style="color: #0000FF;">Compose</span><span style="color: #008000;">&#40;</span>addOne<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var k <span style="color: #0600FF; font-weight: bold;">in</span> FFC<span style="color: #008000;">.</span><span style="color: #0000FF;">Map</span><span style="color: #008000;">&#40;</span>addTwo, numbers<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>k <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var sum <span style="color: #008000;">=</span> FFC<span style="color: #008000;">.</span><span style="color: #0000FF;">FoldL</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>x, y<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">+</span> y, numbers, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>sum <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/func-with-c-30/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lazy Lists in C#</title>
		<link>http://porg.es/blog/lazy-lists-in-c</link>
		<comments>http://porg.es/blog/lazy-lists-in-c#comments</comments>
		<pubDate>Thu, 27 Sep 2007 21:23:36 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Cool]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://porg.es/blog/lazy-lists-in-c</guid>
		<description><![CDATA[I had the thought&#8212;while browsing through some old code&#8212;that the code I used to implement futures in C# would be useful for doing things lazily, if you just moved the evaluation phase to when the value was actually demanded&#8230; this started me off thinking about how to implement a proper lazily-evaluated list in C#. A [...]]]></description>
			<content:encoded><![CDATA[<p>I had the thought&mdash;while browsing through some old code&mdash;that the code I used to <a href="http://porg.es/blog/implementing-futures-in-c">implement futures in C#</a> would be useful for doing things lazily, if you just moved the evaluation phase to when the value was actually demanded&#8230; this started me off thinking about how to implement a proper lazily-evaluated list in C#.</p>
<h4>A first attempt</h4>
<p>The first thing I thought of was to implement them using an IEnumerable with yield statements, so I began with a construction borrowed from Haskell: the iterate function.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> FakeLazyList
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Iterate<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T, T<span style="color: #008000;">&gt;</span> f, T x<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> x<span style="color: #008000;">;</span>
            x <span style="color: #008000;">=</span> f<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>While this works (try <code lang="csharp">FakeLazyList.Iterate(i => i + 1, 0)</code>), it is obviously a poor solution; it is not flexible as it requires a seperate function to be written for every incarnation.</p>
<h4>Lazy data in general</h4>
<p>My next approach was to begin with creating a generic type for lazy data. All it has to do is act as a wrapper for a piece of data. However, since in C# arguments must be evaluated before they are used, the data must in turn be wrapped inside a delegate so that the delegate&#8217;s execution can be postponed. This resulted in:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> Lazy<span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> del<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Del <span style="color: #008000;">=</span> del<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Del<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> T PValue<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> HasValue <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">implicit</span> <span style="color: #0600FF; font-weight: bold;">operator</span> T<span style="color: #008000;">&#40;</span>Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> f<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>f<span style="color: #008000;">.</span><span style="color: #0000FF;">HasValue</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            f<span style="color: #008000;">.</span><span style="color: #0000FF;">PValue</span> <span style="color: #008000;">=</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">Del</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            f<span style="color: #008000;">.</span><span style="color: #0000FF;">HasValue</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">PValue</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As you can see, execution of the data-bearing delegate is delayed until the lazy data is forced to become normal data.</p>
<h4>Implementing the list</h4>
<p>For the list implementation itself, I chose to go with the traditional cons list structure, so that each list item has an object of data, and a pointer to the next item in the list. In order to make the list lazy, the next item of the list should be generated only on-access, so it is wrapped in the above <code>Lazy&lt;T&gt;</code> class.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>, IEnumerable
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> T _First<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Lazy<span style="color: #008000;">&lt;</span>LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> _Rest<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> T First <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _First<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Rest <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _Rest<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> LazyList<span style="color: #008000;">&#40;</span>T first, Lazy<span style="color: #008000;">&lt;</span>LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> rest<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        _First <span style="color: #008000;">=</span> first<span style="color: #008000;">;</span> _Rest <span style="color: #008000;">=</span> rest<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Next comes some boilerplate so that the class can be used as an enumerable structure:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Enumerator<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    IEnumerator IEnumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Enumerator<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">class</span> Enumerator <span style="color: #008000;">:</span> IEnumerator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>, IEnumerator
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> curr <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> next<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Enumerator<span style="color: #008000;">&#40;</span>LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> parent<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            next <span style="color: #008000;">=</span> parent<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080; font-style: italic;">/* must implement both interfaces */</span>
        <span style="color: #6666cc; font-weight: bold;">object</span> IEnumerator<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span> <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> T Current <span style="color: #008000;">&#123;</span>
                get <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>curr <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                        <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">else</span>
                        <span style="color: #0600FF; font-weight: bold;">return</span> curr<span style="color: #008000;">.</span>_First<span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> MoveNext<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> curr <span style="color: #008000;">=</span> next<span style="color: #008000;">;</span> next <span style="color: #008000;">=</span> next<span style="color: #008000;">.</span>_Rest<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">return</span> curr <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Reset<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> NotSupportedException<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Last of all comes a couple of utility functions for constructing lists, both of which I took from Haskell. The first, <code>iterate</code>, generates a list of items by taking a function <code>f</code> and a datum <code>x</code> and generating the list like this <code>[x,f(x),f(f(x)),f(f(f(x))),...]</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span> Iterate<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>X, X<span style="color: #008000;">&gt;</span> f, X x<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>x, <span style="color: #008000;">new</span> Lazy<span style="color: #008000;">&lt;</span>LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> Iterate<span style="color: #008000;">&#40;</span>f, f<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>The next takes a datum and generates the infinite list of just that datum:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span> Repeat<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>X x<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>x, <span style="color: #008000;">new</span> Lazy<span style="color: #008000;">&lt;</span>LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> Repeat<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As you can see, the usage of the <code>LazyList</code> constructor isn&#8217;t the most elegant. I had hoped that I would be able to use an implicit operator on the <code>Lazy</code> class, such as:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">implicit</span> <span style="color: #0600FF; font-weight: bold;">operator</span> Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> f<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>f<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&#8230; and then use it like this &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">new</span> LazyList<span style="color: #008000;">&lt;</span>X<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>x, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Repeat<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Unfortunately, this doesn&#8217;t work, giving error <a href="http://msdn2.microsoft.com/en-us/library/hy74she2(vs.80).aspx">CS1660</a>. Apparently the C# compiler cannot use the fact that an implicit cast has been defined. Still, this implementation works well.</p>
<h4>Usage</h4>
<p>The usage of the <code>LazyList</code> in normal code is very simple, and as shown in this example, you can also use the new C# 3.0-defined list operators <code>Take</code>, <code>Drop</code>, <code>Where</code> etc:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> ys<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Lazy list of all positive integers</span>
ys <span style="color: #008000;">=</span> LazyList<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;.</span><span style="color: #0000FF;">Iterate</span><span style="color: #008000;">&#40;</span>i <span style="color: #008000;">=&gt;</span> i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Take</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var y <span style="color: #0600FF; font-weight: bold;">in</span> ys<span style="color: #008000;">&#41;</span> Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>y<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//Lazy list of [89, 89, 89, 89, ...]</span>
ys <span style="color: #008000;">=</span> LazyList<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;.</span><span style="color: #0000FF;">Repeat</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">89</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Take</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var y <span style="color: #0600FF; font-weight: bold;">in</span> ys<span style="color: #008000;">&#41;</span> Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>y<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/lazy-lists-in-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Computing with XSLT</title>
		<link>http://porg.es/blog/computing-with-xslt</link>
		<comments>http://porg.es/blog/computing-with-xslt#comments</comments>
		<pubDate>Thu, 19 Jul 2007 02:34:18 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://porg.es/blog/computing-with-xslt</guid>
		<description><![CDATA[Since XSLT is basically a pattern-matching functional programming language, we should be able to use it to compute. I&#8217;m going to use it to implement one of the most basic functions: Peano-style addition. First of all we have to have an idea of what the numbers look like. We want to simulate this in XSL: [...]]]></description>
			<content:encoded><![CDATA[<p>Since XSLT is basically a pattern-matching functional programming language, we should be able to use it to compute. I&#8217;m going to use it to implement one of the most basic functions: Peano-style addition.</p>
<p>First of all we have to have an idea of what the numbers look like. We want to simulate this in XSL:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> Nat <span style="color: #339933; font-weight: bold;">=</span> Zero <span style="color: #339933; font-weight: bold;">|</span> Succ Nat</pre></div></div>

<p>Using XSD we can have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;complexType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Nat&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;choice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Zero&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Succ&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Nat&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/choice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><small>(Note that this is not tested, and probably wrong!)</small></p>
<p>However, since I don&#8217;t have an XSLT processor on hand that can deal with XSD types, I had to drop this course of action (although I still used the introduced type for the data). The type gives data like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;Zero</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&#8230;in this case standing for the number two. Now the idea of addition on these numbers is given by a function:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">add Zero y <span style="color: #339933; font-weight: bold;">=</span> y
add <span style="color: green;">&#40;</span>Succ x<span style="color: green;">&#41;</span> y <span style="color: #339933; font-weight: bold;">=</span> Succ <span style="color: green;">&#40;</span>add x y<span style="color: green;">&#41;</span></pre></div></div>

<p>Since this function involves pattern-matching it is an ideal candidate for implementation in XSLT, and it looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span> </span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- apply the add function to two numbers --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;add&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;add&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;*[1]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;*[2]&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:apply-templates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- add Succ(x) y = Succ(add x y) --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;addSucc&quot;</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;Succ&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:apply-templates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Succ<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- add Zero y = y --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;addZero&quot;</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;adder&quot;</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;Zero&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$rest&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The first template matches any occurances of <code>&lt;add/&gt;</code>, and calls the real &#8216;add&#8217; function on them. It passes the second number as a parameter, while the first is what the function actually recurses on&mdash;exactly as in the Haskell example. The rest proceeds similarly.</p>
<p>You can <a href="http://www.w3.org/2005/08/online_xslt/xslt?xslfile=http%3A%2F%2Fporg.es%2Fprocessor.xsl&#038;xmlfile=http%3A%2F%2Fporg.es%2Fdata.xml&#038;content-type=&#038;submit=transform">test it yourself</a> by using the W3C online XSLT processor, which uses <a href="http://porg.es/processor.xsl">my processor</a> and <a href="http://porg.es/data.xml">some sample input data</a>. It should work for any similar data that you can throw at it as well. In this case it is adding 2 + 2 and coming up with 4, as we expect <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/computing-with-xslt/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Firefox extension: CustomizeGoogle</title>
		<link>http://porg.es/blog/firefox-extension-customizegoogle</link>
		<comments>http://porg.es/blog/firefox-extension-customizegoogle#comments</comments>
		<pubDate>Sun, 20 May 2007 02:20:26 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Asides]]></category>
		<category><![CDATA[Cool]]></category>
		<category><![CDATA[Firefox]]></category>

		<guid isPermaLink="false">http://porg.es/blog/firefox-extension-customizegoogle</guid>
		<description><![CDATA[The latest version of CustomizeGoogle is great. I particularly recommend the (currently ‘in beta’) &#8220;stream search results&#8221; feature. When you would normally reach the bottom of a page in Google, the extension appends the next page onto the bottom, so you&#8217;ll never have to click &#8220;next&#8221; again!]]></description>
			<content:encoded><![CDATA[<p>The latest version of <a href="http://www.customizegoogle.com/">CustomizeGoogle</a> is great.</p>
<p>I particularly recommend the (currently ‘in beta’) &#8220;stream search results&#8221; feature. When you would normally reach the bottom of a page in Google, the extension appends the next page onto the bottom, so you&#8217;ll never have to click &#8220;next&#8221; again! <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/firefox-extension-customizegoogle/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exponential Bags: Integration of Types, continued</title>
		<link>http://porg.es/blog/exponential-bags-integration-of-types-continued</link>
		<comments>http://porg.es/blog/exponential-bags-integration-of-types-continued#comments</comments>
		<pubDate>Wed, 11 Apr 2007 10:49:16 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Odd]]></category>
		<category><![CDATA[Thought]]></category>

		<guid isPermaLink="false">http://porg.es/blog/exponential-bags-integration-of-types-continued</guid>
		<description><![CDATA[Derivatives of Containers claims to look for the &#8216;container which is its own derivative&#8217;; in other words, a type analogue of ex. They arrive at (approximated in ASCII and the notation used in my previous post): T[X] = Forall(n : N) Xn / Autn This is a little much for me, since I don&#8217;t understand [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cs.nott.ac.uk/~ctm/derivcont.ps.gz">Derivatives of Containers</a> claims to look for the &#8216;container which is its own derivative&#8217;; in other words, a type analogue of <i>e<sup>x</sup></i>. They arrive at (approximated in ASCII and the notation used in my previous post):</p>
<pre><code>T[<i>X</i>] = Forall(<i>n</i> : N) <i>X</i><sup><i>n</i></sup> / Aut<sub><i>n</i></sub></code></pre>
<p>This is a little much for me, since I don&#8217;t understand the symbolism and terminology used in the other 99% of the paper, either! However, as they note that it closely matches the Taylor-series expansion of <i>e<sup>x</sup></i>. I think that using the simple concepts I linked to in the previous post, I can approach a similar definition.</p>
<p>First of all, start with a simple (identity?) type:</p>
<pre><code>F[X] = X</code></pre>
<p>Now, since what we want is a type that when differentiated, gives itself, we should start by integrating:</p>
<pre><code>F[X]  = X<sup>2</sup> ÷ 2</code></pre>
<p>As shown in the previous post, we can interpret the division operator as &#8216;missing information&#8217;. In this case, the missing information is a 2-enum stating where the new type element was to be inserted&#8230; thus we have a type consisting of two elements, only we don&#8217;t know in what order they should appear. If we repeat this process several times, we obtain:</p>
<pre><code>F[X] = X<sup>3</sup> ÷ (2×3)
F[X] = X<sup>4</sup> ÷ (2×3×4)
F[X] = X<sup>5</sup> ÷ (2×3×4×5)</code></pre>
<p>Now we can begin to see a pattern emerging. Each time we integrate the function, we are adding an element to the type, without saying where it should be inserted&#8212;in other words, we are creating an unordered set of objects (note that these aren&#8217;t true sets, but merely bags: sets which can contain duplicates). Now if we take the literal Taylor expansion of <i>e<sup>x</sup></i>, we can see what it really is as a type:</p>
<pre><code>E[X] = Σ (X<sup>n</sup> ÷ n!)</code></pre>
<p>It is either a bag with one element, or a bag with two elements, or a bag with three elements&#8230; it is in fact the union of all bag types! That is, the exponential function interpreted as a type, is the type of bags.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/exponential-bags-integration-of-types-continued/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Types as Functions (and Integration thereof)</title>
		<link>http://porg.es/blog/types-as-functions-and-integration-thereof</link>
		<comments>http://porg.es/blog/types-as-functions-and-integration-thereof#comments</comments>
		<pubDate>Wed, 11 Apr 2007 00:34:19 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Thought]]></category>

		<guid isPermaLink="false">http://porg.es/blog/types-as-functions-and-integration-thereof</guid>
		<description><![CDATA[After reading Differention of Datastructures, I think I finally understand what is meant by &#8220;types as functions&#8221;, and immediately I can see one direction in which to go from what is presented there. If we take integration to be the logical inverse of differentiation (which is defined in the article as &#8216;removal of an element [...]]]></description>
			<content:encoded><![CDATA[<p>After reading <a href="http://homepage.mac.com/sigfpe/Computing/diff.html">Differention of Datastructures</a>, I think I finally understand what is meant by &#8220;types as functions&#8221;, and immediately I can see one direction in which to go from what is presented there.</p>
<p>If we take integration to be the logical inverse of differentiation (which is defined in the article as &#8216;removal of an element from a type&#8217<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" />, then we have integration as the addition of an element to a type.</p>
<p>For example, take the type of an array of 3 elements of the same type:</p>
<pre><code>F[T] = T<sup>3</sup></code></pre>
<p>If we are to add an element to this array, we want to integrate it. This gives us:</p>
<pre><code>F[T] = T<sup>4</sup> ÷ 4</code></pre>
<p>Clearly this is not what we expected. If we want to add an element to a 3-element array, we are expecting a 4-element array. However, if we look again at the above result, we can reinterpret it as telling us something: the mere integration of the type is not supplying the expected result; and if we interpret division as &#8216;without&#8217; (just as addition is &#8216;or&#8217; and multiplication is &#8216;and&#8217<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" />, then we can understand the result obtained.</p>
<p>What this is in fact telling us is that we need to provide another parameter (an enum in this case), which will give us the required result. In other words (if we do it backwards&#8230<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" />:</p>
<pre><code>F[T] = T<sup>4</sup> -- wanted
F'[T] = 4.T<sup>3</sup> -- needed</code></pre>
<p>In order to obtain a 4-element array, we need a 3-element array and a 4-enum. This makes sense, because if we have a 3-element array:</p>
<pre><code>x y z</code></pre>
<p>&#8230;and we wish to add an element &#8216;w&#8217; to it, we need to know where to insert it. The number of insertion points is:</p>
<pre><code>(1) x (2) y (3) z (4)</code></pre>
<p>&#8230;and thus we need to supply a 4-enum to specify exactly where we want the insertion to take place. Once we have done this, we obtain our 4-element array with no &#8216;missing&#8217; parameters.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/types-as-functions-and-integration-thereof/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Functional programming, APL and Unix pipes</title>
		<link>http://porg.es/blog/functional-programming-apl-and-unix-pipes</link>
		<comments>http://porg.es/blog/functional-programming-apl-and-unix-pipes#comments</comments>
		<pubDate>Sun, 04 Mar 2007 05:27:52 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Thought]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://porg.es/blog/functional-programming-apl-and-unix-pipes</guid>
		<description><![CDATA[I&#8217;ve noticed that when reading rather deeply-nested functional code in Haskell, the process taking place in reading the code seems to resemble trying to decipher APL more than anything else. This seems to be because functions which operate over lists are easily concatenated (possibly using the $ operator), because they operate on lists in their [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve noticed that when reading rather deeply-nested functional code in Haskell, the process taking place in reading the code seems to resemble trying to decipher <abbr title="A Programming Language">APL</abbr> more than anything else. This seems to be because functions which operate over lists are easily concatenated (possibly using the <code>$</code> operator), because they operate on lists in their last parameters. For example: </p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">foldr</span> getCounts <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">map</span> classify <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">concat</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span><span style="color: red;">20</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: red;">30</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">490</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: red;">500</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">8120</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: red;">8130</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span></pre></div></div>

<p>In order to understand what is happening here, one needs to start from the <abbr title="right-hand side">RHS</abbr> of the expression and then work backwards&mdash;much the same as in this snippet of <abbr>APL</abbr>:</p>
<pre><code>(∼R∈R∘.×R)/R←1↓⍳R</code></pre>
<p><small>(See the <a href="http://en.wikipedia.org/wiki/APL_(programming_language)">Wikipedia article on APL</a> for what is happening here.)</small></p>
<p>In both cases, the code is operating upon a common parameter which is modified by each function in turn. Similarly, there seems to be a rather high-level isomorphism between these kinds of functional code and the concept of Unix pipes:</p>
<pre><code>sort file | uniq -c | sort -n</code></pre>
<p>&#8230;with the difference being that in this case, the common parameter is being passed from the left instead of from the right.</p>
<p>Now, in Haskell I can define a new &#8220;pipe&#8221; operator (<code>|</code> is already taken):</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">~&gt;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>a<span style="color: #339933; font-weight: bold;">-&gt;</span>b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> b
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">~&gt;</span><span style="color: green;">&#41;</span> x f <span style="color: #339933; font-weight: bold;">=</span> f x</pre></div></div>

<p>Which just has the reverse signature to the <code>$</code> operator, and:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">foldr</span> getCounts <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">map</span> classify <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">concat</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span><span style="color: red;">20</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: red;">30</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">490</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: red;">500</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">8120</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: red;">8130</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span></pre></div></div>

<p>&#8230;becomes&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#91;</span><span style="color: green;">&#91;</span><span style="color: red;">20</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: red;">30</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">490</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: red;">500</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: red;">8120</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: red;">8130</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">~&gt;</span> <span style="font-weight: bold;">concat</span> <span style="color: #339933; font-weight: bold;">~&gt;</span> <span style="font-weight: bold;">map</span> classify <span style="color: #339933; font-weight: bold;">~&gt;</span> <span style="font-weight: bold;">foldr</span> getCounts <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</span></pre></div></div>

<p>&#8230;which makes it a bit easier to read, at least in my (used-to-Linux) eyes.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/functional-programming-apl-and-unix-pipes/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Implementing futures in C#</title>
		<link>http://porg.es/blog/implementing-futures-in-c</link>
		<comments>http://porg.es/blog/implementing-futures-in-c#comments</comments>
		<pubDate>Tue, 13 Feb 2007 08:03:15 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Cool]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[Reference]]></category>

		<guid isPermaLink="false">http://porg.es/blog/implementing-futures-in-c</guid>
		<description><![CDATA[Why? I was bored, and it didn&#8217;t seem to have been done before, so here&#8217;s some Future action in C#. The Code using System; using System.IO; using System.Reflection; using System.Threading; &#160; public class Future&#60;T&#62; &#123; public delegate R FutureDelegate&#60;R&#62;&#40;&#41;; public Future &#40;FutureDelegate&#60;T&#62; del&#41; &#123; Del = del; Result = del.BeginInvoke&#40;null,null&#41;; &#125; private FutureDelegate&#60;T&#62; Del; private [...]]]></description>
			<content:encoded><![CDATA[<h3>Why?</h3>
<p>I was bored, and it didn&#8217;t seem to have been done before, so here&#8217;s some <a href="http://en.wikipedia.org/wiki/Future_(programming)">Future</a> action in C#.</p>
<h3>The Code</h3>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Future<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">delegate</span> R FutureDelegate<span style="color: #008000;">&lt;</span>R<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Future <span style="color: #008000;">&#40;</span>FutureDelegate<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> del<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                Del <span style="color: #008000;">=</span> del<span style="color: #008000;">;</span>
                Result <span style="color: #008000;">=</span> del<span style="color: #008000;">.</span><span style="color: #0000FF;">BeginInvoke</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">null</span>,<span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> FutureDelegate<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Del<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> IAsyncResult Result<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> T PValue<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> HasValue <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> T Value <span style="color: #008000;">&#123;</span>
                get <span style="color: #008000;">&#123;</span>
                        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>HasValue<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>Result<span style="color: #008000;">.</span><span style="color: #0000FF;">IsCompleted</span><span style="color: #008000;">&#41;</span>
                                        Result<span style="color: #008000;">.</span><span style="color: #0000FF;">AsyncWaitHandle</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WaitOne</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                                PValue <span style="color: #008000;">=</span> Del<span style="color: #008000;">.</span><span style="color: #0000FF;">EndInvoke</span><span style="color: #008000;">&#40;</span>Result<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                                HasValue <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
                        <span style="color: #008000;">&#125;</span>
                        <span style="color: #0600FF; font-weight: bold;">return</span> PValue<span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">implicit</span> <span style="color: #0600FF; font-weight: bold;">operator</span> T<span style="color: #008000;">&#40;</span>Future<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> f<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> MainClass <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//This is purposely wasteful!</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">ulong</span> Fib<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                        <span style="color: #0600FF; font-weight: bold;">return</span> Fib<span style="color: #008000;">&#40;</span>n<span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">+</span>Fib<span style="color: #008000;">&#40;</span>n<span style="color: #008000;">-</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
                        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008080; font-style: italic;">//n == 0</span>
                        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080; font-style: italic;">//Demo using futures.</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;First, call synchronously.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>Fib<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">40</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Next, call asynchronously.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                Future<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> fib40 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Future<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#123;</span>
                        <span style="color: #0600FF; font-weight: bold;">return</span> Fib<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">40</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Begins computing Fib(40) at this point...</span>
                Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1500</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Note that we can do other things in the mean time, and that the thread only blocks when we ask for the value.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>fib40<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Need it now!</span>
                Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2000</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;We can then ask for the computed value as many times as we like.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>fib40<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Already computed.</span>
        <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h3>Caveats</h3>
<p>Most notably, this doesn&#8217;t do <a href="http://c2.com/cgi/wiki?PromisePipelining">promise pipelining</a> (and I doubt I could without some compiler magicking), so instead of nesting futures, you&#8217;ll want to seperate them out. That is; instead of:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">t3 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Future<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#123;</span><span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Future<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#123;</span><span style="color: #0600FF; font-weight: bold;">return</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">a</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">c</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Future<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#123;</span><span style="color: #0600FF; font-weight: bold;">return</span> y<span style="color: #008000;">.</span><span style="color: #0000FF;">b</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>&#8230;you&#8217;ll want to do:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">t1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Future<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#123;</span><span style="color: #0600FF; font-weight: bold;">return</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">a</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
t2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Future<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#123;</span><span style="color: #0600FF; font-weight: bold;">return</span> y<span style="color: #008000;">.</span><span style="color: #0000FF;">b</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
t3 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Future<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#123;</span><span style="color: #0600FF; font-weight: bold;">return</span> t1<span style="color: #008000;">.</span><span style="color: #0000FF;">c</span><span style="color: #008000;">&#40;</span>t2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>&#8230;although you&#8217;d probably want to do this anyway if you don&#8217;t want to wander into The LispZone.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/implementing-futures-in-c/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Easier local DNS cache</title>
		<link>http://porg.es/blog/easier-local-dns-cache</link>
		<comments>http://porg.es/blog/easier-local-dns-cache#comments</comments>
		<pubDate>Mon, 09 Oct 2006 06:59:24 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[bind9]]></category>
		<category><![CDATA[Cool]]></category>
		<category><![CDATA[DNS]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://porg.es/blog/easier-local-dns-cache</guid>
		<description><![CDATA[After a while pdnsd stopped working for me, and try as I might I can&#8217;t fix it. So I purged pdnsd and found an easier method, with no configuration this time!apt-get install bind9 resolvconf]]></description>
			<content:encoded><![CDATA[<p>After a while <a href="http://porg.es/blog/easy-persistent-local-dns-cache-to-reduce-lookup-times-2">pdnsd</a> stopped working for me, and try as I might I can&#8217;t fix it. So I purged <code>pdnsd</code> and found an easier method, with no configuration this time!<code>apt-get install bind9 resolvconf</code></p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/easier-local-dns-cache/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Easy, persistent, local DNS cache to reduce lookup times</title>
		<link>http://porg.es/blog/easy-persistent-local-dns-cache-to-reduce-lookup-times-2</link>
		<comments>http://porg.es/blog/easy-persistent-local-dns-cache-to-reduce-lookup-times-2#comments</comments>
		<pubDate>Thu, 05 Oct 2006 02:26:15 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[Cool]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://porg.es/blog/easy-persistent-local-dns-cache-to-reduce-lookup-times-2</guid>
		<description><![CDATA[Important Update! Don&#8217;t use this method any more! I have a much easier way to do it in a more recent post. But for posterity&#8230; First install resolvconf and pdnsd: sudo apt-get install pdnsd resolvconf. Resolvconf handles the messy interaction between different programs fighting over the /etc/resolv.conf file, and pdnsd provides a persistent DNS daemon, [...]]]></description>
			<content:encoded><![CDATA[<h3>Important Update!</h3>
<p>Don&#8217;t use this method any more! I have a much easier way to do it in a <a href="http://porg.es/blog/easier-local-dns-cache">more recent post</a>.</p>
<h3>But for posterity&#8230;</h3>
<p>First install resolvconf and pdnsd: <code>sudo apt-get install pdnsd resolvconf</code>. Resolvconf handles the messy interaction between different programs fighting over the <code>/etc/resolv.conf</code> file, and pdnsd provides a <b>p</b>ersistent <b>DNS</b> <b>d</b>aemon, which will cache the DNS lookups on your machine in order to make browsing faster.</p>
<p>Next, you just need to edit pdnsd&#8217;s configuration file, <code>/etc/pdnsd.conf</code> as root. Edit it to look like this:</p>
<pre><code>global {
        perm_cache=512;
        cache_dir="/var/cache/pdnsd";
        max_ttl=604800;
        run_as="pdnsd";
        paranoid=on;
#       next setting allows ppp/ip-up update the name servers -- ABa / 20040213
        status_ctl=on;
        server_port=53;
        server_ip="127.0.0.1";
}

#Edit these to be your own servers if wished:
server {
        label = "OpenDNS";
        ip=208.67.222.222,208.67.220.220;
        proxy_only=on;
        timeout=10;
}

# if you installed resolvconf, and status_ctl=on
server {
    label="resolvconf";
}

source {
        ttl=86400;
        owner="localhost.";
#       serve_aliases=on;
        file="/etc/hosts";
}</pre>
<p></code></p>
<p>And finally, restart pdnsd with <code>sudo /etc/init.d/pdnsd restart</code> to use the new settings. In the example above, I've used OpenDNS's servers, but you can change these to whatever you want. As far as actual results go:</p>
<blockquote><p><code>~$ dig example.com</p>
<p>; <<>> DiG 9.3.2 <<>> example.com<br />
;; global options:  printcmd<br />
;; Got answer:<br />
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 17447<br />
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0</p>
<p>;; QUESTION SECTION:<br />
;example.com.                   IN      A</p>
<p>;; ANSWER SECTION:<br />
example.com.            172800  IN      A       192.0.34.166</p>
<p><b>;; Query time: 513 msec</b><br />
;; SERVER: 127.0.0.1#53(127.0.0.1)<br />
;; WHEN: Thu Oct  5 15:24:19 2006<br />
;; MSG SIZE  rcvd: 45</p>
<p>$ dig example.com</p>
<p>; <<>> DiG 9.3.2 <<>> example.com<br />
;; global options:  printcmd<br />
;; Got answer:<br />
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 19338<br />
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0</p>
<p>;; QUESTION SECTION:<br />
;example.com.                   IN      A</p>
<p>;; ANSWER SECTION:<br />
example.com.            172799  IN      A       192.0.34.166</p>
<p><b>;; Query time: 0 msec</b><br />
;; SERVER: 127.0.0.1#53(127.0.0.1)<br />
;; WHEN: Thu Oct  5 15:24:20 2006<br />
;; MSG SIZE  rcvd: 45<br />
</code></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/easy-persistent-local-dns-cache-to-reduce-lookup-times-2/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Normalizing your MP3 collection with mp3gain</title>
		<link>http://porg.es/blog/normalizing-your-mp3-collection-with-mp3gain</link>
		<comments>http://porg.es/blog/normalizing-your-mp3-collection-with-mp3gain#comments</comments>
		<pubDate>Wed, 20 Sep 2006 02:36:32 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://porg.es/blog/normalizing-your-mp3-collection-with-mp3gain</guid>
		<description><![CDATA[Carthik Sharma from Ubuntu Blog suggests the following method to normalize all the MP3 files in your collection: find . -type f -iname &apos;*.mp3&apos; -print0 &#124; xargs -0 mp3gain -r -k Unfortunately, this seems to have problems with very large collections (I suspect that the command line is being filled up), so here&#8217;s what I [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://ubuntu.wordpress.com/2006/09/11/normalize-the-gain-playback-volume-of-your-mp3s/">Carthik Sharma from Ubuntu Blog suggests the following method to normalize all the MP3 files in your collection</a>:</p>
<pre><code>find . -type f -iname &apos;*.mp3&apos; -print0 | xargs -0 mp3gain -r -k</code></pre>
<p>Unfortunately, this seems to have problems with very large collections (I suspect that the command line is being filled up), so here&#8217;s what I came up with:</p>
<pre><code>find . -iname &apos;*.mp3&apos; -execdir mp3gain -r -k &quot;{}&quot; &#x5C;; &#038;</code></pre>
<p>If you&#8217;d rather use album normalization (and you have your files separated by album such as &#8220;~/Music/David Bowie/Hunky Dory&#8221;/):</p>
<pre><code>find . -iname &apos;*.mp3&apos; -execdir mp3gain -r -k <u>-a</u> &quot;{}&quot; <u>+</u> &#038;</code></pre>
<h3>How to use it</h3>
<p>Simply open a terminal, browse to your music directory (using <code>cd</code>), and type it in.</p>
<h3>Break it down</h3>
<p>What the command does:</p>
<ol>
<li><code>find . -iname &apos;*.mp3&apos;</code> finds all MP3 files in the current directory and all subdirectories.</li>
<li><code>-execdir mp3gain -r -k &quot;{}&quot; \; &#038;</code> executes &#8220;mp3gain -r -k&#8221; on each file (the filename goes where {} is found). The semi-colon indicates to execute the command once per file, but since ; is also a significant character for bash, you need to escape it with a backslash. The final &#8216;&#038;&#8217; tells bash to run the command in the background.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/normalizing-your-mp3-collection-with-mp3gain/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>The Ur-Quan Masters on Ubuntu AMD64</title>
		<link>http://porg.es/blog/the-ur-quan-masters-on-ubuntu-amd64</link>
		<comments>http://porg.es/blog/the-ur-quan-masters-on-ubuntu-amd64#comments</comments>
		<pubDate>Thu, 14 Sep 2006 20:41:31 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://porg.es/blog/the-ur-quan-masters-on-ubuntu-amd64</guid>
		<description><![CDATA[At the moment, The Ur-Quan Masters doesn&#8217;t work on AMD64. This is a shame, because I just found it and realised Star Control II was the game that I had played on my friend-down-the-road&#8217;s computer so many years ago. Attempting to fix this by forcing the install of the 32-bit version of uqm doesn&#8217;t work, [...]]]></description>
			<content:encoded><![CDATA[<p>At the moment, <a href="http://sc2.sourceforge.net/">The Ur-Quan Masters</a> doesn&#8217;t work on AMD64. This is a shame, because I just found it and realised Star Control II was the game that I had played on my friend-down-the-road&#8217;s computer so many years ago.</p>
<p>Attempting to fix this by forcing the install of the 32-bit version of <code>uqm</code> doesn&#8217;t work, because for some reason then apt won&#8217;t recognise that it is installed and complain that <code>uqm-content</code> has unmet dependencies. So here&#8217;s the official recommendation from me:</p>
<ol>
<li>Install <code>uqm</code> normally with the package manager.</li>
<li>Download the i386 .debs, extract them and overwrite the binary files from <code>uqm</code> (there are two of them).</li>
<li>Install <code>ia32-libs-sdl</code> for the 32-bit versions of the libraries it depends on.</li>
</ol>
<p>&#8230; and it should work perfectly. If you want voice-overs and music (yes!), head to <a href="http://sc2.sourceforge.net/downloads.php">the UQM download page</a>, and download the second two files listed under &#8220;Content&#8221; (you already have the first). These are in fact just renamed zip files, so unzip them to <code>/usr/share/games/uqm/content/</code> and you&#8217;re good to go.</p>
<p>And the game? Excellent. This is much better than the last game I bought&mdash;Civilization IV.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/the-ur-quan-masters-on-ubuntu-amd64/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Neat little Linux tool</title>
		<link>http://porg.es/blog/neat-little-linux-tool</link>
		<comments>http://porg.es/blog/neat-little-linux-tool#comments</comments>
		<pubDate>Tue, 12 Sep 2006 08:14:45 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://porg.es/blog/neat-little-linux-tool</guid>
		<description><![CDATA[Command Not Found is a great little utility for Linux. If you&#8217;ve ever tried to follow a set of instructions only to find that you don&#8217;t have some of the CLI programs needed, this will come to your rescue. Install command-not-found then add this to your ~/.bashrc: function command_not_found_handle { /usr/bin/command-not-found $1 return $? } [...]]]></description>
			<content:encoded><![CDATA[<p>Command Not Found is a great little utility for Linux. If you&#8217;ve ever tried to follow a set of instructions only to find that you don&#8217;t have some of the <abbr title="command line interface">CLI</abbr> programs needed, this will come to your rescue.</p>
<p>Install <code>command-not-found</code> then add this to your <code>~/.bashrc</code>:</p>
<pre><code>function command_not_found_handle {
        /usr/bin/command-not-found $1
        return $?
}</code></pre>
<p>And you&#8217;re all done:</p>
<blockquote><p><code>~$ ssed<br />
The program 'ssed' is currently not installed, you can install it by typing:<br />
sudo apt-get install ssed<br />
</code></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/neat-little-linux-tool/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spotted</title>
		<link>http://porg.es/blog/spotted</link>
		<comments>http://porg.es/blog/spotted#comments</comments>
		<pubDate>Mon, 04 Sep 2006 22:33:14 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[New Zealand]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://porg.es/blog/spotted</guid>
		<description><![CDATA[This seen outside New World supermarket, Wellington, New Zealand. I&#8217;m not sure if the placement is some kind of metaphor.]]></description>
			<content:encoded><![CDATA[<p><a class="imagelink" href="http://porg.es/blog/wp-content/uploads/2006/09/0_0.jpg" title="Ubuntu sticker on rubbish bin"><img id="image76" src="http://porg.es/blog/wp-content/uploads/2006/09/0_0.thumbnail.jpg" alt="Ubuntu sticker on rubbish bin" /></a></p>
<p>This seen outside New World supermarket, Wellington, New Zealand. I&#8217;m not sure if the placement is some kind of metaphor.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/spotted/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DeskBar Calculator</title>
		<link>http://porg.es/blog/deskbar-calculator</link>
		<comments>http://porg.es/blog/deskbar-calculator#comments</comments>
		<pubDate>Mon, 04 Sep 2006 22:05:22 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Cool]]></category>
		<category><![CDATA[GNOME]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://porg.es/blog/deskbar-calculator</guid>
		<description><![CDATA[As created by Spooky. It really is easy to use (and surprisingly simple to install), and much easier than popping up Qalculate (despite its awesomnity). Follow his lead and you&#8217;ll have calculation support in DeskBar in less than a minute. The thing that&#8217;s nice about these Python applications is their extensibility, and not having to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://spooky-possum.org/cgi-bin/pyblosxom.cgi/db-calculator.html" title="DeskBar Calculator">As created by Spooky</a>. It really is easy to use (and surprisingly simple to install), and much easier than popping up <a href="http://qalculate.sourceforge.net/">Qalculate</a> (despite its awesomnity). <a href="http://www.spooky-possum.org/callum/software/index.html#db-calculator" title="DeskBar calculator install instructions">Follow his lead</a> and you&#8217;ll have calculation support in DeskBar in less than a minute.</p>
<p>The thing that&#8217;s nice about these Python applications is their extensibility, and not having to restart them for new plugins and suchlike. Now if only they&#8217;d automatically check for updates&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/deskbar-calculator/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

