<?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; C#</title>
	<atom:link href="http://porg.es/blog/tag/c-sharp/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description></description>
	<lastBuildDate>Thu, 12 Jan 2012 23:45:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</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>Things I&#8217;d like to see in C#: Conditional interface implementation</title>
		<link>http://porg.es/blog/things-id-like-to-see-in-c-conditional-interface-implementation</link>
		<comments>http://porg.es/blog/things-id-like-to-see-in-c-conditional-interface-implementation#comments</comments>
		<pubDate>Sat, 20 Sep 2008 02:54:29 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[idea]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=171</guid>
		<description><![CDATA[To explain this, imagine you are designing a type Wrapper&#60;T&#62;, which is just a simple wrapper around a type T. class Wrapper&#60;T&#62; &#123; T value; public Wrapper&#40;T theObject&#41; &#123; value = theObject; &#125; // Some other methods... &#125; Now, since this is a just a wrapper around some type T, we would like to implement [...]]]></description>
			<content:encoded><![CDATA[<p>To explain this, imagine you are designing a type <code>Wrapper&lt;T&gt;</code>, which is just a simple wrapper around a type <code>T</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">class</span> Wrapper<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
    T value<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> Wrapper<span style="color: #008000;">&#40;</span>T theObject<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        value <span style="color: #008000;">=</span> theObject<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #008080; font-style: italic;">// Some other methods...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now, since this is a just a wrapper around some type <code>T</code>, we would like to implement some simple interfaces around this object. For example, if <code>T</code> is comparable, we would like the wrapper class to implement <code>IComparable&lt;Wrapper&lt;T&gt;&gt;</code>. However this is not possible with C#. In Haskell we would have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Disposable a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> Disposable <span style="color: green;">&#40;</span>Wrapper a<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span> <span style="color: #339933; font-weight: bold;">...</span></pre></div></div>

<p>This says &#8220;if <code>a</code> is a type that is an instance of <code>Disposable</code>, then the type <code>Wrapper a</code> is also an instance of <code>Disposable</code>”. In C#, I&#8217;d like to see something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">class</span> Wrapper<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">:</span> IComparable<span style="color: #008000;">&lt;</span>Wrapper<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span>
        when T <span style="color: #008000;">:</span> IComparable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008080; font-style: italic;">//this is the syntax extension</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And then in any methods that the interface specifies (in this case <code>int Compare(Wrapper&lt;T&gt; other)</code>), it is assumed that for any object of type <code>T</code> you have access to all the methods that <code>T</code> has when it implements <code>IComparable&lt;T&gt;</code>.</p>
<p>At the moment the best you can do is to <em>always</em> implement <code>IComparable&lt;Wrapper&lt;T&gt;&gt;</code> and just throw a runtime exception when <code>T</code> doesn&#8217;t implement <code>IComparable&lt;T&gt;</code>, which isn&#8217;t very nice.</p>
<p>A simple idea, but it would add great power to C#.</p>
<p>After some (very quick) research I haven&#8217;t found anything that suggests anyone else has attempted to get this into C#, but there is <a href="http://homepages.cwi.nl/~ralf/JavaGI/paper.pdf">JavaGI</a> (see section 2.4 for the equivalent of what this post talks about) where the authors have extended Java to deal with what they term ‘generalized interfaces’—a concept which alters Java (rather radically) to make interface implementations similar to Haskell&#8217;s typeclasses.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/things-id-like-to-see-in-c-conditional-interface-implementation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing IEnumerable easily</title>
		<link>http://porg.es/blog/implementing-ienumerable-easily</link>
		<comments>http://porg.es/blog/implementing-ienumerable-easily#comments</comments>
		<pubDate>Fri, 04 Apr 2008 09:43:02 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IEnumerable]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=125</guid>
		<description><![CDATA[Say that you’re implementing a linked list, and you want an enumerator: public IEnumerator&#60;T&#62; GetEnumerator&#40;&#41; &#123; return new Stream&#60;T,Node&#62;&#40;first, node =&#62; node.next == null ? null : Tuple.Of&#40;node.next, node.datum&#41;.AsNullable&#40;&#41;&#41;; &#125; This uses the following utility class to implement the enumerator in one line (along with some code for Tuples and an extension method for structs): [...]]]></description>
			<content:encoded><![CDATA[<p>Say that you’re implementing a linked list, and you want an enumerator:</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> Stream<span style="color: #008000;">&lt;</span>T,Node<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>first,
        node <span style="color: #008000;">=&gt;</span> node<span style="color: #008000;">.</span><span style="color: #0000FF;">next</span> <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;">null</span> <span style="color: #008000;">:</span> Tuple<span style="color: #008000;">.</span><span style="color: #0000FF;">Of</span><span style="color: #008000;">&#40;</span>node<span style="color: #008000;">.</span><span style="color: #0000FF;">next</span>, node<span style="color: #008000;">.</span><span style="color: #0000FF;">datum</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsNullable</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></pre></div></div>

<p>This uses the following utility class to implement the enumerator in one line (along with some code for Tuples and an extension method for structs):</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> Stream<span style="color: #008000;">&lt;</span>Tdata, Tstate<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> IEnumerator<span style="color: #008000;">&lt;</span>Tdata<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Tdata current<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Tstate initialState<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Tstate state<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Func<span style="color: #008000;">&lt;</span>Tstate, Pair<span style="color: #008000;">&lt;</span>Tstate, Tdata<span style="color: #008000;">&gt;?&gt;</span> moveNext<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Stream<span style="color: #008000;">&#40;</span>Tstate initialState, Func<span style="color: #008000;">&lt;</span>Tstate, Pair<span style="color: #008000;">&lt;</span>Tstate, Tdata<span style="color: #008000;">&gt;?&gt;</span> calcNextValue<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        moveNext <span style="color: #008000;">=</span> calcNextValue<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">initialState</span> <span style="color: #008000;">=</span> initialState<span style="color: #008000;">;</span>
        Reset<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#region IEnumerator&lt;Tdata&gt; Members</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Tdata Current
    <span style="color: #008000;">&#123;</span>
        get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> current<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #008080;">#region IDisposable Members</span>
&nbsp;
    <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>
        Dispose<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;">;</span>
        GC<span style="color: #008000;">.</span><span style="color: #0000FF;">SuppressFinalize</span><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;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span> disposing<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>disposing<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            var disposeCurrent <span style="color: #008000;">=</span> current <span style="color: #0600FF; font-weight: bold;">as</span> IDisposable<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>disposeCurrent <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                disposeCurrent<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            var disposeState <span style="color: #008000;">=</span> state <span style="color: #0600FF; font-weight: bold;">as</span> IDisposable<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>disposeState <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                disposeState<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</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;">//safe; have checked already above.</span>
                <span style="color: #008080; font-style: italic;">//type of state == type of initialstate</span>
                <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>IDisposable<span style="color: #008000;">&#41;</span>initialState<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><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: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #008080;">#region IEnumerator Members</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">object</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Collections</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">IEnumerator</span><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> current<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>
        var result <span style="color: #008000;">=</span> moveNext<span style="color: #008000;">&#40;</span>state<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>result<span style="color: #008000;">.</span><span style="color: #0000FF;">HasValue</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            current <span style="color: #008000;">=</span> result<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Right</span><span style="color: #008000;">;</span>
            state <span style="color: #008000;">=</span> result<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Left</span><span style="color: #008000;">;</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;">else</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: #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>
        state <span style="color: #008000;">=</span> initialState<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/implementing-ienumerable-easily/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>object.Equals handles null values correctly</title>
		<link>http://porg.es/blog/objectequals-handles-null-values-correctly</link>
		<comments>http://porg.es/blog/objectequals-handles-null-values-correctly#comments</comments>
		<pubDate>Thu, 27 Mar 2008 20:58:12 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://porg.es/blog/objectequals-handles-null-values-correctly</guid>
		<description><![CDATA[Here’s the source code, as disassembled by Reflector: public static bool Equals&#40;object objA, object objB&#41; &#123; return &#40;&#40;objA == objB&#41; &#124;&#124; &#40;&#40;&#40;objA != null&#41; &#38;&#38; &#40;objB != null&#41;&#41; &#38;&#38; objA.Equals&#40;objB&#41;&#41;&#41;; &#125; It seems that not even Microsoft knows this! I spotted this code, from ASP.NET’s MVC implementation, on Scott Hanselman’s blog: return &#40;other != null&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>Here’s the source code, as disassembled by Reflector:</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: #6666cc; font-weight: bold;">bool</span> Equals<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> objA, <span style="color: #6666cc; font-weight: bold;">object</span> objB<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;">&#40;</span><span style="color: #008000;">&#40;</span>objA <span style="color: #008000;">==</span> objB<span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>objA <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>objB <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> objA<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>objB<span style="color: #008000;">&#41;</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>It seems that not even Microsoft knows this! I spotted this code, from ASP.NET’s MVC implementation, on <a href="http://www.hanselman.com/blog/TheWeeklySourceCode21ASPNETMVCPreview2SourceCode.aspx">Scott Hanselman’s blog</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>other <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_first <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>_first <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span>
    <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_first <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> other<span style="color: #008000;">.</span>_first<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>_first<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_second <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>_second <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span>
    <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_second <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> other<span style="color: #008000;">.</span>_second<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>_second<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This can be rewritten as:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>other <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_first, _first<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
  <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>other<span style="color: #008000;">.</span>_second, _second<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Much nicer!</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/objectequals-handles-null-values-correctly/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>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>Toilets and Thread Synchronization in C#</title>
		<link>http://porg.es/blog/toilets-and-thread-synchronization-in-c</link>
		<comments>http://porg.es/blog/toilets-and-thread-synchronization-in-c#comments</comments>
		<pubDate>Sat, 16 Sep 2006 07:15:15 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://porg.es/blog/toilets-and-thread-synchronization-in-c</guid>
		<description><![CDATA[It dates from 2003, but this is still the best explanation of thread synchronization using Monitors that I&#8217;ve read.]]></description>
			<content:encoded><![CDATA[<p>It dates from 2003, but <a href="http://pluralsight.com/blogs/mike/archive/2004/12/13/3905.aspx">this is still the best explanation of thread synchronization using Monitors that I&#8217;ve read</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/toilets-and-thread-synchronization-in-c/feed</wfw:commentRss>
		<slash:comments>0</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>
	</channel>
</rss>

