<?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>... master of none</description>
	<lastBuildDate>Sat, 12 Sep 2009 07:57:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Fun(c) with C# 3.0</title>
		<link>http://porg.es/blog/func-with-c-30</link>
		<comments>http://porg.es/blog/func-with-c-30#comments</comments>
		<pubDate>Tue, 09 Oct 2007 02:03:31 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Cool]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Programming]]></category>

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

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">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: #000000;">&#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: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span>
        from it <span style="color: #0600FF;">in</span> source
        select func<span style="color: #000000;">&#40;</span>it<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">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: #000000;">&#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> from<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>F it <span style="color: #0600FF;">in</span> from<span style="color: #000000;">&#41;</span>
        yield <span style="color: #0600FF;">return</span> func<span style="color: #000000;">&#40;</span>it<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">static</span> T FoldL<span style="color: #008000;">&lt;</span>F, T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#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: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    T last <span style="color: #008000;">=</span> init<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>F it <span style="color: #0600FF;">in</span> source<span style="color: #000000;">&#41;</span>
        last <span style="color: #008000;">=</span> func<span style="color: #000000;">&#40;</span>last, it<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">return</span> last<span style="color: #008000;">;</span>
<span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">static</span> T FoldL1<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#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: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    T last <span style="color: #008000;">=</span> source.<span style="color: #0000FF;">First</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>T it <span style="color: #0600FF;">in</span> source.<span style="color: #0000FF;">Skip</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        last <span style="color: #008000;">=</span> func<span style="color: #000000;">&#40;</span>last, it<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">return</span> last<span style="color: #008000;">;</span>
<span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">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: #000000;">&#40;</span><span style="color: #0600FF;">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: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> x <span style="color: #008000;">=&gt;</span> second<span style="color: #000000;">&#40;</span>first<span style="color: #000000;">&#40;</span>x<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#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: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> numbers <span style="color: #008000;">=</span> <span style="color: #000000;">&#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: #000000;">&#125;</span><span style="color: #008000;">;</span>
Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span>, <span style="color: #FF0000;">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;">foreach</span> <span style="color: #000000;">&#40;</span>var k <span style="color: #0600FF;">in</span> FFC.<span style="color: #0000FF;">Map</span><span style="color: #000000;">&#40;</span>addOne, numbers<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>k <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span>,<span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> addTwo <span style="color: #008000;">=</span> addOne.<span style="color: #0000FF;">Compose</span><span style="color: #000000;">&#40;</span>addOne<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var k <span style="color: #0600FF;">in</span> FFC.<span style="color: #0000FF;">Map</span><span style="color: #000000;">&#40;</span>addTwo, numbers<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>k <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var sum <span style="color: #008000;">=</span> FFC.<span style="color: #0000FF;">FoldL</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>x, y<span style="color: #000000;">&#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: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>sum <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#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;">static</span> <span style="color: #FF0000;">class</span> FakeLazyList
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">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: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T, T<span style="color: #008000;">&gt;</span> f, T x<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            yield <span style="color: #0600FF;">return</span> x<span style="color: #008000;">;</span>
            x <span style="color: #008000;">=</span> f<span style="color: #000000;">&#40;</span>x<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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;">public</span> <span style="color: #FF0000;">class</span> Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> 
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> Lazy<span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> del<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Del <span style="color: #008000;">=</span> del<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">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;">private</span> T PValue<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> HasValue <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">implicit</span> <span style="color: #0600FF;">operator</span> T<span style="color: #000000;">&#40;</span>Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> f<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>f.<span style="color: #0000FF;">HasValue</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            f.<span style="color: #0000FF;">PValue</span> <span style="color: #008000;">=</span> f.<span style="color: #0000FF;">Del</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            f.<span style="color: #0000FF;">HasValue</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">return</span> f.<span style="color: #0000FF;">PValue</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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;">public</span> <span style="color: #FF0000;">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: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> T _First<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">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;">public</span> T First <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _First<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Rest <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _Rest<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> LazyList<span style="color: #000000;">&#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: #000000;">&#41;</span>
    <span style="color: #000000;">&#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: #000000;">&#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;">public</span> IEnumerator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> Enumerator<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    IEnumerator IEnumerable.<span style="color: #0000FF;">GetEnumerator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> Enumerator<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">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: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> curr <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">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;">public</span> Enumerator<span style="color: #000000;">&#40;</span>LazyList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> parent<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            next <span style="color: #008000;">=</span> parent<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #008080; font-style: italic;">/* must implement both interfaces */</span>
        <span style="color: #FF0000;">object</span> IEnumerator.<span style="color: #0000FF;">Current</span> <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Current</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> T Current <span style="color: #000000;">&#123;</span>
                get <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>curr <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF;">else</span>
                        <span style="color: #0600FF;">return</span> curr._First<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> MoveNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> curr <span style="color: #008000;">=</span> next<span style="color: #008000;">;</span> next <span style="color: #008000;">=</span> next._Rest<span style="color: #008000;">;</span> <span style="color: #0600FF;">return</span> curr <span style="color: #008000;">!=</span> null<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Reset<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> NotSupportedException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> return<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">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: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>X, X<span style="color: #008000;">&gt;</span> f, X x<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">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: #000000;">&#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: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> Iterate<span style="color: #000000;">&#40;</span>f, f<span style="color: #000000;">&#40;</span>x<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">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: #000000;">&#40;</span>X x<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">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: #000000;">&#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: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> Repeat<span style="color: #000000;">&#40;</span>x<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">implicit</span> <span style="color: #0600FF;">operator</span> Lazy<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> f<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">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: #000000;">&#40;</span>f<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#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: #000000;">&#40;</span>x, <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Repeat<span style="color: #000000;">&#40;</span>x<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#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: #FF0000;">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: #FF0000;">int</span><span style="color: #008000;">&gt;</span>.<span style="color: #0000FF;">Iterate</span><span style="color: #000000;">&#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: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var y <span style="color: #0600FF;">in</span> ys<span style="color: #000000;">&#41;</span> Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>y<span style="color: #000000;">&#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: #FF0000;">int</span><span style="color: #008000;">&gt;</span>.<span style="color: #0000FF;">Repeat</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">89</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var y <span style="color: #0600FF;">in</span> ys<span style="color: #000000;">&#41;</span> Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>y<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#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;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Future<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">delegate</span> R FutureDelegate<span style="color: #008000;">&lt;</span>R<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> Future <span style="color: #000000;">&#40;</span>FutureDelegate<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> del<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                Del <span style="color: #008000;">=</span> del<span style="color: #008000;">;</span>
                Result <span style="color: #008000;">=</span> del.<span style="color: #0000FF;">BeginInvoke</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span>,<span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">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;">private</span> IAsyncResult Result<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> T PValue<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> HasValue <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> T Value <span style="color: #000000;">&#123;</span>
                get <span style="color: #000000;">&#123;</span>
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>HasValue<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>Result.<span style="color: #0000FF;">IsCompleted</span><span style="color: #000000;">&#41;</span>
                                        Result.<span style="color: #0000FF;">AsyncWaitHandle</span>.<span style="color: #0000FF;">WaitOne</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                                PValue <span style="color: #008000;">=</span> Del.<span style="color: #0000FF;">EndInvoke</span><span style="color: #000000;">&#40;</span>Result<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                                HasValue <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
                        <span style="color: #000000;">&#125;</span>
                        <span style="color: #0600FF;">return</span> PValue<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">implicit</span> <span style="color: #0600FF;">operator</span> T<span style="color: #000000;">&#40;</span>Future<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> f<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> f.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> MainClass <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//This is purposely wasteful!</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">ulong</span> Fib<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">ulong</span> n<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>n <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                        <span style="color: #0600FF;">return</span> Fib<span style="color: #000000;">&#40;</span>n<span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">+</span>Fib<span style="color: #000000;">&#40;</span>n<span style="color: #008000;">-</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>n <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">else</span> <span style="color: #008080; font-style: italic;">//n == 0</span>
                        <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #008080; font-style: italic;">//Demo using futures.</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;First, call synchronously.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>Fib<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">40</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Next, call asynchronously.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Future<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">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: #FF0000;">ulong</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#123;</span>
                        <span style="color: #0600FF;">return</span> Fib<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">40</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#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: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1500</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#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: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>fib40<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Need it now!</span>
                Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#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: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>fib40<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Already computed.</span>
        <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#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: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#123;</span><span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> Future<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#123;</span><span style="color: #0600FF;">return</span> x.<span style="color: #0000FF;">a</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">c</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Future<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#123;</span><span style="color: #0600FF;">return</span> y.<span style="color: #0000FF;">b</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#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: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#123;</span><span style="color: #0600FF;">return</span> x.<span style="color: #0000FF;">a</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
t2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Future<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#123;</span><span style="color: #0600FF;">return</span> y.<span style="color: #0000FF;">b</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
t3 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Future<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#123;</span><span style="color: #0600FF;">return</span> t1.<span style="color: #0000FF;">c</span><span style="color: #000000;">&#40;</span>t2<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#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>3</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>
