<?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; Development</title>
	<atom:link href="http://porg.es/blog/tag/development/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>C# type inference and extension methods: an abuse</title>
		<link>http://porg.es/blog/c-type-inference-and-extension-methods-an-abuse</link>
		<comments>http://porg.es/blog/c-type-inference-and-extension-methods-an-abuse#comments</comments>
		<pubDate>Wed, 23 Feb 2011 03:11:48 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[silly]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=554</guid>
		<description><![CDATA[Here&#8217;s a little example of statically-sized stacks in C#. They&#8217;re implemented with a linked-list as the backing store: using System; &#160; namespace ConsoleApplication &#123; public static class MainClass &#123; // Example: &#160; public static void Main&#40;string&#91;&#93; args&#41; &#123; var stack = Stack.New&#60;int&#62;&#40;&#41;; // real type Stack&#60;Z,int&#62; &#160; var s1 = stack.Push&#40;1&#41;.Push&#40;2&#41;.Push&#40;3&#41;; // real type Stack&#60;S&#60;S&#60;S&#60;Z&#62;&#62;&#62;&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a little example of statically-sized stacks in C#. They&#8217;re implemented with a linked-list as the backing store:</p>

<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>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> ConsoleApplication
<span style="color: #008000;">&#123;</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;">class</span> MainClass
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Example:</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;">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>
            var stack <span style="color: #008000;">=</span> Stack<span style="color: #008000;">.</span><span style="color: #008000;">New</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><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: #008080; font-style: italic;">// real type Stack&lt;Z,int&gt;</span>
&nbsp;
            var s1 <span style="color: #008000;">=</span> stack<span style="color: #008000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// real type Stack&lt;S&lt;S&lt;S&lt;Z&gt;&gt;&gt;&gt;</span>
            var s2 <span style="color: #008000;">=</span> s1<span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// real type Stack&lt;Z,int&gt;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// statically disallowed:</span>
            <span style="color: #008080; font-style: italic;">// s2.Pop();</span>
&nbsp;
            Pop2<span style="color: #008000;">&#40;</span>s1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>s1<span style="color: #008000;">.</span><span style="color: #0000FF;">IsEmpty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</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>s2<span style="color: #008000;">.</span><span style="color: #0000FF;">IsEmpty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Example of a method requiring &quot;at least 2&quot; items on stack.</span>
        <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Pop2<span style="color: #008000;">&lt;</span>TRest,T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TRest<span style="color: #008000;">&gt;&gt;</span>,T<span style="color: #008000;">&gt;</span> stack<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            T x1<span style="color: #008000;">;</span>
            T x2<span style="color: #008000;">;</span>
            stack<span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> x1<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> x2<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;{0}, {1}&quot;</span>, x1, x2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Implementation:</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// very weak type level naturals</span>
    <span style="color: #008080; font-style: italic;">// Z = zero, S&lt;x&gt; = successor of x.</span>
    <span style="color: #008080; font-style: italic;">// so, S&lt;Z&gt; = 1, S&lt;S&lt;Z&gt;&gt; = 2, etc.</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> S<span style="color: #008000;">&lt;</span>TPrev<span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> Z <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// internal node type for the linked list</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">class</span> StackNode<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> StackNode<span style="color: #008000;">&#40;</span>T item, StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> next<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">item</span> <span style="color: #008000;">=</span> item<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">next</span> <span style="color: #008000;">=</span> next<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">internal</span> T item<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> next<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// the stack wrapper type (sized with types)</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Stack<span style="color: #008000;">&lt;</span>TSize,T<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> node<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Construct wrapper:</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> Stack<span style="color: #008000;">&#40;</span>StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> node<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">node</span> <span style="color: #008000;">=</span> node<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Push:</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> Stack<span style="color: #008000;">&#40;</span>T item, StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> node<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">node</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StackNode<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>item, node<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// note that the return type has the size increased by 1</span>
        <span style="color: #008080; font-style: italic;">// S&lt;TSize&gt; ~= TSize++ </span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TSize<span style="color: #008000;">&gt;</span>,T<span style="color: #008000;">&gt;</span> Push<span style="color: #008000;">&#40;</span>T item<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> Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TSize<span style="color: #008000;">&gt;</span>,T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>item, node<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// extension methods that perform the operations:</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;">class</span> Stack
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// a new stack has size zero</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Stack<span style="color: #008000;">&lt;</span>Z,T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">New</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</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> Stack<span style="color: #008000;">&lt;</span>Z, T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</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>
&nbsp;
        <span style="color: #008080; font-style: italic;">// pop with ignored result</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Stack<span style="color: #008000;">&lt;</span>TRest,T<span style="color: #008000;">&gt;</span> Pop<span style="color: #008000;">&lt;</span>TRest,T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TRest<span style="color: #008000;">&gt;</span>,T<span style="color: #008000;">&gt;</span> stack<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            T ignored<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> stack<span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> ignored<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// popping an item removes one S&lt;&gt; from the size (~= TSize--)</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Stack<span style="color: #008000;">&lt;</span>TRest,T<span style="color: #008000;">&gt;</span> Pop<span style="color: #008000;">&lt;</span>TRest,T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TRest<span style="color: #008000;">&gt;</span>,T<span style="color: #008000;">&gt;</span> stack, <span style="color: #0600FF; font-weight: bold;">out</span> T item<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            item <span style="color: #008000;">=</span> stack<span style="color: #008000;">.</span><span style="color: #0000FF;">node</span><span style="color: #008000;">.</span><span style="color: #0000FF;">item</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Stack<span style="color: #008000;">&lt;</span>TRest, T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>stack<span style="color: #008000;">.</span><span style="color: #0000FF;">node</span><span style="color: #008000;">.</span><span style="color: #0000FF;">next</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// These are statically known, because of the types:</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;">bool</span> IsEmpty<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Stack<span style="color: #008000;">&lt;</span>Z,T<span style="color: #008000;">&gt;</span> stack<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;">true</span><span style="color: #008000;">;</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: #6666cc; font-weight: bold;">bool</span> IsEmpty<span style="color: #008000;">&lt;</span>TRest, T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Stack<span style="color: #008000;">&lt;</span>S<span style="color: #008000;">&lt;</span>TRest<span style="color: #008000;">&gt;</span>, T<span style="color: #008000;">&gt;</span> stack<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;">false</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>One fun thing about this is that we never have to check whether we&#8217;ve hit the bottom of the stack (<code>node.next == null</code>). Since the size is statically part of the type, and there&#8217;s no Pop for an empty list, we never have to check for this situation.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/c-type-inference-and-extension-methods-an-abuse/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A simple BigNum library for .NET</title>
		<link>http://porg.es/blog/a-simple-bignum-library-for-dot-net</link>
		<comments>http://porg.es/blog/a-simple-bignum-library-for-dot-net#comments</comments>
		<pubDate>Sat, 13 Oct 2007 08:05:51 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://porg.es/blog/a-simple-bignum-library-for-net</guid>
		<description><![CDATA[Update Due to minor demand, the code is also available: BigNum source. Please note that I haven’t actually touched the code since it was first written. I’m sure there’s some things that don’t work properly. If you’re doing anything big with this you probably want to write some ‘destructive’ update functions for adding, etc. At [...]]]></description>
			<content:encoded><![CDATA[<h4>Update</h4>
<p>Due to minor demand, the code is also available: <a href='http://porg.es/blog/wp-content/uploads/2008/04/bignum.zip'>BigNum source</a>.</p>
<p>Please note that I haven’t actually touched the code since it was first written. I’m sure there’s some things that don’t work properly. If you’re doing anything big with this you probably want to write some ‘destructive’ update functions for adding, etc. At the moment every time you add, subtract, etc, a completely new BigInt is returned. To make it faster you’d want to just update the number in-place.</p>
<h4>Original content&#8230;</h4>
<p>I’ve created a simple wrapper for <a href="http://www.gmplib.org">GMP</a>. At the moment only BigInts are implemented, but I expect to have BigRationals and BigFloats coming along soon enough. (<i>Edit: this never happened.</i>)</p>
<h4>Usage</h4>
<p>I think this is very close to as-you’d-expect. The only minor thing that might come as a surprise is that BigInts aren’t value types; since they need destructors, they can’t be. The rest is fairly straightforward:</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;">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>
    BigInt n<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">999</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        n <span style="color: #008000;">=</span> BigInt<span style="color: #008000;">.</span><span style="color: #0000FF;">Factorial</span><span style="color: #008000;">&#40;</span>i<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>n<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</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>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This code executes with sub 5-second times, which I’m pretty pleased with. I’m not sure what kind of performance decrease you get through using GMP under managed code.</p>
<h4>Caveats</h4>
<ul>
<li>Consider this alpha software <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></li>
<li>The included GMP DLL is compiled with none of the magical assembly they provide. If you want to, you can compile it yourself with all this enabled, and you should be able to just use it as a drop-in replacement.</li>
</ul>
<h4>Download</h4>
<p>Available here: <a href='http://porg.es/blog/wp-content/uploads/2007/10/bignum.zip' title='BigNum library'>BigNum library</a>. Documentation <a href="http://porg.es/Help/Index.html">is available</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/a-simple-bignum-library-for-dot-net/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<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>NullPointerException</title>
		<link>http://porg.es/blog/nullpointerexception</link>
		<comments>http://porg.es/blog/nullpointerexception#comments</comments>
		<pubDate>Thu, 24 May 2007 21:26:41 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Critique]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Thought]]></category>
		<category><![CDATA[Types]]></category>

		<guid isPermaLink="false">http://porg.es/blog/nullpointerexception</guid>
		<description><![CDATA[Why is this such an insidious error in Java? (An opinion piece!) A Comparison Firstly, I&#8217;ll show a short comparison between some Java code and some code from a language that doesn&#8217;t have NullPointerExceptions, but does have something that allows you to accomplish anything you might want to do with null pointers. This Java code: [...]]]></description>
			<content:encoded><![CDATA[<p>Why is this such an insidious error in Java? (An opinion piece!)</p>
<h3>A Comparison</h3>
<p>Firstly, I&#8217;ll show a short comparison between some Java code and some code from a language that doesn&#8217;t have <code>NullPointerException</code>s, but does have something that allows you to accomplish anything you might want to do with null pointers.</p>
<p>This Java code:</p>
<pre><code>AnObject thing = someMethod();</code></pre>
<p>is equivalent to the following Haskell:</p>
<pre><code>let thing = Maybe AnObject</code></pre>
<p>That is, every Java object reference is possibly null. What this means is that (using the type notation mentioned in earlier posts) every Java reference has the type:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/80c7aa7402953bdabc5fcf23a7a9b406.gif' title='ObjectReference[Object] = 1 + Object' alt='ObjectReference[Object] = 1 + Object' align=absmiddle></p>
<p><small>(Every object reference for an object is either to &#8216;null&#8217; (<abbr title="also known as">AKA</abbr> &#8216;unit&#8217; or &#8216;singleton&#8217<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" />, or the object itself.)</small></p>
<p>&#8230; and when you&#8217;re dealing with so many objects—Java is, after all, an object-oriented language, so (almost) everything is an object—this simple little &#8220;+1&#8243; gets lost easily.</p>
<h3>Why so bad?</h3>
<p>This wouldn&#8217;t seem to be such a big deal, but when contrasted against Java&#8217;s penchant for making things (sometimes painfully) explicit—think <code>public static void main (string argh!)</code>—a small, implicit item like this is easily overlooked. Conversely, one might say that the verbosity of Java <em>increases</em> the cognitive load of understanding its code; thus helping the chances of a small mistake like this sneaking through.</p>
<p>Either way, I think I can say that this is objectively A Bad Thing. C# has gone some of the way to a &#8216;correct&#8217; solution in its introduction of possibly-null primitives, which are explicitly marked with a <code>?</code> (such as <code>int?</code>), but unfortunately this hasn&#8217;t been &#8216;back-ported&#8217; to the rest of the language. If one were able to force between possibly-null and definitely-not-null references in C#—perhaps by the analogous <code>object?</code> vs. <code>object</code>, this would help to reduce the number of errors &#8216;hidden&#8217; by the syntax.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/nullpointerexception/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>About using Rosetta</title>
		<link>http://porg.es/blog/about-using-rosetta</link>
		<comments>http://porg.es/blog/about-using-rosetta#comments</comments>
		<pubDate>Fri, 11 May 2007 05:55:07 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[GNOME]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://porg.es/blog/about-using-rosetta</guid>
		<description><![CDATA[Oh, that reminds me… I should really contact some translators to ask for updates and release Gnome Specimen 0.2. —Wouter Bolsterlee The Gnome Specimen product is not set up for translation in Launchpad. You might want to talk with Wouter Bolsterlee (uws), the project registrant, about using Rosetta. —Launchpad Done!]]></description>
			<content:encoded><![CDATA[<p>
<blockquote>
<p>Oh, that reminds me… I should really contact some translators to ask for updates and release Gnome Specimen 0.2.</p>
</blockquote>
<p>—<cite><a href="http://uwstopia.nl/blog/2007/05/free-your-fonts">Wouter Bolsterlee</a></cite></p>
<p>
<blockquote>
<p>The Gnome Specimen product is not set up for translation in Launchpad.</p>
<p>You might want to talk with Wouter Bolsterlee (uws), the project registrant, about using Rosetta.</p>
</blockquote>
<p>—<cite><a href="https://launchpad.net/gnome-specimen/+translations">Launchpad</a></cite></p>
<p>Done!</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/about-using-rosetta/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>Sans-serif fonts for coding</title>
		<link>http://porg.es/blog/sans-serif-fonts-for-coding</link>
		<comments>http://porg.es/blog/sans-serif-fonts-for-coding#comments</comments>
		<pubDate>Tue, 19 Sep 2006 00:41:30 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://porg.es/blog/sans-serif-fonts-for-coding</guid>
		<description><![CDATA[After reading a post about colour schemes and font choices for use in an IDE, it seems I&#8217;m one of the only people on the planet that uses non-monospace fonts for coding. So, in the spirit of equal access, I&#8217;ve decided to try a couple of different fonts in MonoDevelop. While it doesn&#8217;t have the [...]]]></description>
			<content:encoded><![CDATA[<p>After reading <a href="http://www.codinghorror.com/blog/archives/000682.html">a post about colour schemes and font choices for use in an IDE</a>, it seems I&#8217;m one of the only people on the planet that uses non-monospace fonts for coding. So, in the spirit of equal access, I&#8217;ve decided to try a couple of different fonts in MonoDevelop. While it doesn&#8217;t have the greatest highlighting support at the moment, or any code folding (although I suspect these shortcomings are due to <code>libgtksourceview</code>), it&#8217;s still good enough for my purposes. My colour coding is based upon the default, but with blue keywords instead of green, and the comments changed to a light grey colour to make them more ignorable (the default is a bright blue which is quite startling).</p>
<p>First up is DejaVu Sans, which is my default sans-serif font on my system. It is quite plain looking which is possibly a good thing as it doesn&#8217;t distract from the task at hand. The &#8217;0&#8242; and &#8216;O&#8217; characters are different enough, but you may have problems with &#8216;I&#8217; and &#8216;l&#8217;.</p>
<p><img id="image82" src="http://porg.es/blog/wp-content/uploads/2006/09/dejavu2.png" alt="DejaVu font test" /></p>
<p>Next I tried Candara, which is a new font that is bundled with Windows Vista. It has a bit of character, but the <a href="http://en.wikipedia.org/wiki/Text_figures">text figures</a> are a bit distracting, and the punctuation really doesn&#8217;t fit in nicely with the rest of the font.</p>
<p><img id="image83" src="http://porg.es/blog/wp-content/uploads/2006/09/candara2.png" alt="Candara font test" /></p>
<p>Finally I picked <a href="http://www.urwpp.de/english/home.html">URW</a> Gothic. While this was just a test to see how extreme the fonts could be before they were completely unsuitable for coding, it turned out to work surprisingly well. The large open geometric shapes make for easy reading and the punctuation goes well with the other glyphs. The only problem I could identify is the square brackets looking too much like the rounded ones, but if you can get over that it looks like a very nice font to program with. In fact, I&#8217;ve just changed it to be the default in MonoDevelop.</p>
<p><img id="image84" src="http://porg.es/blog/wp-content/uploads/2006/09/gothic2.png" alt="Gothic font test" /></p>
<p>Of course, there are many other sans-serif fonts out there, but I picked these three as representative of several classes of styles. If anyone knows of sans-serif (or even serif!) non-monospace fonts that have been specifically designed for programming, I&#8217;d be glad to hear about them.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/sans-serif-fonts-for-coding/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>DBus, Mono, Rhythmbox, and Beagle, Oh My!</title>
		<link>http://porg.es/blog/dbus-mono-rhythmbox-and-beagle-oh-my</link>
		<comments>http://porg.es/blog/dbus-mono-rhythmbox-and-beagle-oh-my#comments</comments>
		<pubDate>Fri, 15 Sep 2006 10:26:38 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mono]]></category>

		<guid isPermaLink="false">http://porg.es/blog/dbus-mono-rhythmbox-and-beagle-oh-my</guid>
		<description><![CDATA[So I had this idea of writing a quick search plugin for Beagle to search Rhythmbox&#8217;s current playlist titles. They would show up in the search GUI and then you&#8217;d double-click on one and it&#8217;d handle this (probably through a pseudo-URL like playlist://rhythmbox/my-playlist-name which seems to be the de facto standard amongst Beagle plugins) by [...]]]></description>
			<content:encoded><![CDATA[<p>So I had this idea of writing a quick search plugin for Beagle to search Rhythmbox&#8217;s current playlist titles. They would show up in the search GUI and then you&#8217;d double-click on one and it&#8217;d handle this (probably through a pseudo-URL like <code>playlist://rhythmbox/my-playlist-name</code> which seems to be the <i>de facto</i> standard amongst Beagle plugins) by sending a DBus event to Rhythmbox to start playing this playlist. Sounds simple enough.</p>
<h3>Promise</h3>
<p>First of all I headed over to the Beagle site. It turns out that if you want to do more than handle a specific file-type (not an option in this case because the playlists are stored as <code>.xml</code>) you create not a &#8220;Filter&#8221; but a &#8220;Queryable&#8221;. Unfortunately the Queryable HowTo is <a href="http://beagle-project.org/index.php?title=Queryable_HOWTO">still being written</a>. Nevermind, I can always ask on the mailing list, and taking a look through the tutorial for Filters shows that the API for that is nice and clean and should be similar for the Queryables.</p>
<p>Next I try to find a reference for the C# DBus bindings. I install the Monodoc documentation for DBus, only to discover everything marked with &#8220;Documentation for this section has not yet been entered&#8221;&mdash;the default message for undocumented functions, classes, and so on. Nevertheless, I find <a href="http://www.electricrelaxation.com/2006/09/08/rhythmbox-toggle-showhide-using-dbus-and-python">a short tutorial on how to use Python with DBus to control Rhythmbox</a>. The class names and functions seem to have similar titles in C#, so I figure this will map fairly directly.</p>
<p>Finding the DBus reference for Rhythmbox takes a fair bit more effort, trawling through mailing list archives on Google. Eventually I find the DBus APIs hiding in a couple of <code>.xml</code> files in the CVS repositories; <a href="http://cvs.gnome.org/viewcvs/*checkout*/rhythmbox/shell/rb-playlist-manager.xml?content-type=text%2Fplain"><code>org.gnome.Rhythmbox.PlaylistManager</code></a> looks promising.</p>
<h3>Misfortune</h3>
<p>Unfortunately, I find a <a href="http://blogs.gnome.org/view/tko/2006/07/27/">post by a last-exit hacker</a> in which he states (my emphasis):</p>
<blockquote><p>As I was told <em>DBus C# bindings are unusable</em> I went on about writing DBus# objects the hard way.</p></blockquote>
<p>What? This can&#8217;t be right, I&#8217;ve already written a little test program:</p>
<pre><code>using System;
using DBus;

namespace RhythmboxQuery
{
   class MainClass
   {
      public static void Main(string[] args)
      {
         Connection bus = Bus.GetSessionBus();
         try {
            Service rhythmbox = Service.Get(bus,"org.gnome.Rhythmbox");
         } catch (ApplicationException ex) {
            Console.WriteLine("Rhythmbox not started: " + ex);
            return;
         }
         Console.WriteLine("Found Rhythmbox.");
         /*

         Okay, so all I need to do now is to call `getPlaylists` on:
         /org/gnome/Rhythmbox/PlaylistManager

         */
         Console.WriteLine("Uhoh.");
      }
   }
}</code></pre>
<p>Despite my valiant attempts to stave off the past through trivial C# programs, the DBus site <a href="http://www.freedesktop.org/wiki/Software_2fDBusBindings">confirms the aforementioned revelation</a>:</p>
<blockquote><p>They are schedualed to be removed from the core but has yet to find a maintainer. [sic]</p></blockquote>
<h3>Epilogue</h3>
<p>As it turns out, what I had been planning to do would have been impossible anyway because the Rhythmbox DBus bindings lack a <code>PlayPlaylist</code> function in any of the three interfaces. Also, it seems that DBus under C# only plays nicely with C# objects that you have access to&mdash;but this could be just because I couldn&#8217;t find a single example of how to call methods on non-C# DBus applications.</p>
<p>Ah well, that Beagle Filter API did seem quite nice anyway, I wonder if there are any filetypes I could parse out there&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/dbus-mono-rhythmbox-and-beagle-oh-my/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Excursions into Parrot</title>
		<link>http://porg.es/blog/excursions-into-parrot</link>
		<comments>http://porg.es/blog/excursions-into-parrot#comments</comments>
		<pubDate>Tue, 08 Aug 2006 10:37:39 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Parrot]]></category>

		<guid isPermaLink="false">http://porg.es/blog/excursions-into-parrot</guid>
		<description><![CDATA[Let me warn you from the start: Patrick Michaud&#8217;s slides make it look deceptively easy! First of all, I created &#8220;simple.pg&#8221; according to his recipe: grammar Simple::Grammar ; token variable { [ &#124; _ ] \w* } token number { \d+ } token addop { \+ &#124; - } token mulop { \* &#124; / [...]]]></description>
			<content:encoded><![CDATA[<p>Let me warn you from the start: <a href="http://www.pmichaud.com/2006/pres/yapc-parsers/">Patrick Michaud&#8217;s slides</a> make it look deceptively easy!</p>
<p>First of all, I created &#8220;simple.pg&#8221; according to his recipe:</p>
<pre><code>grammar Simple::Grammar ;

token variable { [ <alpha> | _ ] \w* }
token number { \d+ }

token addop { \+ | - }
token mulop { \* | / }

rule expression { <addterm> [ <addop> <addterm> ] * }
rule addterm { <multerm> [ <mulop> <multerm> ] * }
rule multerm { <variable> | <number> | \( <expression> \) }</code></pre>
<p>This looks all fine and dandy. Next, to compile: <code>$ parrot pgc.pir simple.pg > simple.pir</code>.</p>
<pre><code>Error reading source file pgc.pir.</code></pre>
<p>Huh? Well he didn&#8217;t say anything about that. Maybe I have a path wrong somewhere? A quick &#8220;locate&#8221; reveals it doesn&#8217;t lie anywhere on my computer. Next step is to check the downloaded source code to see if I wasn&#8217;t meant to delete it yet&#8230; ah, yes, it&#8217;s hiding under <code>./compilers/pge/</code>. It looks like it will be easier to work from this directory, so I&#8217;ll move everything into there.</p>
<p>Unfortunately, pgc.pir won&#8217;t run straight away, and you&#8217;ll need to patch it up a bit:</p>
<pre><code>@@ -49,7 +49,7 @@
     .param pmc args
     load_bytecode 'Getopt/Obj.pbc'
     load_bytecode 'dumper.pbc'
-    load_bytecode 'PGE/Dumper.pbc'
+    load_bytecode 'Data/Dumper.pbc'
     '__onload'()

     ##   create an option parser
@@ -153,8 +153,8 @@

 .sub '__onload' :load
     load_bytecode 'PGE.pbc'
-    load_bytecode 'PGE/Text.pbc'
-    load_bytecode 'PGE/Util.pbc'
+    load_bytecode 'PGE/Text.pir'
+    load_bytecode 'PGE/Util.pir'

     .local pmc p6regex
     p6regex = compreg 'PGE:<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_razz.gif" alt="" />6Regex'
</code></pre>
<p>Now it runs fine and generates the output file perfectly. Quite a bit of code in there! Next to use the parser on a real string, using the example:</p>
<pre><code>.include 'simple.pir'

.sub 'foo'
  ...
  ##   get the parrot sub for parsing an expression
  parse = find_global 'Simple::Grammar', 'expression'

  ##   the expression to be parsed
  expr = '3 + 4 * (a-2)'

  ##   parse the expression
  match = parse(expr)

  ##   dump the parse tree
  '_dumper'(match)</code></pre>
<p>That little ellipsis is really in there to let you know that you need to read quite a few more Parrot docs before you&#8217;re allowed to progress past this step. <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_smile.gif" alt="" /></p>
<p>In fact, you&#8217;ll be able to get up to speed in no time just by copying some of the example files. I found that I could get by with guessing a bit;</p>
<ul>
<li>.sub needed an .end,</li>
<li>the main method needs to be marked :main to run automatically,</li>
<li>the variables need to be declared&#8230; with appropriate types&#8230; you can&#8217;t just use pmc for everything,</li>
<li>and the _dumper code needed to be loaded.</li>
</ul>
<p>Here&#8217;s what it eventually looked like:</p>
<pre><code>.include 'simple.pir'

.sub main :main
        .local pmc parse, match
        .local string expr
        ## get the parrot sub for parsing an expression
        parse = find_global 'Simple::Grammar', 'expression'
        ## the expression to be parsed
        expr = "3 + 4 * (a-2)"
        ## parse the expression
        match = parse(expr)
        ## dump the parse tree
        load_bytecode 'dumper.pbc'
        '_dumper'(match)
.end
</code></pre>
<p>But that&#8217;s not all, you&#8217;ll need to jump through the automagically-generated parser code to put in things that it forgot to. For brevity, here&#8217;s the diff:</p>
<pre><code>@@ -1,6 +1,7 @@
       .sub '__onload' :load
           .local pmc optable
           ## namespace Simple::Grammar
+          load_bytecode 'PGE.pbc'
           $I0 = find_type 'Simple::Grammar'
           if $I0 != 0 goto onload_186
           $P0 = subclass 'PGE::Grammar', 'Simple::Grammar'
@@ -288,6 +289,7 @@
           .local string target    :unique_reg
           .local pmc mfrom, mpos  :unique_reg
           .local int cpos, iscont :unique_reg
+          load_bytecode 'PGE/Match.pir'
           $P0 = getclass 'PGE::Match'
           (mob, cpos, target, mfrom, mpos, iscont) = $P0.'new'(mob, adverbs :flat :named)           .local pmc ustack :unique_reg
@@ -321,6 +323,7 @@
           goto fail_cut
         R: # concat
         R147: # subrule ws
+          load_bytecode 'PGE/Regex.pir'
           captob = captscope
           $P0 = getattribute captob, '$.pos'
           $P0 = pos
</code></pre>
<p>And now, one and a half hours later, for the grand finale&#8230;</p>
<pre><code>$ parrot simpleparse.pir
"VAR1" => PMC 'PGE::Match' { ... }</code></pre>
<p>Voilà! My first excursion into Parrot. A bit fiddly, but I enjoyed myself <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/excursions-into-parrot/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Baleegal!</title>
		<link>http://porg.es/blog/baleegal</link>
		<comments>http://porg.es/blog/baleegal#comments</comments>
		<pubDate>Sat, 24 Jun 2006 11:37:45 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[Unicode]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://porg.es/blog/baleegal</guid>
		<description><![CDATA[XML 1.0 allows you to insert characters from the C1 control code range, whilst those from the C0 range are outright forbidden. XML 1.1 allows you to insert characters from the C0 range as long as they are escaped as character entity references, and mandates that you do the same for those from the C1 [...]]]></description>
			<content:encoded><![CDATA[<p>XML 1.0 allows you to insert characters from the C1 control code range, whilst those from the C0 range are outright forbidden.</p>
<p>XML 1.1 allows you to insert characters from the C0 range <em>as long as they are escaped as character entity references</em>, and mandates that you do the same for those from the C1 range.</p>
<p>This little fact is the reason why not all XML 1.0-valid documents are valid under XML 1.1. Nasty.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/baleegal/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why not to design fixed-width layouts</title>
		<link>http://porg.es/blog/why-not-to-design-fixed-width-layouts</link>
		<comments>http://porg.es/blog/why-not-to-design-fixed-width-layouts#comments</comments>
		<pubDate>Thu, 09 Mar 2006 07:15:58 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Critique]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://porg.es/blog/why-not-to-design-fixed-width-layouts</guid>
		<description><![CDATA[Any more questions?]]></description>
			<content:encoded><![CDATA[<p><a class="imagelink" href="http://porg.es/blog/wp-content/uploads/2006/03/fixedwidth.png" title="Un-named news site"><img id="image58" src="http://porg.es/blog/wp-content/uploads/2006/03/fixedwidth.thumbnail.png" alt="Un-named news site" /></a></p>
<p>Any more questions?</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/why-not-to-design-fixed-width-layouts/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>combobulate</title>
		<link>http://porg.es/blog/combobulate</link>
		<comments>http://porg.es/blog/combobulate#comments</comments>
		<pubDate>Mon, 17 Oct 2005 01:54:50 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Default]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Firefox]]></category>

		<guid isPermaLink="false">http://porges.name/blog/combobulate</guid>
		<description><![CDATA[Following the example set by the excellent hoodwink&#8217;d, here is a GreaseMonkey script. It grabs customized CSS/XPath expressions for the current page you&#8217;re on (currently only from my server, but you can edit that if you like). What does this mean? Ad-blocking via CSS and XPath on a per-domain basis, with no need to update [...]]]></description>
			<content:encoded><![CDATA[<p>Following the example set by the excellent <a href="http://hoodwink.d">hoodwink&#8217;d</a>, here is a GreaseMonkey script. It grabs customized CSS/XPath expressions for the current page you&#8217;re on (currently only from my server, but you can edit that if you like). What does this mean?</p>
<p><em>Ad-blocking</em> via CSS and XPath <em>on a per-domain basis</em>, with no need to update your ad-blocking definitions. Ever. Oh, and did I mention Javascript injection?</p>
<p>The next step is to create a miniwiki that allows editing of the items, for true community-oriented ad-busting. Assistance in this regard would be appreciated. After that I will add support for multiple &#8220;overlays&#8221; of CSS/XPath/Javascript rules.</p>
<p>Oh, and the link: <a href="http://porges.name/combobulate.user.js">Combobulate. Cleaning up the web one site at a time.</a></p>
<p>Current example pages:</p>
<ul>
<li>http://cnn.com</li>
<li>http://boingboing.net</li>
<li>http://deviantart.com</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/combobulate/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Starting over</title>
		<link>http://porg.es/blog/starting-over</link>
		<comments>http://porg.es/blog/starting-over#comments</comments>
		<pubDate>Wed, 04 May 2005 23:46:37 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Default]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://porges.name/blog/starting-over</guid>
		<description><![CDATA[Ruby + Berkeley XML DB = Weblog software.]]></description>
			<content:encoded><![CDATA[<p>Ruby + Berkeley XML DB = Weblog software.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/starting-over/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Berkeley XML DB (Doesn&#8217;t Build)</title>
		<link>http://porg.es/blog/berkeley-xml-db-doesnt-build</link>
		<comments>http://porg.es/blog/berkeley-xml-db-doesnt-build#comments</comments>
		<pubDate>Fri, 08 Apr 2005 05:22:56 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Default]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://porges.name/blog/berkeley-xml-db-doesnt-build</guid>
		<description><![CDATA[Having a look around for a free native-XML database, I decided upon SleepyCat&#8217;s offering, as it touts a PHP API. Unfortunately I can&#8217;t get it to build, with a cryptic failure message haunting me. Update: An email to the mailing list provided an answer: DreamHost has an older version of GCC installed. So, I downloaded [...]]]></description>
			<content:encoded><![CDATA[<p>Having a look around for a free native-XML database, I decided upon SleepyCat&#8217;s offering, as it touts a PHP API. Unfortunately I can&#8217;t get it to build, with a cryptic failure message haunting me.</p>
<p>Update: An email to the mailing list provided an answer: DreamHost has an older version of GCC installed. So, I downloaded and built the latest GCC and tried again. I got much further in the process this time, but still a failure (something along the lines of &#8220;cannot find libstdc++.so.6&#8243<img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" />. I think I&#8217;ll wait until DreamHost upgrades to Debian Sarge before trying again.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/berkeley-xml-db-doesnt-build/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Four Elements</title>
		<link>http://porg.es/blog/four-elements</link>
		<comments>http://porg.es/blog/four-elements#comments</comments>
		<pubDate>Thu, 24 Mar 2005 01:42:53 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Default]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://porges.name/blog/four-elements</guid>
		<description><![CDATA[After moving to this domain and getting WordPress up and running, I&#8217;ve decided to restart &#8220;the project&#8221; now that I know a lot more about what I&#8217;m doing. Firstly, I will build a basic &#8216;blog-engine&#8217; which supports Atom and Atom only, then develop the web interface from there. This will require a degree of separation: [...]]]></description>
			<content:encoded><![CDATA[<p>After moving to this domain and getting WordPress up and running, I&#8217;ve decided to restart &#8220;the project&#8221; now that I know a lot more about what I&#8217;m doing. Firstly, I will build a basic &#8216;blog-engine&#8217; which supports Atom and Atom only, then develop the web interface from there.</p>
<p>This will require a degree of separation: Content will be stored in the database (SQL-based), and will then be supplied with structure via XSLT into either Atom or HTML format. I&#8217;m thinking that I will write a simple script to transform what is returned from the database queries into XML, possibly by replacing the column names with elements.</p>
<p>Presentation and behaviour are then also separated from HTML into external CSS and JS files. So that leaves me with four components of a web page:</p>
<ul>
<li>Content</li>
<li>Structure</li>
<li>Presentation</li>
<li>Behaviour</li>
</ul>
<p>, each of which can be modified seperately.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/four-elements/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&lt;abbr&gt; and CSS</title>
		<link>http://porg.es/blog/abbr-and-css</link>
		<comments>http://porg.es/blog/abbr-and-css#comments</comments>
		<pubDate>Mon, 28 Feb 2005 21:57:25 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://porges.name/blog/abbr-and-css</guid>
		<description><![CDATA[Here&#8217;s a go at combining the &#60;abbr&#62; element with some CSS to create abbreviations which expand in the text when you mouse-over them. Inline expanding abbreviations. The &#8220;title&#8221; attribute is pulled out and placed in brackets using generated content, and is displayed when the abbreviation itself is hovered on.]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a go at combining the &lt;abbr&gt; element with some CSS to create abbreviations which expand in the text when you mouse-over them.</p>
<p><a href="http://porges.name/examples/abbr.htm">Inline expanding abbreviations.</a></p>
<p>The &#8220;title&#8221; attribute is pulled out and placed in brackets using generated content, and is displayed when the abbreviation itself is hovered on.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/abbr-and-css/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bayesian scavenging</title>
		<link>http://porg.es/blog/bayesian-scavenging</link>
		<comments>http://porg.es/blog/bayesian-scavenging#comments</comments>
		<pubDate>Thu, 30 Dec 2004 09:42:06 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://porges.name/blog/bayesian-scavenging</guid>
		<description><![CDATA[Spent the afternoon coding the algorithms from SpamBayes into PHP. Hurrah for open-source!]]></description>
			<content:encoded><![CDATA[<p>Spent the afternoon coding the algorithms from <a href="http://spambayes.sourceforge.net">SpamBayes</a> into PHP. Hurrah for open-source!</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/bayesian-scavenging/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spam prevention process</title>
		<link>http://porg.es/blog/spam-prevention-process</link>
		<comments>http://porg.es/blog/spam-prevention-process#comments</comments>
		<pubDate>Wed, 29 Dec 2004 05:22:30 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://porges.name/blog/spam-prevention-process</guid>
		<description><![CDATA[A quick outline of the process I&#8217;ll be using to prevent comment spam. Hidden field hashing as described in &#8220;Comment spam prevention&#8220;. This also forces a preview, which may fool spambots, and allows you to check the comments (for such things as invalid HTML, too many links) without having to perform all the following for [...]]]></description>
			<content:encoded><![CDATA[<p>A quick outline of the process I&#8217;ll be using to prevent comment spam.</p>
<ol>
<li>Hidden field hashing as described in &#8220;<a href="http://escrib.blogspot.com/2004/12/comment-spam-prevention.html">Comment spam prevention</a>&#8220;. This also forces a preview, which may fool spambots, and allows you to check the comments (for such things as invalid HTML, too many links) without having to perform all the following for the preview.</li>
<li>Check the IP address against <a href="http://www.spamhaus.org/">Spamhaus</a>, <a href="http://dsbl.org/">DSBL</a>, and any other RBLs that the user specifies. If one matches, block the IP for a short period and reject the comment.</li>
<li>Find URIs.</li>
<li>Check URIs against a blacklist, such as <a href="http://www.jayallen.org/comment_spam/blacklist.txt">MT-Blacklist</a>, or a personal blacklist such as <a href="http://simon.incutio.com/blacklist.txt">Simon Willison&#8217;s blacklist</a>. If one matches, block the IP for a short period and reject the comment.</li>
<li>Check URIs against <a href="http://surbl.org/">SURBL</a>. If one matches, block the IP for a short period and reject the comment.</li>
<li>Run the comment through a <a href="http://en.wikipedia.org/wiki/Bayesian_filtering">Bayesian filter</a>. If the match to other spam comments is high, block the IP for a short period and reject the comment. If the match is unsure, move the comment to the moderation queue.</li>
<li>Optionally, for the paranoid:
<ol>
<li>Follow links in the post (following all redirects) and check all the links on the resultant page.</li>
<li>Force all comments to join the moderation queue, unless the user accepts an email verification or is authenticated through other means (TypeKey, site-specific registration).</li>
</ol>
</li>
</ol>
<p>If the comment passes all these tests it is probably not spam. Any transformations can be performed and the comment stored in the database.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/spam-prevention-process/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

