<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Matthew Podwysocki's Blog</title><link>http://weblogs.asp.net/podwysocki/default.aspx</link><description>Architect, Develop, Inspire...</description><language>en</language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><geo:lat>38.977109</geo:lat><geo:long>-77.385278</geo:long><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/MatthewPodwysockisBlog" type="application/rss+xml" /><item><title>The “Anti-For” Campaign</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/WXqFrboOwXo/the-anti-for-campaign.aspx</link><category>C#</category><category>F#</category><category>Functional Programming</category><category>Haskell</category><category>Erlang</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Thu, 25 Jun 2009 21:38:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7134577</guid><description>&lt;p&gt;Recently, there has been an effort launched called the &lt;a href="http://www.antiifcampaign.com/" mce_href="http://www.antiifcampaign.com/"&gt;“Anti-If Campaign”&lt;/a&gt; in which they deride the use of if statements and instead, focus on Object Oriented Principles in order to create more flexible designs.&amp;nbsp; Now certainly, I have a sympathetic ear to this cause as I’ve seen code that literally walks off the side of the screen due to nesting of if statements.&amp;nbsp; Pattern matching to me, especially at the top level of the function is actually quite beautiful in a way, such as the implementations in Haskell:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c847654b-3dff-4fd4-928b-52fdb03c6f0a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;--&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Haskell&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;lucas :: Int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Integer
lucas &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
lucas &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
lucas n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas (n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas (n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;And in Erlang, this also holds true:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:309094f4-0c8d-4e93-83af-86ac469bbdba" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;%&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Erlang
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-module&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(lucascalc).
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-export&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;([lucas&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]).

lucas(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
lucas(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
lucas(N) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas (N &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas (N &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Simple, easy to understand and best of all, no if statements.&amp;nbsp; But, instead of focus on this debate, I’d like to propose another which strikes closer to this functional programmers heart, the “Anti-For Campaign”.&amp;nbsp; This is simply to say that we should create and use composable functions instead of explicit for loops.&amp;nbsp; This is actually an old post I had written months ago and until now had been unfinished, but now with some inspiration, it’ll finally be done.&lt;/p&gt;

&lt;h2&gt;What and Why?&lt;/h2&gt;

&lt;p&gt;Before you throw all sorts of questions asking what and why, let me instead ask a question.&amp;nbsp; When you’re writing a loop, ask yourself the question, “What am I accomplishing in this loop?”&amp;nbsp; Chances are, it might be one of the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Query (Map, Filter, etc)&lt;/li&gt;

  &lt;li&gt;Aggregation (Sum, Count, etc)&lt;/li&gt;

  &lt;li&gt;Perform some side effect&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re doing more than one of those in a single loop, then well, you’re probably doing too much.&amp;nbsp; In fact, Martin Fowler’s Refactoring site has a refactoring called &lt;a href="http://www.refactoring.com/catalog/splitLoop.html" mce_href="http://www.refactoring.com/catalog/splitLoop.html"&gt;“Split Loop”&lt;/a&gt; which would solve that issue.&amp;nbsp; It is better for future refactorings and readability if we keep those loops pointed to do one thing, and one thing only.&amp;nbsp; Better yet, we could rid ourselves of that loop altogether, and that’s what we’ll focus on here.&lt;/p&gt;

&lt;p&gt;Looking at first two bullet points, you’ll notice most of LINQ is indeed built around those two to be able to query data as well as aggregate.&amp;nbsp; The final bullet point, we perform some sort of side effect, perhaps writing to a file, printing to the console, or even perhaps sending messages.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;So, what’s my problem with them?&amp;nbsp; My problem is that it focuses more on the How instead of the What.&amp;nbsp; Let’s look at a quick example down below of what I mean.&amp;nbsp; First, we’ll attempt to find all prime numbers under 100 using C# as an example language.&amp;nbsp; First in the How:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e7517c49-06cc-4423-a641-fec8ee8a3894" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IsPrime(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; i) {
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lim &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Math.Sqrt(i);
    
    Func&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; check &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
    check &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; j &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        j &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lim &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;||&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;%&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; j &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; check(j &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
        
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; check(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
}

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Main(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[] args) {
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; numbers &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; output &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; List&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;();

    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; number &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; numbers)
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(IsPrime(number)) output.Add(number);
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The problem is that if we then want to compose another operation, well, it’s really hard to do inside of these for loops.&amp;nbsp; We’re too focused on the how at this point.&amp;nbsp; Instead, getting to know generics and lazy evaluation, in .NET 2.0 and beyond, we were able to write generic functions to take advantage of some functional constructs.&amp;nbsp; This will give us a more declarative style that we can now focus on the what.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:59330329-7867-403f-a083-540beaf692a4" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IEnumerable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Filter&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IEnumerable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; items,
    Func&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; predicate) {
    
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; item &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; items)
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(predicate(item))
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; item;
}

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Main(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[] args) {
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; numbers &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
    
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; primes &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; items.Filter(x &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IsPrime(x));
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Of course we realize that LINQ already has such constructs built in, so we could rewrite the entire code above in just one statement.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:63f23cd3-baf9-46ca-8d50-517f5ebabdfb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; primes &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
    .Where(x &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IsPrime(x));&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;And what’s better is that it is composable that we could do other operations such as aggregations (sum, count, etc) without much additional code:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3953a208-84d3-401f-9f1c-c451beab8611" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; primesCount &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
    .Where(x &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IsPrime(x))
    .Count();&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;And there you have it.&amp;nbsp; We not only have a query, but also an aggregation.&amp;nbsp; Try doing that with those non-composable loops!&amp;nbsp; &lt;a href="http://www.codethinked.com/post/2009/06/15/Life-After-Loops.aspx" mce_href="http://www.codethinked.com/post/2009/06/15/Life-After-Loops.aspx"&gt;Justin Etheredge has a nice writeup&lt;/a&gt; as well recently on the subject.&lt;/p&gt;

&lt;h2&gt;Coping Strategies&lt;/h2&gt;

&lt;p&gt;Functional languages tend to deemphasize the use of such constructs.&amp;nbsp; In fact, Haskell has neither a for loop nor a while loop, and languages such as F# and OCaml have limited support for such constructs in terms of no break and continue.&amp;nbsp; We tend to look at those two in particular with suspicion due to the fact that it cannot return a value and instead it mutates state in some fashion.&amp;nbsp; With that in mind, how do we cope with the fact that those aren’t available to us?&amp;nbsp; Above I showed a basic concept of a filter instead of an explicit loop, but what about some other considerations?&lt;/p&gt;

&lt;p&gt;Some things we might want to consider with some links to some of my previous posts on the subject:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/08/13/recursing-on-recursion-continuation-passing.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/08/13/recursing-on-recursion-continuation-passing.aspx"&gt;Explicit Recursion&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;Transforming each item (Map/Select &amp;amp; SelectMany in LINQ)&lt;/li&gt;

  &lt;li&gt;Selecting items (Filter/Where in LINQ)&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/02/14/fun-with-folds.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/02/14/fun-with-folds.aspx"&gt;Aggregating Data&lt;/a&gt; (Folds/Aggregate in LINQ)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In fact, many times that people could consider using explicit recursion could instead use a fold to aggregate the data which then cuts out the issue of tail call optimization.&amp;nbsp; By truly understanding the goals of LINQ as well as the concepts of functional programming, we can realize that most of the looping that we do can indeed be replaced by the above, outside of side effects of course.&lt;/p&gt;

&lt;h2&gt;Now What About Those Side Effects?&lt;/h2&gt;

&lt;p&gt;Is there a place where we draw the line and say that explicit loops are ok?&amp;nbsp; Eric Lippert was recently asked about the reasoning of the &lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx"&gt;lack of the ForEach extension method on IEnumerable&amp;lt;T&amp;gt;&lt;/a&gt;.&amp;nbsp; His response was that he was philosophically opposed to such an endeavor as the whole approach is to cause a side effect.&amp;nbsp; As IEnumerable&amp;lt;T&amp;gt; collections are immutable, he doesn’t believe it makes as much sense because you wouldn’t be side effecting the collection itself.&amp;nbsp; Not only that, but introducing closures can complicate object lifetimes and all sorts of potential reference issues.&lt;/p&gt;

&lt;p&gt;What about me?&amp;nbsp; I understand his concerns, and in C#, I can certainly see where he is coming from.&amp;nbsp; However, in F# we have such constructs readily available to us in the iter and iteri functions as shown below in F# Interactive:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:62c5fb8e-73f1-4057-b68c-f2ee5de6ee16" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; flip f y x &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; f x y
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; [&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;..&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; List.map((&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; List.filter(flip (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;%&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; List.iter(printfn &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;%d&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);;
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;6&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;12&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;18&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; [&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;..&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; List.map((&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; List.filter(flip (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;%&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; List.iteri(printfn &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;%d\t%d&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);;
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;6&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;12&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;       &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;18&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Outside of logging, writing to the console and such are rare in functional programming, so once again, I can certainly understand the concern.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I hope by going through some basic scenarios that we will indeed question our code the next time we see that we are writing that explicit loop with a for and a while.&amp;nbsp; With a tool chest filled with such functions as transforming every item, to filtering content, to aggregating data and so on, we can realize that we can create composable solutions instead of creating mutable collections or mutable variables and aggregating to them which are not as much.&amp;nbsp; So, come and join me in the “Anti-For Campaign”.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7134577" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=WXqFrboOwXo:9mwY_-AJEOM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=WXqFrboOwXo:9mwY_-AJEOM:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=WXqFrboOwXo:9mwY_-AJEOM:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=WXqFrboOwXo:9mwY_-AJEOM:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=WXqFrboOwXo:9mwY_-AJEOM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=WXqFrboOwXo:9mwY_-AJEOM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/WXqFrboOwXo" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7134577</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/06/26/the-anti-for-campaign.aspx</feedburner:origLink></item><item><title>Providing Safe Alternatives</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/dD9wORrsLWI/providing-safe-alternatives.aspx</link><category>F#</category><category>Functional Programming</category><category>Haskell</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Wed, 24 Jun 2009 06:18:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7133468</guid><description>&lt;p&gt;When I was reading through &lt;a href="http://realworldhaskell.org" mce_href="http://realworldhaskell.org"&gt;Real World Haskell&lt;/a&gt;, I was struck several times by the mention of providing safe function alternatives.&amp;nbsp; The idea is to provide a function that in all cases returns a value as well as the one which is meant to accept valid input and throw exceptions should that contract be violated.&amp;nbsp; There is a real performance consideration to be taken into account as a function which repeatedly throws exceptions as logic will tend to overwhelm a system and slow it down significantly.&amp;nbsp; Imagine if you will an application which reads a large directory to check each file for an X509 certificate, whether it has one or not, and it throws an exception if one is not present.&amp;nbsp; The problem of course is there is no way to determine whether a file was signed at the time using the .NET class without resorting to P/Invoke (my favorite).&lt;/p&gt;

&lt;p&gt;Recently on Twitter, there was talk finally of the &lt;a href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94072" mce_href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94072"&gt;inclusion of Guid.TryParse in .NET 4.0&lt;/a&gt;, yet somehow, &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282488" mce_href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282488"&gt;Enum.TryParse has not been addressed yet&lt;/a&gt; (gee, we’re consistent here).&amp;nbsp; The idea is to provide a way of determining whether the string is a Guid format without throwing an exception, and instead returning a flag indicating success and an out parameter with the value which is set to the value if there is success, else the default value.&amp;nbsp; Before we dig any further, let’s look at some terminology.&lt;/p&gt;

&lt;h2&gt;Partial Versus Total Functions&lt;/h2&gt;

&lt;p&gt;To get some terminology straight, let’s talk about partial functions versus total functions.&amp;nbsp; Partial functions are those functions which only return values for a defined subset of valid inputs, as throwing an exception is not considered a return value.&amp;nbsp; On the other hand, functions that return valid results over the entire input are considered to be total functions.&amp;nbsp; Let’s look at a quick example of what that means in F# interactive, the first being a partial function and the latter being a total function.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:45719e3b-2c94-4c73-8f83-8e6666282714" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Linq;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ([] : int list).First();;
System.InvalidOperationException: Sequence contains no elements
   at System.Linq.Enumerable.First[TSource](IEnumerable`&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; source)
   at &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;StartupCode$FSI_0003&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.$FSI_0003.main@()
stopped due to error
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ([] : int list).FirstOrDefault();;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;We see by the above that the First() function is a partial function because it only has defined behavior for non-empty collections, whereas FirstOrDefault() has defined behavior for empty lists and returns the default value for the given generic type.&amp;nbsp; When we’re writing our code, we better know which one we’re dealing with as default values can cause unintended consequences.&amp;nbsp; Without proper tests around these calls, this can be hard to manage.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;Mitigating these partial functions with calls to such things as the Count() extension method is not necessarily the answer either.&amp;nbsp; When dealing with non-lazy lists such as an array or a List&amp;lt;T&amp;gt;, then such things as the number of items in our collection is well known and part of the state.&amp;nbsp; In the case of the lazy sequence, this is not the case, so any call to Count() would cause an evaluation of all items in the list, which could lead to unintended consequences.&amp;nbsp; So, if we’re chaining together 4 partial functions together, we could be doing something wrong.&amp;nbsp; All in all, total functions should be preferred usage, but of course within reason.&lt;/p&gt;

&lt;h2&gt;Can We Do Better Than Try?&lt;/h2&gt;

&lt;p&gt;Going back to one of the original points, can we do better than the standard Try (Parse/GetValue/etc) pattern that is pervasive throughout .NET code?&amp;nbsp; After all, it tries to do two things at once, and the success flag can be missed if one so chose.&amp;nbsp;&amp;nbsp; F# decided to take a slightly different angle to this problem by returning the success flag and the value as a pair tuple.&amp;nbsp; Such an example would be like this memoize function below:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:61a0baf6-a98f-4403-8385-a861b4cc38ef" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Collections.Generic

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoize f &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; t &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Dictionary&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;_,_&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; t.TryGetValue(n) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; , value) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; value
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, _    ) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; res &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; f n
        t.Add(n, res)
        res&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What we’re doing above is calling the TryGetValue method which takes a key and returns a success flag as well as the out parameter for the value (set if there is success).&amp;nbsp; F# automatically creates a pair tuple for us which we can then pattern match against to take appropriate action.&amp;nbsp; Unfortunately, for this go around at least, C# does not treat tuples as first class data types, so a solution like this exclusive to F# at this time.&amp;nbsp; Overall, a much cleaner design, but maybe there’s another option?&lt;/p&gt;

&lt;h2&gt;Maybe there’s an Option…&lt;/h2&gt;

&lt;p&gt;Instead of dealing with the clumsiness of the above code, I would much rather solve this in a clean way using the F# Option type.&amp;nbsp; This allows us to definitively specify whether we have a value or not.&amp;nbsp; Oh great you say, another version of null.&amp;nbsp; Not quite, as sometimes null can be a value…&amp;nbsp; More to the point, we can have a universal solution to having a value without having to resort to the Nullable&amp;lt;T&amp;gt; or reference type null-ness nonsense.&amp;nbsp; Simply put, the Option type is no more than the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ade5c4ca-230d-4103-b6ca-615137e7c0d5" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Option&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt; =&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Some &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; None&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;You’ll notice two constructors for the Option type, the Some which takes a value, and the None which takes no parameters.&amp;nbsp; Now, using this to extend the Dictionary class much like the F# Map class, we can add a function which returns an Option type whether we have a value or not.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:007b6878-3890-454a-acdd-a52b159ca95f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Collections.Generic.Dictionary&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;k,&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;v&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Lookup(k:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;k) =&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.TryGetValue(k) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; , value) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Some value
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, _    ) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; None&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Then we can rewrite our memoize function to utilize this in a cleaner fashion using the Option type for pattern matching purposes to show our true intentions of whether we have a value or not.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bef88ed3-0ea4-4b0d-9b18-cc6301f08bf0" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoize f &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; t &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Dictionary&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;_,_&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; t.Lookup(n) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Some v &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; v
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; None   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; res &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; f n
        t.Add(n, res)
        res&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Much cleaner and shows better intent on our part.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;How far do we take this?&amp;nbsp; After all, making full functions everywhere sometimes isn’t feasible as we want to constrain our system.&amp;nbsp; Adding both partial and full implementations might just clutter up our entire API.&amp;nbsp; With this comes balance, but there are certainly places where this comes into play such as parsing values, querying values from collections and so on.&amp;nbsp; Could Code Contracts in .NET 4.0 help us here in terms of statically verifying that we’re not failing on a chained partial function?&amp;nbsp; We’re not quite finished here with this discussion as I’ve yet to cover the many values of null.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7133468" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=dD9wORrsLWI:bk4AODAWoFs:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=dD9wORrsLWI:bk4AODAWoFs:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=dD9wORrsLWI:bk4AODAWoFs:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=dD9wORrsLWI:bk4AODAWoFs:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=dD9wORrsLWI:bk4AODAWoFs:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=dD9wORrsLWI:bk4AODAWoFs:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/dD9wORrsLWI" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7133468</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/06/24/providing-safe-alternatives.aspx</feedburner:origLink></item><item><title>F# – Async Running with Continuation Scissors</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/vp57DiwpzrI/f-async-running-with-continuation-scissors.aspx</link><category>F#</category><category>Concurrency</category><category>Functional Programming</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Fri, 19 Jun 2009 23:09:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7131022</guid><description>&lt;p&gt;As you may have noticed, I’ve been covering a bit about concurrency on this blog lately, and for good reason.&amp;nbsp; Between Axum, Erlang, Scala and F#, there is a lot to explore with actor model concurrency, task based concurrency, data parallel applications and so on.&amp;nbsp; In the next couple of months, I have some talks coming up on concurrency and in particular with F#.&amp;nbsp; I’ve been covering a bit of that with F# and mailbox processing, but I wanted to step back a bit into the asynchronous workflows and hitting against a well known API.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;In this case, let’s walk through a simple example of using the &lt;a href="http://apiwiki.twitter.com/Twitter-API-Documentation"&gt;Twitter HTTP API&lt;/a&gt; to get a user timeline using F# asynchronous workflows.&amp;nbsp; Best of all, I’ve done all of the above code without ever once opening up Visual Studio and instead using the power of a simple text editor and F# interactive, I’m able to be quite productive.&amp;nbsp; It certainly makes me rethink my utter dependence on certain tools…&lt;/p&gt;

&lt;h2&gt;Give me some tweets… eventually, when you get around to it&lt;/h2&gt;

&lt;p&gt;In order to get started, we need some helpers when dealing with LINQ to XML.&amp;nbsp; &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/06/11/f-duck-typing-and-structural-typing.aspx"&gt;In a previous post&lt;/a&gt;, I talked about the need for such a thing due to the lack of implicit operator support in F#.&amp;nbsp; We can use the same operator that we had before such as the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:199c0565-d5a0-406e-a951-99c08a147a45" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;inline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; implicit arg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a) arg)

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) : string &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; implicit&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Alternatively, we could look at utilizing the XNamespace as well due to it also having an implicit operator to convert from a string.&amp;nbsp; This is helpful as it has a defined the op_Addition which allows us to add an XNamespace to a string to create an XName.&amp;nbsp; To take advantage we could add an additional operator to take care of this.&amp;nbsp; Below is a simple session of F# interactive to show how it might work:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4de5fc91-a1c5-4ccf-8767-beb3c23053ff" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!?&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) : string &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XNamespace &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; implicit;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!?&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ) : (string &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XNamespace)

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!?&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : (string &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XName) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:it@&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;205&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!?&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : XName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; foo {LocalName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
                      Namespace &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ;
                      NamespaceName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;If you’ll notice above, I curried the XNamespace implicit operator function and sure enough it recognizes it needs a string in order to produce an XName.&amp;nbsp; Now that we’ve defined our helpers, let’s start looking at how to get a user’s timeline in Twitter.&amp;nbsp; We’ll need to define a holder for our status data as well as the URL we will be using to post data.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3d95bf17-90e2-4de7-9105-5a4eb994d342" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; UserStatus &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; { UserName : string; ProfileImage : string; Status : string; StatusDate : DateTime }

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; timelineUrl &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;http://twitter.com/statuses/friends_timeline.xml&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;We’re interested at this point just of the user name, their image file location, their status and the date of the tweet.&amp;nbsp; Now let’s move onto retrieving and parsing the data.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:52dc38aa-f5cb-472d-89b5-17e7d0377517" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; parseDocument xml &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; document &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XDocument.Parse(xml)
  document.Root.Descendants()
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Seq.choose(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; node &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
         &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; node.Element(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;user&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;then&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
           &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; status &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HttpUtility.HtmlDecode(node.Element(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;text&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).Value)
           &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; image &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; node.Element(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;user&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).Element(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;profile_image_url&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).Value
           &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; userName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; node.Element(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;user&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).Element(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;screen_name&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).Value
           &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; statusDate &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DateTime.ParseExact(node.Element(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;created_at&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).Value, 
                                                &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;ddd MMM dd HH:mm:ss +0000 yyyy&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, 
                                                CultureInfo.CurrentCulture)
           Some { UserName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; userName; ProfileImage &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; image; Status &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; status; StatusDate &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; statusDate}
         &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; None)  

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getUserTimeline userName password &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  async { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; WebRequest.Create timelineUrl :&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HttpWebRequest
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;do&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.Credentials &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; NetworkCredential(userName, password)
          
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.AsyncGetResponse()
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; StreamReader(response.GetResponseStream())
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; xml &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader.AsyncReadToEnd()
          
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; parseDocument xml
        }&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;In the above code, we create a WebRequest, setting the credentials through Basic Authentication.&amp;nbsp; We’ll cover this approach for now with some attention to OAuth at some point in the relative near future.&amp;nbsp; After setting the identity, we get the response stream and read it to the end.&amp;nbsp; This gives us XML to parse and by using LINQ to XML, we’re able to iterate through each node, and if the user node is not null then we parse out each element for which we’re concerned.&lt;/p&gt;

&lt;p&gt;Now that we have the ability to do some asynchronous behavior, what do we do about it?&amp;nbsp; In previous posts, I’ve taken the road of running these operations in parallel, which then runs all instances and returns an answer.&amp;nbsp; But, if we’re&amp;nbsp; only executing one instance, how do we enlist in asynchronous behavior overall?&amp;nbsp; To illustrate, I’ll create a simple Windows Forms application to display the tweets in a textbox and have a refresh button to call the above function to repopulate our textbox.&amp;nbsp; First, let’s create the basic skeleton:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:40f65dc8-8a32-40b7-9322-de876ba1e274" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; form &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Form(Visible&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, TopMost &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, Width&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;500&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, Height&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;300&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; status &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; TextBox(Multiline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, ScrollBars&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ScrollBars.Vertical)
form.Controls.Add status
status.Width &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;400&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
status.Height &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;300&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; refresh &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Button(Text&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Refresh&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
form.Controls.Add refresh
refresh.Left &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;410&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
refresh.Top &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;10&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Now that we have a basic skeleton, let’s add the behavior to the button to refresh our textbox.&amp;nbsp; To do this, we simply add a handler to the button click event.&amp;nbsp; In there, we will call Async.RunWithContinuations which gives us the ability to handle not only the successful case, but also should an exception be thrown or some form of cancelation occurs.&amp;nbsp; Before we look at the implementing code, let’s look at the signature of RunWithContinuations:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:171314ff-f655-4ee9-b2fe-aa1f634cc891" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Async.RunWithContinuations;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it :
  (&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a -&amp;gt; unit) * (exn -&amp;gt; unit) * (OperationCanceledException -&amp;gt; unit) *&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  Async&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt; -&amp;gt; unit = &amp;lt;fun:clo@0-42&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What this tells us is that it takes a success continuation as the first argument, the exception continuation as the second argument, the cancelation continuation as the third, and then finally the asynchronous operation.&amp;nbsp; This allows us to uniquely handle each type of event, should they occur.&amp;nbsp; In this case, our success continuation should populate the textbox, whereas both the cancelation and the exceptional case should show a message box containing the exception.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f42edafa-9e26-4ecd-a7c1-0e1c225a8884" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;refresh.Click.Add
   (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; args &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
      status.Text &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
      Async.RunWithContinuations(
        (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; res &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
           res &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Seq.iter(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; item &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; status.Text &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; sprintf &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;User: %s - %s\r\n\r\n%s&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; item.UserName item.Status status.Text )),
        (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; exn &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MessageBox.Show(sprintf &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;An error occurred: %A&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; exn) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ignore),
        (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; cxn &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MessageBox.Show(sprintf &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;A cancellation error ocurred: %A&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; cxn) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ignore),
        (getUserTimeline &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;username&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;password&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)))&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt; As you can see, the behavior is rather straight forward to define handlers for our three cases.&amp;nbsp; Lastly, we provide our credentials to the getUserTimeline function and then when the button is clicked, we’ll get results much like the following screenshot.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_4626DF96.png" style="border-width: 0px; display: inline;" title="image" alt="image" mce_src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_4626DF96.png" border="0" height="259" width="461"&gt; &lt;/p&gt;

&lt;p&gt; Then we can keep pressing the refresh button and then asynchronously, it will handle the behavior appropriately, either in success or in failure.&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This is a pretty simply and naive application, but it can show how you can take advantage of asynchronous behavior using the Async Workflows in F#.&amp;nbsp; By being able to have the F# libraries handle the exceptions and cancelation, a major source of errors is reduced by quite a bit.&amp;nbsp; Giving us the power then to handle each one of these scenarios, either in success, failure or cancelation is a great concept.&amp;nbsp; There is still more yet to be covered including Futures and the inclusion of the Task Parallel Library as well as other adventures into Twitter and Bing APIs.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7131022" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=vp57DiwpzrI:wiPxE0maMGI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=vp57DiwpzrI:wiPxE0maMGI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=vp57DiwpzrI:wiPxE0maMGI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=vp57DiwpzrI:wiPxE0maMGI:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=vp57DiwpzrI:wiPxE0maMGI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=vp57DiwpzrI:wiPxE0maMGI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/vp57DiwpzrI" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7131022</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/06/20/f-async-running-with-continuation-scissors.aspx</feedburner:origLink></item><item><title>Revisiting Memoization</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/vjhGhvV88xY/revisiting-memoization.aspx</link><category>F#</category><category>Functional Programming</category><category>Haskell</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Thu, 18 Jun 2009 08:05:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7129025</guid><description>&lt;p&gt;After revisiting the &lt;a href="http://www.haskell.org/haskellwiki/Memoization" mce_href="http://www.haskell.org/haskellwiki/Memoization"&gt;Haskell Wiki&lt;/a&gt; recently, I wanted to look at &lt;a href="http://en.wikipedia.org/wiki/Memoization" mce_href="http://en.wikipedia.org/wiki/Memoization"&gt;memoization&lt;/a&gt; again for a brief second after talking about it &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/08/01/recursing-into-recursion-memoization.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/08/01/recursing-into-recursion-memoization.aspx"&gt;a while ago&lt;/a&gt;.&amp;nbsp; In particular, there were two competing ideas, one around using a generic dictionary/map for storing the memoized values, the other using a lazy list approach.&amp;nbsp; I wanted to briefly look into those as possible solutions.&lt;/p&gt;

&lt;h2&gt;The Baseline Approach&lt;/h2&gt;

&lt;p&gt;Let’s take a baseline approach, and instead of using the trite &lt;a href="http://en.wikipedia.org/wiki/Fibonacci_number" mce_href="http://en.wikipedia.org/wiki/Fibonacci_number"&gt;Fibonacci sequence&lt;/a&gt;, let’s use the &lt;a href="http://en.wikipedia.org/wiki/Lucas_number" mce_href="http://en.wikipedia.org/wiki/Lucas_number"&gt;Lucas Sequence&lt;/a&gt; instead.&amp;nbsp; It’s a slight variation on the theme, and enough to avoid that dreaded function.&amp;nbsp; The baseline should produce the following sequence: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123&lt;/p&gt;

&lt;p&gt;It might be implemented something like this:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5c52cb4d-2de7-4704-892b-b5832ae39ab3" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;function&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 0I &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 2I
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 1I &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 1I
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas (n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 1I) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas (n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 2I)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;You’ll notice that F# cannot handle BigInt values as literals like you can for integers and other primitives, but can be easily worked around by using a when clause in the pattern.&amp;nbsp; When we run the aforementioned function in F# interactive, we see that we get the expected results.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2b0d1c6a-8f8f-47e1-993d-878a4b91ebc9" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas 2I;;
Real: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.003&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, CPU: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.000&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen1: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen2: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : bigint &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 3I&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The problem with the above function is that when dealing with larger numbers, and calling this function repeatedly, you have to take a rather large performance hit each time.&amp;nbsp; And worse yet, when dealing with those larger numbers say greater than 30, the function just may hang.&amp;nbsp; We can solve this through a process called memoization, which I’ve talked about before.&amp;nbsp; The main idea is to cache all values that we’ve seen before for quicker retrieval instead of calculating the value each and every time.&lt;/p&gt;

&lt;h2&gt;The LazyList Approach&lt;/h2&gt;

&lt;p&gt;There are two ways I’ll cover memoization here, one using a lazy list and the other using the dictionary approach.&amp;nbsp; First, let’s look at how we might be able to use a list to store those values we’ve already calculated.&amp;nbsp; Let’s first look at the Haskell implementation which is similar to the Haskell Wiki example.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:226ac5df-c376-4fcf-a11f-f04d3cb1d87f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;memoized_lucas :: Int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Integer
memoized_lucas &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  (map lucas [&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ..] &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;where&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        lucas &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        lucas n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas (n&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas (n&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;


&lt;p&gt;The approach here is to perform a map of the lucas from 0 to infinity and taking the nth value.&amp;nbsp; Since this is partially applied, we need to wait on the last value to say which value we need.&amp;nbsp; When we run this through GHCi, we can calculate the 1000th Lucas number rather easily with me omitting the result and only checking the time:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c6616b04-6f60-49fb-8acd-c4b809e09541" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Main&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1000&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0.03&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; secs, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; bytes)
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Not only that, but the performance is rather good.&amp;nbsp; Any subsequent calls to this function should now be memoized such as the following call:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:837ca8cf-0adf-4195-b737-174eec729c98" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Main&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1000&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0.00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; secs, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; bytes)
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Now, how would we apply this to F#?&amp;nbsp; Can we get the same results as a lazy language?&amp;nbsp; If we switch over from using a list to a LazyList, the answer is yes, and the performance will be close.&amp;nbsp; First, we need to define the nth function which returns the value at the index specified.&amp;nbsp; Using pattern matching, we can then return the appropriate value, or recurse again.&amp;nbsp; We could use Seq.nth, but this may be more efficient.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b922427e-f3a1-4656-a042-a809c31a96dd" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;module&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; LazyList &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Microsoft.FSharp.Collections.LazyList

  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; nth list index &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; list &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Cons(h, t) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; index &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; index &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;then&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; h &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; nth t (index &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; _ &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; invalidArg &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;index&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;the index was outside the range of elements in the list&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Now that we’ve defined our nth function, it’s time to define our memoized Lucas function.&amp;nbsp; The logic is the same as above, and I’ve introduced a new operator to get the nth value which could be used as an infix operator.&amp;nbsp; The problem is that F# currently does not allow for the infix operator to be in the middle during partially applied functions, so instead I have to define it in the beginning.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f63fef6e-d979-4348-831e-21f82e950848" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Microsoft.FSharp.Math
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;!&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) s (n:bigint) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; LazyList.nth s (int n)

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas : bigint &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; bigint &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;function&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 0I &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 2I
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 1I &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 1I
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas (n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 1I) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas (n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 2I)
  (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;!&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) (LazyList.map lucas (seq {0I..&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; BigInt(Int32.MaxValue)} &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; LazyList.of_seq))&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This is a rather clumsy approach in some ways as we’re downcasting our bigint value to an integer, so in reality, we’re only able to get values up to Int32.MaxValue, which in most cases, should be ok. Let’s look at the performance of this in the same way we did above:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3bb1ac63-1723-4d5e-b437-0c9385d9cfde" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas 1000I;;
Real: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.092&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, CPU: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.093&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;15&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen1: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen2: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;You’ll notice that we are taking a slight performance penalty in terms of our Generation 0 memory in the GC.&amp;nbsp; Any subsequent calls are memoized given the following results:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f0889c20-02f6-4e76-987e-6a3a8dc77c15" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas 1000I;;
Real: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.001&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, CPU: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.015&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen1: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen2: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Let’s kick it up a notch to see what kind of penalty we may pay should we calculate the 10,000th Lucas sequence number:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8bc71079-acfa-483d-9f1f-b6566e40ca9c" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas 10000I;;
Real: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;07.922&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, CPU: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;07.815&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1511&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen1: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;356&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen2: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Ouch!&amp;nbsp; But, there is another approach that we can use and that I’ve shown before using the generic dictionary to store our values.&lt;/p&gt;

&lt;h2&gt;The Dictionary Approach&lt;/h2&gt;

&lt;p&gt;The generic dictionary approach is another approach rather than using the lazy list approach.&amp;nbsp; We simply create a dictionary outside of our inner function which stores these values from our inner Lucas sequence function.&amp;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8b5c10bc-f751-4f3b-ab43-1ea9194164fb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Collections.Generic
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; : bigint -&amp;gt; bigint =&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; t &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Dictionary&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;_,_&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; t.TryGetValue(n) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; , res) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; res
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, _  ) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 0I &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 2I
       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 1I &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 1I
       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
           &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; res &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas (n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 1I) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas (n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 2I)
           t.Add(n, res)
           res
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; lucas n&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The code itself is rather self explanatory.&amp;nbsp; We have the outer dictionary to store our values, then we have the inner function to calculate the value at the nth index.&amp;nbsp; We then determine whether it is in the dictionary and, if so return it, else calculate, add to the list and then return.&amp;nbsp; You’ll notice that we return not the literal value of the calculation, but instead a function which allows us to specify the nth at a later time.&amp;nbsp; This is important!&lt;/p&gt;

&lt;p&gt;Looking at the performance, we’ll notice the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:396327ea-26e5-4e4c-9789-ed0f90081c93" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; 1000I;;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Real: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.006&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, CPU: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.000&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen1: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen2: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; 1000I;;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Real: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.001&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, CPU: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.000&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen1: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen2: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;You’ll notice that the first calls is less expensive than the lazy list approach and has less impact on Generation 0 of the GC.&amp;nbsp; Any subsequent calls, as above will be nil.&amp;nbsp; But once again, let’s ratchet up the number to 10,000 much as we did above:&amp;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cf658ecc-b646-47ea-b94d-ee2c3734a34d" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; memoized_lucas&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; 10000I;;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Real: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.047&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, CPU: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00.062&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, GC gen0: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen1: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, gen2: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;You’ll notice a drastically less performance hit than the above solution.&amp;nbsp; &lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;If nothing else, it was an interesting exercise in seeing different ways of memoization in action and what kind of performance penalties you may pay along the way.&amp;nbsp; Now back to your regular programming…&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7129025" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=vjhGhvV88xY:3lDHpxeny7U:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=vjhGhvV88xY:3lDHpxeny7U:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=vjhGhvV88xY:3lDHpxeny7U:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=vjhGhvV88xY:3lDHpxeny7U:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=vjhGhvV88xY:3lDHpxeny7U:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=vjhGhvV88xY:3lDHpxeny7U:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/vjhGhvV88xY" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7129025</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/06/18/revisiting-memoization.aspx</feedburner:origLink></item><item><title>F# – Duck Typing and Structural Typing</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/zngdHuSOAyI/f-duck-typing-and-structural-typing.aspx</link><category>F#</category><category>Functional Programming</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Thu, 11 Jun 2009 09:57:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7117659</guid><description>&lt;p&gt;As you may have noticed on this blog lately that I’ve been focusing on Asynchronous Workflows.&amp;nbsp; In those adventures, I’ve been taking well known APIs such as Twitter, Bing, among others and seeing what I can do with them.&amp;nbsp; In this instance, when using LINQ to XML, I’ve run into a slight syntax problem.&lt;/p&gt;

&lt;h2&gt;The Problem with LINQ to XML&lt;/h2&gt;

&lt;p&gt; The syntax problem I had revolves around the retrieval of an XElement from the given XContainer.&amp;nbsp; The signature of this is as follows:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d6326816-0118-4d04-8971-1317a8369346" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: White;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;abstract&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XContainer : XNode {
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XElement Element(XName name);
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;But, what you may not realize is that there is an implicit operator which allows us to implicitly convert a string to an XName with little effort such as this:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:afacadad-f244-4daf-921d-a72f60ee5496" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;sealed&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XName : IEquatable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;XName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, ISerializable {
...
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;implicit&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;operator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XName(
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; expandedName);
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This allows us to use a string instead of an XName when specifying which element name we want, including namespace if desired, rather easily.&amp;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bc427b99-9d4c-459c-84f6-ac39684523c1" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; document &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XDocument.Parse(xml);
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; statuses &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; document.Root.Descendants()
    .Select(node &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; node.Element(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;text&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).Value);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Unfortunately for us, this behavior does not work in the current F# release.&amp;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1893402f-ad79-422a-8a81-e3b37bfa87cc" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; document.Root.Descendants() 
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Seq.map(fun node &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; node.Element(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;node&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).Value);;

stdin(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;5&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;65&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;): error FS0001: This expression has &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        string
but is here used with &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        XName&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;It does not recognize this implicit operator.&amp;nbsp; What options do we have around this?&amp;nbsp; One simple answer might be just a simple function that does the conversion for us, such as this:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:43cc0d63-2361-4778-8187-a6d0345af5f7" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ) : string &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XName.op_Implicit;;
val ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ) : string &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XName

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; xFoo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
val xFoo : XName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; foo&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What we’re now able to do is convert at will using the !! operator, or what I call the “Convert Dammit” operator.&amp;nbsp; But, there is another solution worth exploring that is a bit more generic through the use of duck typing.&lt;/p&gt;

&lt;h2&gt;Duck Typing to the Rescue?!?&lt;/h2&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;For those unfamiliar with the term &lt;a href="http://en.wikipedia.org/wiki/Duck_typing" mce_href="http://en.wikipedia.org/wiki/Duck_typing"&gt;duck typing&lt;/a&gt;, it is a style of dynamic typing in which the object’s current set of methods, properties, etc, determines its validity instead of its inheritance chain.&amp;nbsp; In F#, we have the ability to use this mechanism to use this to our advantage to create generic functions.&amp;nbsp; For example, let’s take the implicit operator example from above and use duck typing instead of the direct call to the XName class.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:80df072c-c3a4-4561-a74a-21ffa6b18225" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Longhand&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;inline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; implicit&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a,&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; arg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a) arg)
  
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Shorthand - let the compiler do the work&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;inline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; implicit arg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a) arg)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The two above statements are syntactically the same, although in the latter version, I don’t have to clutter up my declarations as much and instead let the compiler type inference magic do its work.&amp;nbsp; From here on out, I’ll use the shorthand unless the longhand doesn’t quite work out.&amp;nbsp; What you’ll notice is that I have a requirement for my ^a generic to have a restriction that it must have the implicit operator declared.&amp;nbsp; Then we can execute said operation by passing in our argument.&amp;nbsp; I can rewrite the convert dammit operator then to look something like this:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eb0c7fb0-6b43-412c-8e71-f798978e9d92" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Xml.Linq
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) : string &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; implicit

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; xFoo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Our new XName&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt; Now you can see here that I didn’t explicitly have to call anything on XName as I let the implicit operator do the work for me.&amp;nbsp; Best of all here is that it’s now statically typed to that any call to implicit must have that operator defined on it, else it will not compile.&lt;/p&gt;

&lt;p&gt;But, let’s not stop there, you can continue on all sorts of tangents.&amp;nbsp; What about functions which have more than one argument?&amp;nbsp; Let’s take the addition operator.&amp;nbsp; Could we use duck typing in our favor for this?&amp;nbsp; Although it’s not recommended, the answer is yes.&amp;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8324a32f-ebc3-45e7-8cc8-be3f4676c01b" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;inline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; add arg1 arg2 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; op_Addition : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a) (arg1, arg2))
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;inline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; add :
   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ) :  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a)

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; addedTime &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; add DateTime.Now (TimeSpan.FromSeconds &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;5000&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.);;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; addedTime : DateTime &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;6&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;11&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2009&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;00&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;32&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PM&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The reason why it’s not recommended is that a lot of primitive types are implicitly augmented with this operator such as int, float, etc, so this would not work for those plus not work on string as well since dynamic invocation is not allowed on this particular instance.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;This is useful though in other scenarios.&amp;nbsp; Take for example extracting a Name property from a given class.&amp;nbsp; Once again, using the methodologies from above, it’s rather simple:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:431e4364-c355-4318-bd77-656269681ac6" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;inline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getName arg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Name : string) arg)
  
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.IO
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; fName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getName (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; FileInfo(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;fsi.exe&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;))
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; dName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getName (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DirectoryInfo(Environment.CurrentDirectory))&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What about multiple restrictions?&amp;nbsp; That’s not too difficult either.&amp;nbsp; Let’s do the canonical Duck example with a little twist.&amp;nbsp; First, we will define the overall function which will extract the Flying and Walking state of a given class.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eb4b59c1-e236-4e37-acd4-c4c633f8b628" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;inline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; flyAndWalk arg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; flying &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Fly : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; string) arg)
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; walking &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Walk : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; string) arg)
  (flying, walking)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;And now we can define our classes which have the methods which have our interest such as the Fly and Walk methods.&amp;nbsp; We execute each of these, obtain the result and finally return the results of each as a tuple.&amp;nbsp; Now, let’s define two classes below of a Duck and an Eagle.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2992a6ed-d2ed-458a-a2c5-3887628b1356" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Duck() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Swim() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;paddling&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Fly() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;flapping&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Walk() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;waddling&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
 
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Eagle() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Fly() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;soaring&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Walk() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;creeping&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
 
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (eFly, eWalk) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; flyAndWalk (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Eagle())
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (dFly, dWalk) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; flyAndWalk (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Duck())&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Our final result will be rather predictable as you might imagine.&amp;nbsp; Just as well if we put a Penguin class which contains no fly, well, then this class won’t compile as shown below in F# interactive:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fae1e10d-8b89-42db-ba44-fd1f5d31b329" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Penguin() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Walk() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;waddle&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Swim() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;swimming&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Penguin &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Penguin
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Swim : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; string
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Walk : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; string
  end

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (pFly, pWalk) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; flyAndWalk(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Penguin());;

  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (pFly, pWalk) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; flyAndWalk(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Penguin());;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-------------------------------^^^^^^^^^^^^^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;

stdin(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;84&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;32&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;): error FS0001: The &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Penguin&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; does not support any operators na
med &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Fly&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;As you can see, it’s a rather powerful concept, when used in moderation, can help you solve some interesting problems such as implicit operators as well as other scenarios.&lt;/p&gt;

&lt;h2&gt;Structural Typing&lt;/h2&gt;

&lt;p&gt;There is a concept that is prevalent in languages such as OCaml, ML, and Scala for &lt;a href="http://en.wikipedia.org/wiki/Structural_typing" mce_href="http://en.wikipedia.org/wiki/Structural_typing"&gt;Structural Typing&lt;/a&gt; as opposed to Duck Typing.&amp;nbsp; This is a concept in which compatibility and equivalence is based upon the type’s structure and not the explicit declarations as I’ve had above.&amp;nbsp; Equivalence is then measured by the structural sameness between the two even though they may be nothing more than anonymous objects.&lt;/p&gt;

&lt;p&gt;In F#, we can accomplish this in a sense through the use of the record type quite easily.&amp;nbsp; For example, we can define a class that has two properties and then check for sameness between them.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5b8680d2-d9f3-4466-a962-2d9515556c22" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Foo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; { X : int; Y : int};;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Foo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  {X: int;
   Y: int;}

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; {X&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;32&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;Y&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;45&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;} &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; {X&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;32&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;Y&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;45&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : bool &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Normally, given that these are reference types and not standard structures, they shouldn’t ordinarily equate one to another automatically, but F# through the use of structural typing, does allow for this.&amp;nbsp; The same also applies for Discriminated Unions as well such as the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:31c6ebb8-6cd2-4d33-b245-bdda90186ef2" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DU &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; A &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; string &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; B &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; int;;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DU &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; A &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; string
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; B &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; int

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; B &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;42&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; B &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;42&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : bool &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Once again, since they are reference types, they shouldn’t equal each other, but with F#, they do, through the use of structural typing.&amp;nbsp; &lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Through the use of structural typing and duck typing, we realize how flexible the F# language really is.&amp;nbsp; This gives us the ability to be creative in how we solve our problems generically in specialized cases such as our LINQ to XML example from above.&amp;nbsp; Now that F# is a full citizen in the .NET space with Visual Studio 2010, it’s even more exciting about what we can do with the language.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7117659" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=zngdHuSOAyI:ouQvJVEcGkM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=zngdHuSOAyI:ouQvJVEcGkM:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=zngdHuSOAyI:ouQvJVEcGkM:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=zngdHuSOAyI:ouQvJVEcGkM:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=zngdHuSOAyI:ouQvJVEcGkM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=zngdHuSOAyI:ouQvJVEcGkM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/zngdHuSOAyI" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7117659</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/06/11/f-duck-typing-and-structural-typing.aspx</feedburner:origLink></item><item><title>Axum – Ping Pong with Ordered Interaction Points</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/HuKP7_DK2wc/axum-ping-pong-with-ordered-interaction-points.aspx</link><category>F#</category><category>Concurrency</category><category>Haskell</category><category>Axum</category><category>Erlang</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Wed, 03 Jun 2009 23:22:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7107807</guid><description>&lt;p&gt;&lt;b&gt;UPDATE: Removed code and explained that what I had was not intended behavior&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;After a slight diversion into F# mailbox processing, it’s time to come back to talk a little bit more about &lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx"&gt;Axum&lt;/a&gt;.&amp;nbsp; In &lt;a href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2009/05/15/axum-ping-pong-with-dataflow-networks.aspx" mce_href="http://weblogs.asp.net/blogs/matthew.podwysocki/archive/2009/05/15/axum-ping-pong-with-dataflow-networks.aspx"&gt;our last Axum post&lt;/a&gt;, we discussed using dataflow networks to clean up our canonical Ping-Pong example.&amp;nbsp; This time, we’ll try another approach using ordered interaction points.&lt;/p&gt;
&lt;h2&gt;A Little Housekeeping&lt;/h2&gt;
&lt;p&gt;Before we get started with our full demonstration, let’s take a brief look at some concepts that will come into play for this solution.&amp;nbsp; In our previous example using data flow networks, we a shared-nothing approach between these two agents.&amp;nbsp; This time, let’s introduce the concept of domains.&amp;nbsp; A domain in Axum allows for a group of agents to safely share data while shielding this state from the outside world.&amp;nbsp; Like normal classes, domains can have such things as fields and methods, but also, we can declare agents.&amp;nbsp; We’ll discuss how this comes into play later on in the post.&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:96c280ff-11b1-4ecf-9f15-e1bedcba3f80" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;domain&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; A {
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; fieldName;
    ...
}&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Another key concept to understand is reader and writer agents.&amp;nbsp; Typically, when dealing with shared state of some sort, we have a notion of a reader-writer lock.&amp;nbsp; This synchronization technique allows access to the shared state as one to many readers or a single writer to ensure consistency of that shared resource.&amp;nbsp; Agents in Axum also follow this notion where a reader agent is only allowed to read shared state and a writer agent can both read and write to domain state.&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4a4397cc-e03a-4451-bd2d-9508db75b514" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;domain&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; A {
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; fieldName;
    
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;reader&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; A1 { ... } &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Can read fieldName&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;writer&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; A2 { ... } &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Can read/write fieldName&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;One more concept that needs to be covered before our solution is the idea of hosts.&amp;nbsp; By using a host, we are able to associate an agent’s type with a particular instance in the domain.&amp;nbsp; For example when we look at the following code, what we’re doing is when someone asks for our A1 domain with an address of myA1, we attach it to the current domain and then return the associated C channel endpoint.&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0eb43e6e-275c-4ef7-870b-e0bdcaa95416" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Host&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;A1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;myA1&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;We can connect to this host then through a mechanism such as the following:&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:05c9ac41-79ea-462d-ba01-67c7fd03c618" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; chan &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; C(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;myA1&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Bringing this all together, let’s look at the Ping-Pong example through these eyes.&lt;/p&gt;
&lt;h2&gt;Getting to the Solution&lt;/h2&gt;
&lt;p&gt;In this solution, we’ll introduce the aspect of ordered interaction points.&amp;nbsp; This is an interaction point that acts as both a source and a target and the keyword being ordered which means that the order of messages is preserved.&amp;nbsp; To declare one, use the OrderedInteractionPoint&amp;lt;T&amp;gt; class such as the following:&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8ce2b884-a0f4-428f-8c3d-a02d360d0fcf" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ip &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OrderedInteractionPoint&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;();&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Now, let’s get to our solution.&amp;nbsp; The main application is no different, but where we will find differences is the interaction between the Ping and Pong agents.&amp;nbsp; As the code for that part hasn’t changed, let’s focus on the interaction between the ping and pong inside our domain.&amp;nbsp; First, let’s define the domain and we’ll call it the PingPongDomain (imaginative, I know).&amp;nbsp; In this domain, we need to set up the hosting for the Pong agent at the “Pong” address as well as the ordered interaction points for the ping and the pong.&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:18474123-bd32-4734-93d1-5f0dcbe3cc4a" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;domain&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPongDomain
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPongDomain()
    {
        Host&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Pong&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Pong&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
    }
    
    OrderedInteractionPoint&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Signal&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ping &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OrderedInteractionPoint&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Signal&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;();
    OrderedInteractionPoint&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Signal&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; pong &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OrderedInteractionPoint&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Signal&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;();&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Once we have this defined for our domain, our Ping and Pong agents inside this domain will have access to these points.&amp;nbsp; Since the Ping agent will ultimately both read and write messages, but using the immutable data type as the message type, we need not define it as a writer, but instead as a reader.&amp;nbsp; Let’s define that below.&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3ceec151-c19e-4126-ad14-6d1e344db4c7" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;reader&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ping : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPongStatus
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ping()
        {
            PrimaryChannel::HowMany &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;==&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Process;
        }
        
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal Process(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters)
        {
            &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Create pong&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; chan &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPong(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Pong&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);

           &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Send pings and receive pongs&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;           &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters; i&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;++&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
           {
               ping &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
               &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(pong);
               Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;ping received pong&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
           }

            chan::Done &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
        }
    }&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;You’ll notice that our code doesn’t look too much different than our previous example, except for connecting to the PingPongChannel with the “Pong” address that we defined in our domain above.&amp;nbsp; The other change is that we are sending a message on the ping ordered interaction point and receiving a message on the pong instance.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Moving along to our Pong agent, once again, the code does not deviate much.&amp;nbsp; But, as we are reading and writing our domain state by sending messages that are immutable, we could declare ourselves as a reader agent.&amp;nbsp; We will continuously receive messages either on the ping ordered interaction point or we will receive a message on the Done port on the primary channel.&amp;nbsp; The code will look something like the following.&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9cdf6e01-5880-4182-af37-93af359a6771" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;reader&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Pong : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPong
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Pong()
        {
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
            {
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PrimaryChannel::Done:
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ping:
                    Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;pong received ping&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
                    pong &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;break&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
            }
        }
    }&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Now that we have a solution, but, unfortunately due to a bug, this doesn’t work because the compiler cannot distinguish whether the state is immutable or not.&amp;nbsp; But, we can get it to run with an escape hatch.&lt;/p&gt;
&lt;h2&gt;Being Unsafe in a Safe Way&lt;/h2&gt;
&lt;p&gt;&lt;a href="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_12F5BE77.png" mce_href="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_12F5BE77.png"&gt;&lt;img src="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_1CDAAFE2.png" style="border-width: 0px; display: inline; margin-left: 0px; margin-right: 0px;" title="image" alt="image" mce_src="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_1CDAAFE2.png" align="left" border="0" height="162" width="244"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;As I stated before, Axum does have a notion of an escape hatch when it comes to code that allows you to execute code that might modify shared state through the use of the &lt;i&gt;unsafe&lt;/i&gt; keyword.&amp;nbsp; Most times it’s better to be safe than sorry in this regard, but in this example, it fits quite well.&amp;nbsp; You can think of this unsafe escape hatch much like the Haskell &lt;a href="http://haskell.org/ghc/docs/latest/html/libraries/base/System-IO-Unsafe.html" mce_href="http://haskell.org/ghc/docs/latest/html/libraries/base/System-IO-Unsafe.html"&gt;unsafePerformIO&lt;/a&gt; function.&amp;nbsp; This function in Haskell is the back door into the IO monad which allows an IO computation to be performed at any time.&amp;nbsp; And like our unsafe keyword, in order for this to be safe, we should be free of side effects and not dependent upon the environment.&lt;/p&gt;
&lt;p&gt;With this in mind, let’s rewrite the example using the unsafe construct.&amp;nbsp; First, let’s look at the Ping agent and how we might make that a little unsafe, but yet fully operational:&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5c26b87d-7ef9-42e8-b3f3-dcde519eca90" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ping : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPongStatus
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ping()
    {
        PrimaryChannel::HowMany &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;==&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Process;
    }
    
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal Process(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters)
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Create pong&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; chan &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DefaultCommunicationProvider.Connect&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;PingPong&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Pong&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;unsafe&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        {
            &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Send pings and receive pongs&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters; i&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;++&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
            {
                ping &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(pong);
                Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;ping received pong&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
            }
        }
        chan::Done &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;By simply wrapping our for loop in the unsafe block, we are able to send messages to our ping ordered interaction point and receive from our pong ordered interaction point where we weren’t allowed to in the above example.&amp;nbsp; The same applies for our Pong agent as well.&amp;nbsp; We can wrap the while loop receiving messages in an unsafe block so that we can receive from the ping ordered interaction point and then send a signal to the pong ordered interaction point.&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:58baac6c-27a6-461b-8c5e-9ccae784fed4" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Pong : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPong
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Pong()
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;unsafe&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
        {
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
            {
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PrimaryChannel::Done:
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ping:
                    Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;pong received ping&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
                    pong &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;break&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
            }
        }
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Running our sample, we get our expected results.&amp;nbsp; You can find the complete source code to this example &lt;a href="http://gist.github.com/122797" mce_href="http://gist.github.com/122797"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_6F893FDE.png" mce_href="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_6F893FDE.png"&gt;&lt;img src="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_53980AE6.png" style="border-width: 0px; display: inline;" title="image" alt="image" mce_src="http://weblogs.asp.net/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_53980AE6.png" border="0" height="252" width="495"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Unlike Erlang and other shared-nothing messaging approaches, Axum allows for shared domain state through tightly controlled access.&amp;nbsp; By doing so, we keep the safety aspect of messaging through such notions of a reader-writer lock.&amp;nbsp; Of course, there is an escape hatch for those who truly understand their application and the ramifications of what they are doing.&amp;nbsp; As you can see, Axum has a wide array of solving even the most simple and canonical examples of actor model concurrency.&amp;nbsp; There is still more yet to cover including asynchronous behavior and an exploration of the Auction example that I gave in F#.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx"&gt;Download it today&lt;/a&gt;, use it in anger and &lt;a href="http://social.msdn.microsoft.com/Forums/en/axum." mce_href="http://social.msdn.microsoft.com/Forums/en/axum."&gt;give the team feedback&lt;/a&gt; as to help shape what actor model concurrency might look like on the .NET platform.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7107807" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=HuKP7_DK2wc:K_j55yXI-HU:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=HuKP7_DK2wc:K_j55yXI-HU:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=HuKP7_DK2wc:K_j55yXI-HU:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=HuKP7_DK2wc:K_j55yXI-HU:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=HuKP7_DK2wc:K_j55yXI-HU:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=HuKP7_DK2wc:K_j55yXI-HU:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/HuKP7_DK2wc" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7107807</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/06/04/axum-ping-pong-with-ordered-interaction-points.aspx</feedburner:origLink></item><item><title>[ANN] DC ALT.NET – 6/10/2009 – Evan Light on BDD</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/ZoRXHzt0yik/ann-dc-alt-net-6-10-2009-evan-light-on-bdd.aspx</link><category>TDD/BDD</category><category>ALT.NET</category><category>User Groups</category><category>Ruby</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Tue, 02 Jun 2009 20:55:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7106736</guid><description>&lt;p&gt;The June installment of DC ALT.NET will be on June 10th, 2009 from 7-9PM.&amp;nbsp; Stay tuned to &lt;a href="http://tech.groups.yahoo.com/group/dcaltnet/" mce_href="http://tech.groups.yahoo.com/group/dcaltnet/"&gt;our mailing list&lt;/a&gt; to stay up to date with the happenings of the group.&amp;nbsp; We’ve moved up the schedule just a little bit to accommodate our presenter, who will be attending &lt;a href="http://rubynation.org/" mce_href="http://rubynation.org/"&gt;Ruby Nation&lt;/a&gt; later that week.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;We in the DC ALT.NET group are continuing our back to essentials series with our previous sessions on TDD, Refactoring and Pair Programming.&amp;nbsp; This month, we have &lt;a href="http://evan.tiggerpalace.com/" mce_href="http://evan.tiggerpalace.com/"&gt;Evan Light&lt;/a&gt; to facilitate a discussion on &lt;a href="http://behaviour-driven.org/" mce_href="http://behaviour-driven.org/"&gt;Behaviour (note the u) Driven Development&lt;/a&gt; (BDD).&amp;nbsp; Evan recently gave &lt;a href="http://scotlandonrails.com/schedule/27-march/tdd-its-about-more-than-just-testing/" mce_href="http://scotlandonrails.com/schedule/27-march/tdd-its-about-more-than-just-testing/"&gt;a talk at Scotland on Rails&lt;/a&gt; on the subject and we’ll discuss even further on the following items:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The Whys of BDD &lt;/li&gt;

  &lt;li&gt;How to BDD &lt;/li&gt;

  &lt;li&gt;BDD Antipatterns &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not only will we discuss the terminology and theory behind it, but also some of the frameworks that are available.&amp;nbsp; Note that Evan is a Rubyist at heart, so for many it’ll be a new and hopefully enlightening experience.&lt;/p&gt;

&lt;p&gt;Below are the details:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Date/Time:&lt;/strong&gt; 6/10/2009 – 7-9PM&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Location:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Motley Fool 
  &lt;br&gt;2000 Duke Street 

  &lt;br&gt;Alexandria, VA 22314&lt;/p&gt;
&lt;a href="http://www.eventbrite.com/event/358087048" mce_href="http://www.eventbrite.com/event/358087048"&gt;&lt;img src="http://www.eventbrite.com/static/images/button_ext/rsvp.gif" mce_src="http://www.eventbrite.com/static/images/button_ext/rsvp.gif" border="0"&gt;&lt;/a&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7106736" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=ZoRXHzt0yik:Y3YVHjtDxNc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=ZoRXHzt0yik:Y3YVHjtDxNc:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=ZoRXHzt0yik:Y3YVHjtDxNc:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=ZoRXHzt0yik:Y3YVHjtDxNc:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=ZoRXHzt0yik:Y3YVHjtDxNc:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=ZoRXHzt0yik:Y3YVHjtDxNc:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/ZoRXHzt0yik" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7106736</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/06/02/ann-dc-alt-net-6-10-2009-evan-light-on-bdd.aspx</feedburner:origLink></item><item><title>When Side Effects and Laziness Collide</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/mwRQEkyZz9A/when-side-effects-and-laziness-collide.aspx</link><category>F#</category><category>Concurrency</category><category>Functional Programming</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Tue, 02 Jun 2009 09:34:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7106197</guid><description>&lt;p&gt;While working on a side project recently, I came to rediscover some of the consequences of one of my earlier posts on &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/09/12/side-effects-and-functional-programming.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/09/12/side-effects-and-functional-programming.aspx"&gt;“Side Effects and Functional Programming”&lt;/a&gt;.&amp;nbsp; It’s important that we realize that when we are creating our programs to beware of lazy evaluation and side effects.&amp;nbsp; &lt;/p&gt;

&lt;h2&gt;Adding a Wrinkle&lt;/h2&gt;

&lt;p&gt;Consider the following example.&amp;nbsp; We need to be able to get the stream data from a given web request for a given URL.&amp;nbsp; From there, we will take the stream of data and read each line and return it as a collection.&amp;nbsp; Typically if we’re going to do this through F#, we’d use a sequence expression to lazily read through each line and return it as an IEnumerable&amp;lt;string&amp;gt;.&amp;nbsp; But, let’s add an extra wrinkle to this.&amp;nbsp; What if we want to do this in a non-thread blocking&amp;nbsp; asynchronous scenario?&amp;nbsp; This adds an extra wrinkle to this in terms of managing the lifetime of our objects.&amp;nbsp; We might write the code such as the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:df9ccf0b-5a65-4b47-a03c-4a2f1ab38ac8" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.IO
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Net

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getStreamData (uri:string) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  async { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; WebRequest.Create uri 
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.AsyncGetResponse()
          
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; seq { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; stream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response.GetResponseStream()
                       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; StreamReader(stream)
                       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; not reader.EndOfStream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;do&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader.ReadLine() } 
        }&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What we’re doing is putting a sequence expression inside of an asynchronous expression.&amp;nbsp; We must bind the response outside of the sequence expression due to the fact that we are doing this in an asynchronous fashion.&amp;nbsp; Once inside a sequence expression, a bind has a different meaning.&amp;nbsp; But, there’s a problem with this code.&amp;nbsp; Can you spot it?&amp;nbsp; &lt;/p&gt;

&lt;p&gt;The problem lies in the fact that we’re not explicitly disposing the response, which implements IDisposable.&amp;nbsp; My rule of thumb when flushing out my design is that if a class implements IDisposable and I ultimately control its lifetime, then I must wrap it in a using block.&amp;nbsp; If you read the documentation, you’ll realize that it’s not totally necessary when dealing with an HttpWebRequest to call Dispose, but it is when you’re dealing with an FtpWebRequest.&amp;nbsp; Because I don’t make any distinction in this code, calling Dispose is a must.&amp;nbsp; We might be tempted then to change the code to the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:63ef9ca0-eea0-4e04-917c-87c8e1938865" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.IO
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Net

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getStreamData (uri:string) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  async { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; WebRequest.Create uri 
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.AsyncGetResponse()
          
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; seq { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; stream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response.GetResponseStream()
                       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; StreamReader(stream)
                       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; not reader.EndOfStream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;do&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader.ReadLine() } 
        }&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Running through a quick example using F# interactive, we’d get the following result:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:33da5bce-55e7-4aa7-823d-48e68e34932c" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Async.RunSynchronously (getStreamData &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;http://www.bing.com&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : seq&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  Error: Cannot access a disposed object.
Object name: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;System.Net.HttpWebResponse&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The problem now becomes that our response binding now will be disposed by the time that I iterate the results of the sequence expression.&amp;nbsp; Remember that our evaluation of the sequence expression is lazy, so by the time I’m ready to iterate, the response has already had its Dispose called.&amp;nbsp; So, where does that leave us?&amp;nbsp; &lt;/p&gt;

&lt;p&gt;Well, there are several things that we could do here.&amp;nbsp; First, we could copy the response inside of our sequence expression and bind that to a using block.&amp;nbsp; This would ensure that by the time we’re finished, we would properly dispose the response.&amp;nbsp; That code might look like the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ff768c0b-11b5-49ee-92c7-c748fc4d7308" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.IO
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Net

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getStreamData (uri:string) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  async { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; WebRequest.Create uri 
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.AsyncGetResponse()
          
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; seq { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; = response&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; stream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;.GetResponseStream()&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; StreamReader(stream)
                       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; not reader.EndOfStream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;do&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader.ReadLine() } 
        }&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This approach will work, unlike the previous example.&amp;nbsp; By copying in this reference, we can ensure that the response will be disposed by the time we’re finished with our sequence expression.&amp;nbsp; Although this works, it has a certain code smell to me.&amp;nbsp; This code smell revolves around copying these references and there is a certain danger in doing so in terms of managing the lifetime.&amp;nbsp; Instead, could we try another approach, perhaps more eager?&lt;/p&gt;

&lt;h2&gt;From Lazy to Eager&lt;/h2&gt;

&lt;p&gt;Another approach we could take with this is to do away with the idea of lazy I/O in this solution altogether.&amp;nbsp; Instead, how about something as simple as a list comprehension instead of a sequence expression?&amp;nbsp; The code might then look like the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:72a0d212-fcf5-4e8d-9546-6cc9ff82ac43" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.IO
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Net

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getStreamData (uri:string) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  async { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; WebRequest.Create uri 
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.AsyncGetResponse()
          
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; [ &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; stream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response.GetResponseStream()
                   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; StreamReader(stream)
                   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; not reader.EndOfStream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;do&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader.ReadLine() ] 
        }&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Now what we have is that we have the use binding on our response and stream.&amp;nbsp; At the end of our function, we simply use a list comprehension instead of the sequence comprehension which in this case gives us the benefit of eager evaluation.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;When dealing with asynchronous expressions mixed with sequence expressions can cause problems due to object lifetimes.&amp;nbsp; As I’ve had in my earlier post, it’s best to avoid such scenarios if possible and instead, focus on an eager approach.&amp;nbsp; The savings you get here is you have to worry less about the resource lifetimes which may or may not be in scope by the time you’re ready to execute the sequence expression.&amp;nbsp; The approach using the list comprehension should be the preferred solution.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7106197" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mwRQEkyZz9A:1Xgxq4_CN7M:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mwRQEkyZz9A:1Xgxq4_CN7M:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=mwRQEkyZz9A:1Xgxq4_CN7M:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mwRQEkyZz9A:1Xgxq4_CN7M:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mwRQEkyZz9A:1Xgxq4_CN7M:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=mwRQEkyZz9A:1Xgxq4_CN7M:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/mwRQEkyZz9A" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7106197</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/06/02/when-side-effects-and-laziness-collide.aspx</feedburner:origLink></item><item><title>Actors in F# – The Bounded Buffer Problem</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/nVmxOX7jZ_s/actors-in-f-the-bounded-buffer-problem.aspx</link><category>F#</category><category>Concurrency</category><category>Functional Programming</category><category>Axum</category><category>Erlang</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Wed, 27 May 2009 23:58:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7099636</guid><description>&lt;p&gt;In the &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/05/20/f-actors-revisited.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/05/20/f-actors-revisited.aspx"&gt;previous post&lt;/a&gt;, I covered an example of an auction simulation using asynchronous message passing and a shared nothing approach using the MailboxProcessor class in F#.&amp;nbsp; The auction example was a great piece to demonstrate scalability by adding additional clients to create a sort of bidding war between them.&amp;nbsp; Once again, with this approach, we’ve eliminated the need for locks and other concurrency primitives.&lt;/p&gt;

&lt;p&gt;This time, let’s take another canonical example of a &lt;a href="http://en.wikipedia.org/wiki/Producer-consumer_problem" mce_href="http://en.wikipedia.org/wiki/Producer-consumer_problem"&gt;Bounded Buffer&lt;/a&gt; and look at some of the design patterns around this.&lt;/p&gt;

&lt;h2&gt;The Bounded Buffer&lt;/h2&gt;

&lt;p&gt;The goal of this post is to walk through an example of actor model concurrency of the &lt;a href="http://en.wikipedia.org/wiki/Producer-consumer_problem" mce_href="http://en.wikipedia.org/wiki/Producer-consumer_problem"&gt;canonical Bounded Buffer&lt;/a&gt; which is another &lt;a href="http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk/docs/examples/actors/boundedbuffer.scala" mce_href="http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk/docs/examples/actors/boundedbuffer.scala"&gt;example given in Scala&lt;/a&gt;.&amp;nbsp; The intent of this demo is to store and retrieve items in a buffer (rather simple actually).&amp;nbsp; Given this example, we’ll walk through how we might implement this using the constructs in F#.&amp;nbsp; One important aspect of this solution is to not post messages asynchronously as before, but instead, to post a message and await the reply.&lt;/p&gt;

&lt;p&gt;Without further ado, let’s get into the code.&amp;nbsp; As before, we have a few utility functions that will be quite handy for this journey.&amp;nbsp; Much like we defined the (&amp;lt;—) operator last time for posting messages, I’d like one for posting and waiting for a reply.&amp;nbsp; In addition, I need a way to accomplish currying for reasons you will see later.&amp;nbsp; One thing that has irked me on occasion is the confusion between partial application and currying, which &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/04/23/functional-c-composing-through-partial-application.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/04/23/functional-c-composing-through-partial-application.aspx"&gt;I’ve covered earlier&lt;/a&gt;.&amp;nbsp; Getting back to the issue at hand, let’s look at the code for that:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9c97a4a8-b225-4ab1-bba8-b15fb7561656" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Curry the arguments&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; curry f x y &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; f (x, y)

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Asynchronous post&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) (m:_ MailboxProcessor) msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; m.Post msg

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Post and reply operator&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) (m:_ MailboxProcessor) msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; m.PostAndReply(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; replyChannel &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg replyChannel)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;As you’ll notice, I put the two operators, the asynchronous post and the post and reply operators, the former not being needed for this post.&amp;nbsp; The PostAndReply method gives a way to post a message and wait for the reply.&amp;nbsp; A temporary reply channel is created and that forms part of our message.&amp;nbsp; This reply channel is an AsyncReplyChannel&amp;lt;T&amp;gt; which supports one function of Reply which we will use later.&amp;nbsp; This message is then sent back to the caller as the result.&lt;/p&gt;

&lt;p&gt;Next, we need to define the messages we will be processing as part of this bounded buffer.&amp;nbsp; Each of these messages define operations that our buffer supports, namely put, get and stop.&amp;nbsp; Let’s take a look at these in detail:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8ea5291e-2f79-42dd-89f7-b98fd2ea3048" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a BufferMessage = Put of &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; unit AsyncReplyChannel 
                      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Get &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a AsyncReplyChannel &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Stop &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; unit AsyncReplyChannel&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;As you will notice, each of these has an associated AsyncReplyChannel part to the defined message.&amp;nbsp; This is to allow me to reply to each of the callers in turn.&amp;nbsp; The Put and Stop both have reply channels that take no associated data, so we can create them as an AsyncReplyChannel&amp;lt;unit&amp;gt;.&amp;nbsp;&amp;nbsp; The Put message allows us to put a value into the buffer, the Get allows us to retrieve those values in turn, and the Stop allows us to stop the mailbox.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;Let’s move on to the heart of the matter, the actual bounded buffer.&amp;nbsp; This class takes in a buffer size and then we expose methods that allow us to put values in the buffer, get values from the buffer and stop the mailbox.&amp;nbsp; Below is how the code might look:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f5a1d413-8ab6-43a4-acac-db7d8b5775d7" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a BoundedBuffer(N:int) =&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     
    MailboxProcessor.Start(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buf:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a array = Array.zeroCreate N&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; out n =&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        async { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox.Receive()
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Put (x, replyChannel) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; N &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                    Array.set buf &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                    replyChannel.Reply ()
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop ((&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; + 1) % N) out (n + 1)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                    
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Get replyChannel &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; r &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Array.get buf out
                    replyChannel.Reply r
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; ((out + 1) % N) (n - 1)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                    
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Stop replyChannel &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; replyChannel.Reply(); &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; () }
      loop &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
      
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Put(x:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a) = buffer &amp;lt;-&amp;gt; curry Put x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Get() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Get
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Stop() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Stop&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Inside our BoundedBuffer class, we create the buffer which then creates an initialized array.&amp;nbsp; Because array contents are mutable, there is no sense in putting this as part of our processing loop.&amp;nbsp; Instead, we’ll focus on the input index, the output index and the number of items in the buffer as part of our processing loop.&amp;nbsp; When we receive the Put message when the number of items in the buffer is less than the buffer size, we set the value at the specified input index, return a reply back to the caller, and then loop with an increment to our index with a modulo of the buffer size as well as the number of items in the buffer.&amp;nbsp; In receiving a Get message when the number of items in the buffer is greater than zero, we get the item at the output index, send the reply back to the caller with the value, and then loop with an increment to the output index with a modulo as well as decrementing the number of items in our buffer.&amp;nbsp; Finally, should we receive a Stop, we simply reply back to the caller and return.&lt;/p&gt;

&lt;p&gt;We created three methods to wrap this functionality for outside consumption.&amp;nbsp; The Put method takes in the item to post to the buffer, and then we simply do a PostAndReply with our Put message and our item to post.&amp;nbsp; I used currying here because the Put message requires two parameters, the item to put as well as the reply channel.&amp;nbsp; In this case, my operator already provides that reply channel, so I only need to supply the item to put.&amp;nbsp; Both the Get and the Stop methods are fairly straight forward as they post their respective messages with their private reply channels.&lt;/p&gt;

&lt;p&gt;How does this work?&amp;nbsp; Let’s fire up F# interactive and take a look with an example of posting a few items to our buffer and then retrieving them.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:089376af-9469-4223-a506-f4085607616a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; int BoundedBuffer &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;42&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer : int BoundedBuffer

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer.Put &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;12&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ()
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer.Put &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;34&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ()
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer.Put &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;56&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ()
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer.Get();;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;12&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer.Get();;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;34&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer.Get();;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;56&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer.Stop();;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ()&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What I did was create a BoundedBuffer that handled integers with a buffer size of 42.&amp;nbsp; Then I posted three values, 12, 34 and 56.&amp;nbsp; After putting these values into our buffer, I then retrieved each in the order in which it was placed into our buffer.&amp;nbsp; Finally, I stopped the buffer.&amp;nbsp; The complete source code to this example can be found &lt;a href="http://gist.github.com/118484" mce_href="http://gist.github.com/118484"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Once again, we can create rather interesting solutions using this shared nothing asynchronous message passing approach in F#.&amp;nbsp; This solution involving the bounded buffer is no exception.&amp;nbsp; How might this solution look in Axum?&amp;nbsp; In due time, we will approach this as well as our Auction example from the previous post.&amp;nbsp; There are a lot of Axum items to cover especially in regards to asynchronous methods and ordered interaction points, so stay tuned.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7099636" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=nVmxOX7jZ_s:ni1S2REWFVQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=nVmxOX7jZ_s:ni1S2REWFVQ:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=nVmxOX7jZ_s:ni1S2REWFVQ:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=nVmxOX7jZ_s:ni1S2REWFVQ:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=nVmxOX7jZ_s:ni1S2REWFVQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=nVmxOX7jZ_s:ni1S2REWFVQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/nVmxOX7jZ_s" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7099636</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/05/28/actors-in-f-the-bounded-buffer-problem.aspx</feedburner:origLink></item><item><title>[ANN] DC ALT.NET – 5/27/2009 – Introduction to Pair Programming</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/kNfUt0t-R3s/ann-dc-alt-net-5-27-2009-introduction-to-pair-programming.aspx</link><category>TDD/BDD</category><category>ALT.NET</category><category>User Groups</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Tue, 19 May 2009 22:48:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7093295</guid><description>&lt;p&gt;The &lt;a href="http://tech.groups.yahoo.com/group/dcaltnet/" mce_href="http://tech.groups.yahoo.com/group/dcaltnet/"&gt;DC ALT.NET group&lt;/a&gt; is continuing the back to basics approach with this month covering an introduction to pair programming.&amp;nbsp; It’s part of our commitment to the community to encourage such practices as in our previous sessions on Test Driven Development and Refactoring.&amp;nbsp; This month is no exception in pair programming and why two heads are better than one. &lt;/p&gt;

&lt;p&gt;In this session, we are joined once again by &lt;a href="http://weblogs.asp.net/jcogley/default.aspx" mce_href="http://weblogs.asp.net/jcogley/default.aspx"&gt;Jonathan Cogley&lt;/a&gt;, CEO and founder of &lt;a href="http://www.thycotic.com/home.html" mce_href="http://www.thycotic.com/home.html"&gt;Thycotic&lt;/a&gt;, to not only facilitate a discussion on pair programming, but help lead a session in which we implement user stories in pairs.&amp;nbsp; We will lead this in a fishbowl style and then alternate in and out the pairs just to get an idea of how it is done.&amp;nbsp; Not only will we see how it’s done, but also the benefits that can be had.&lt;/p&gt;

&lt;p&gt;Our efforts in this back to basics series will continue next month covering &lt;a href="http://behaviour-driven.org/" mce_href="http://behaviour-driven.org/"&gt;Behavior Driven Development&lt;/a&gt; with &lt;a href="http://evan.tiggerpalace.com/" mce_href="http://evan.tiggerpalace.com/"&gt;Evan Light&lt;/a&gt;, so stay tuned.&lt;/p&gt;

&lt;p&gt;Below are the details:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Date/Time:&lt;/strong&gt; 

  &lt;br&gt;5/27/2009 – 7-9PM&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Location:&lt;/strong&gt; 

  &lt;br&gt;&lt;a href="http://fool.com" mce_href="http://fool.com"&gt;The Motley Fool&lt;/a&gt; 

  &lt;br&gt;2000 Duke Street 

  &lt;br&gt;Alexandria, VA 22314&lt;/p&gt;
&lt;a href="http://www.eventbrite.com/event/349430155" mce_href="http://www.eventbrite.com/event/349430155"&gt;&lt;img src="http://www.eventbrite.com/static/images/button_ext/rsvp.gif" mce_src="http://www.eventbrite.com/static/images/button_ext/rsvp.gif" border="0"&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp; &lt;p&gt;Hope to see you there!&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7093295" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=kNfUt0t-R3s:XhrDaVDrABM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=kNfUt0t-R3s:XhrDaVDrABM:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=kNfUt0t-R3s:XhrDaVDrABM:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=kNfUt0t-R3s:XhrDaVDrABM:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=kNfUt0t-R3s:XhrDaVDrABM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=kNfUt0t-R3s:XhrDaVDrABM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/kNfUt0t-R3s" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7093295</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/05/20/ann-dc-alt-net-5-27-2009-introduction-to-pair-programming.aspx</feedburner:origLink></item><item><title>F# Actors Revisited</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/VEpTXdK8KQQ/f-actors-revisited.aspx</link><category>F#</category><category>Concurrency</category><category>Axum</category><category>Erlang</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Tue, 19 May 2009 22:25:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7093288</guid><description>&lt;p&gt;&lt;strong&gt;UPDATE: Removed ref cells to use two recursive loops&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/05/15/axum-ping-pong-with-dataflow-networks.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/05/15/axum-ping-pong-with-dataflow-networks.aspx"&gt;previous post&lt;/a&gt;, I covered briefly about the actor model in F#.&amp;nbsp; This style of concurrency, using asynchronous message passing and a shared-nothing approach through the use of mailboxes is a pretty powerful mechanism for achieving scalability.&amp;nbsp; With a shared-nothing approach, we can remove the need for such concurrency primitives such as locks.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;Taking cues from Erlang, F# has the capabilities as well through the use of the MailboxProcessor class.&amp;nbsp; In this post, let’s walk through a canonical example and explain some of the design patterns around this.&lt;/p&gt;

&lt;h2&gt;Mailboxes, Etc&lt;/h2&gt;

&lt;p&gt;Before we begin, let’s make sure we have some of the basics down.&amp;nbsp; As I stated above, the approach here is that inter-agent communication is accomplished through a shared-nothing asynchronous message passing system.&amp;nbsp; Each of these agents have a mailbox, which is nothing more than a queue of messages sent by other processes.&amp;nbsp; These messages are then retrieved via either a scan or receive approach, depending if you wish to continue receiving messages.&amp;nbsp; To determine which message we received, we do basic pattern matching against our given message types and decide how we want to handle it.&amp;nbsp;&amp;nbsp; To continue processing, we then recurse the function again to keep sending and receiving.&amp;nbsp; We can send any number of pieces of data as part of these messages.&amp;nbsp; For our example, we’ll implement the Auction example that comes from Scala.&lt;/p&gt;

&lt;h2&gt;Going Once…&amp;nbsp; Going Twice…&amp;nbsp; Sold!&lt;/h2&gt;

&lt;p&gt;Our goal in this post is to walk through implementing the &lt;a href="http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk/docs/examples/actors/auction.scala" mce_href="http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk/docs/examples/actors/auction.scala"&gt;Auction example&lt;/a&gt; which is an example given in Scala.&amp;nbsp; The intent of this demo is to show an online auction service using the shared-nothing asynchronous message passing.&amp;nbsp; We’ll have two clients bidding against each other to see who wins the particular item.&amp;nbsp; Given that Scala has library support for actors, this is an attempt to see how well F# can handle this as well.&amp;nbsp; The first impressions of the Scala model is that it’s much more focused on classes and objects than the F# version will be.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;Without further ado, let’s start digging through the code.&amp;nbsp; First up, let’s get some utility functions out of the way to help us on our journey.&amp;nbsp; Instead of using the dot operator on some objects to send messages, I prefer the &amp;lt;—operator, as shown also in the Expert F# book.&amp;nbsp; Also, I need a way to deconstruct an option type so that I can deal with a bidder that may not exist yet.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a9a6d011-afec-4ce5-8126-d201e506028f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) (m:_ MailboxProcessor) x &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; m.Post x
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; unSome (Some x) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; x&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Both of these functions should be rather self explanatory.&amp;nbsp; The unSome takes in an option type with Some value and returns the value.&amp;nbsp; Let’s move on to the messages that we need to define.&amp;nbsp; I have comments beside each as to define what each message signifies.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:337570c3-d263-4b65-b730-4d6dc8eb70d6" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionMessage &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Offer &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionReply MailboxProcessor &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Make a bid&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Inquire &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionReply MailboxProcessor     &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Check the status&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;and&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionReply &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Status &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DateTime &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Asked sum and expiration&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; BestOffer                &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Ours is the best offer&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; BeatenOffer &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; int       &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Yours is beaten by another offer&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionConcluded &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;      &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Auction concluded&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;      AuctionReply MailboxProcessor &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionReply MailboxProcessor
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionFailed            &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Failed without any bids&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionOver              &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Bidding is closed&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;I decided to use a recursive type definition as to allow my AuctionMessage discriminated union to appear before my AuctionReply discriminated union, because both messages in AuctionMessage references a MailboxProcessor that handles the AuctionReply messages.&amp;nbsp; In F#, order does matter in which values are declared, so this is a quick way to get a readable output.&amp;nbsp; Next, we need to define some constants such as time to shutdown and our allowed bid increments:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:892e46dd-8e44-4337-91b8-b9aa8ba389dc" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; timeToShutdown &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3000&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; bidIncrement &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;10&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;It’s now time to define our auction agent.&amp;nbsp; This will handle the overall orchestration of how the auction is handled.&amp;nbsp; This includes handling both the Offer and the Inquire messages as well as what happens when we fail to get a message within a certain time period.&amp;nbsp; Let’s step through the code now and then explain down below:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e421756f-88e7-4fc2-a71e-90624b5af862" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; auctionAgent seller minBid closing &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionMessage MailboxProcessor(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop maxBid maxBidder &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
      async { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox.TryReceive((closing &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DateTime.Now).Milliseconds)
              &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Some ( Offer(bid, client) ) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; bid &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; maxBid &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; bidIncrement &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;then&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; maxBid &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; minBid &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;then&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; unSome maxBidder &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; BeatenOffer bid                  
                      client &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; BestOffer
                      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop bid (Some client)
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                      client &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; BeatenOffer maxBid
                      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop maxBid maxBidder
                
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Some ( Inquire client ) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                    client &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Status(maxBid, closing)
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop maxBid maxBidder
                
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; None &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; maxBid &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; minBid &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;then&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reply &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionConcluded(seller, unSome maxBidder)
                      unSome maxBidder &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reply
                      seller &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reply
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; seller &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionFailed
                    
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; = inbox.TryReceive timeToShutdown&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; with&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Some ( Offer (_, client) ) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
                        client &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionOver
                        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop maxBid maxBidder
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; None &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ()         
            }
    loop (minBid &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; bidIncrement) None)   &lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;It’s a bit of code to digest, but the gist of this is rather simple.&amp;nbsp; When creating an instance of this auction agent, we need a seller, a minimum bid and a closing time for the auction.&amp;nbsp; From this, we create our MailboxProcessor to handle AuctionMessage types.&amp;nbsp; Since we are continuously in a cycle of receiving messages and sending results, we have a recursive loop which carries our state for us.&amp;nbsp; This state, which we carry from recursive call to the next is our maximum bid and our maximum bidder at the time.&amp;nbsp; When we begin our call to this loop function, we start with the max bid of the minimum bid minus the bid increment and no maximum bidder.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;When we are receiving messages, it’s important to handle any potential timeouts.&amp;nbsp; In this case, we use this timeout as a mechanism for determining when our auction is over.&amp;nbsp; In order to do this, we must use the TryReceive function instead of the Receive.&amp;nbsp; Let’s look at the differences below:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d8419ddd-e2e2-49dc-b3ed-0f7d39659800" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Throws an exception if timeout is exceeded&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MailboxProcessor.Receive : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;?&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;timeout : int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Async&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;msg&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Returns None if timeout is exceeded&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MailboxProcessor.TryReceive : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;?&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;timeout : int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Async&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;msg option&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Since we need to know how to handle timeouts, it’s best to use the TryReceive approach.&amp;nbsp; If we receive an offer, we determine whether the bid is acceptable, meaning greater than our current maximum plus increment.&amp;nbsp; If it is, then we check whether the maximum bid is greater than the minimum, and if so, then send a message to the current maximum bidder with the BeatenOffer message.&amp;nbsp; If we receive an inquiry, we simply return a Status message back to the client with our current bid and how much time they have left.&amp;nbsp; Finally, if we don’t receive a message in the given timeframe, it’s time to wind down our auction.&amp;nbsp; If the maximum bid is greater than our minimum, our auction is a success and we let our maximum bidder and our seller know.&amp;nbsp; If, however, we don’t receive any bids, then our auction is considered a failure.&amp;nbsp; Finally, and additional offers received will be responded to with an AuctionOver message to indicate to the client that they are not the winner.&amp;nbsp; If this operation times out, we simply exit.&amp;nbsp; A bit of a mouthful, but fairly straight forward.&lt;/p&gt;

&lt;p&gt;Let’s move on to the client aspect.&amp;nbsp; First, let’s set up a few things necessary for our client, encapsulated in the Auction module.&amp;nbsp; We’ll set such parameters as our minimum bid, our closing date, our seller and our auction itself.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7f1a0be2-d37d-462c-b13a-ce97bffae2d0" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;module&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Auction &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; random &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Random()
  
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; minBid &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; closing &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DateTime.Now.AddMilliseconds &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;10000&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.
  
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; seller &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionReply MailboxProcessor(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
      async { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; _ &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox.Receive() 
              &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop()}
    loop())
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; auction &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; auctionAgent seller minBid closing&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;In order to create a seller for our auction, we need to stub out basic features of this agent.&amp;nbsp; In this case, we simply have a processing loop which receives a message, does nothing with it, and continues with the loop again.&amp;nbsp; As I have stated above, we need both our seller agent, and our auction agent, but what about our clients?&amp;nbsp; In our same module, we define them as the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bb0d6497-1b63-4fe8-bd43-bbc890353b81" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; client i increment top &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; name &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; sprintf &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Client %i&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; i
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; log msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;{0}: {1}&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, name, msg)
    
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; AuctionReply MailboxProcessor(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    
      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; startAuction() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        async { log &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;started&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                auction &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Inquire inbox
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; curMsg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox.Receive()
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; curMsg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Status(maxBid,_) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                    log &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; sprintf &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;status(%d)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; maxBid
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; maxBid }
      &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;and&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop current max &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        async { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; max &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; top &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;then&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; log &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;too high for me&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                    
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; current&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; =&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; current &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; max &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;then&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; current&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; = max + increment&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                    Thread.Sleep (&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; random.Next &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1000&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
                    auction &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Offer(current&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;, inbox)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                    current&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; current
                
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox.TryReceive timeToShutdown
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Some BestOffer &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
                    log &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; sprintf &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;bestOffer(%d)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; current&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop current&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; max&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Some (BeatenOffer maxBid) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                    log &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; sprintf &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;beatenOffer(%d)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; maxBid
                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop current&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; maxBid&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Some ( AuctionConcluded(seller, maxBidder) ) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                    log &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;auctionConcluded&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ()
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Some AuctionOver &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                    log &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;auctionOver&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ()
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; None &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; () }
      startAuction())&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What we’re doing here is having two mutually recursive loops.&amp;nbsp; The first loop, the startAuction loop will get the max bid for us and then go to the main loop where we do our main work.&amp;nbsp; From there, we’ll receive a message sent from the auction to the inbox and determine whether we’re the highest bidder, we’re the maximum or it’s time to call it a day.&amp;nbsp; Once again, fairly straight forward code.&lt;/p&gt;

&lt;p&gt;Lastly, it’s time to bring it all together.&amp;nbsp; Let’s set up the calling code and see the results:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6bfc795a-9688-4940-bd0e-1cd7c9203547" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Auction

seller.Start()
auction.Start()
(client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;20&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;200&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).Start()
(client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;300&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).Start()
Console.ReadLine() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ignore&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Executing this code gives us the following for our results:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fc4eacbc-fcde-4667-b5b6-91673e86b3bb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: started
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: started
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: status(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;90&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: status(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;90&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: bestOffer(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: bestOffer(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;110&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: beatenOffer(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;110&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: beatenOffer(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;120&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: bestOffer(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;120&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: auctionConcluded
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: auctionOver&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What this tells us is that client 2 starts first and bids first.&amp;nbsp; Client 2 then bids 100, only to be outdone by client 1 and goes back and forth until client 2 comes in with the best and final offer.&amp;nbsp; The auction is then concluded and client 1 is notified that the auction is over.&amp;nbsp; We can add additional client agents to the list and have just as much fun as before.&amp;nbsp; To give you an idea, if we add another agent with an increment of 30 and a max bid of 150, we get the following results:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:dfbbf263-7d55-4678-80d5-e7dad43397a8" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: started
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: started
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: started
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: status(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;90&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: status(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;90&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: status(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;90&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: bestOffer(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;110&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: beatenOffer(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;120&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: bestOffer(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;120&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: beatenOffer(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;120&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: auctionConcluded
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: auctionOver
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: bestOffer(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;130&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
Client &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;: auctionConcluded&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Now what we have here is that client 2 wins with an offer of 130 and the rest are notified of their loss.&amp;nbsp; You can find the complete code for this example &lt;a href="http://gist.github.com/113912" mce_href="http://gist.github.com/113912"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Solutions such as these using shared-nothing asynchronous message passing can create rather scalable architectures.&amp;nbsp; Utilizing the MailboxProcessor class, we’re able to implement actor model concurrency to tackle some of these problems with coordination of concurrent processes.&amp;nbsp; How might Axum handle this scenario?&amp;nbsp; Good question and I hope to have that answered shortly.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7093288" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=VEpTXdK8KQQ:RFLwWqR2GqI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=VEpTXdK8KQQ:RFLwWqR2GqI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=VEpTXdK8KQQ:RFLwWqR2GqI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=VEpTXdK8KQQ:RFLwWqR2GqI:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=VEpTXdK8KQQ:RFLwWqR2GqI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=VEpTXdK8KQQ:RFLwWqR2GqI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/VEpTXdK8KQQ" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7093288</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/05/20/f-actors-revisited.aspx</feedburner:origLink></item><item><title>Axum – Ping Pong with Dataflow Networks</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/yWURrJ5ngJk/axum-ping-pong-with-dataflow-networks.aspx</link><category>F#</category><category>Concurrency</category><category>Haskell</category><category>Axum</category><category>Erlang</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Fri, 15 May 2009 05:48:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7087877</guid><description>&lt;p&gt;&amp;nbsp;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/05/12/axum-introduction-and-ping-pong-example.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/05/12/axum-introduction-and-ping-pong-example.aspx"&gt;In the previous post&lt;/a&gt;, I gave the canonical Ping-Pong example in &lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx"&gt;Axum&lt;/a&gt; and how it compared with Axum.&amp;nbsp; I want to revisit this post because there are some areas in which we can rework it in addition to the other solutions we’ll visit.&amp;nbsp; Some parts were needlessly chatty and instead we’ll work in some other language features to help clean up our solution.&lt;/p&gt;

&lt;h2&gt;Actors in F#&lt;/h2&gt;

&lt;p&gt;But, before we begin, &lt;a href="http://blogs.msdn.com/concurrently_speaking/" mce_href="http://blogs.msdn.com/concurrently_speaking/"&gt;Nicklas Gustafsson&lt;/a&gt;, the architect behind Axum, was inspired by my post and made an &lt;a href="http://blogs.msdn.com/concurrently_speaking/archive/2009/05/12/actors-in-f.aspx" mce_href="http://blogs.msdn.com/concurrently_speaking/archive/2009/05/12/actors-in-f.aspx"&gt;F# version of the canonical Ping-Pong example&lt;/a&gt;.&amp;nbsp;&amp;nbsp;&amp;nbsp; F# supports a concept of mailbox processing as a first class citizen, much like other languages such as Scala and Erlang, although not as central to the core as it is in Erlang.&lt;/p&gt;

&lt;p&gt;To use these, simply use the MailboxProcessor class.&amp;nbsp; Below is an implementation as given by Nicklas and we can compare with my previous post with the Erlang implementation.&amp;nbsp; The Start function creates a new MailboxProcessor instance and starts it with the given inbox.&amp;nbsp; Much like the Erlang solution, this relies upon recursion as a technique for keeping the mailboxes alive.&amp;nbsp; In order for us to communicate from one mailbox to the other, we must as part of our message, include a reference to our mailbox of origin in order to communicate.&amp;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a6548259-f4c3-4ab5-91b6-1a48ca315188" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;#light&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Message &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Finished &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;of&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Message MailboxProcessor

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ping iters (outbox : Message MailboxProcessor) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    MailboxProcessor.Start(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; async { 
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; n &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; outbox.Post Finished
                   printfn &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;ping finished&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ()
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; _ &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; outbox.Post &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Msg(n, inbox)
                   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox.Receive()
                   printfn &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;ping received pong&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop(n&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)}
        loop iters)
            
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; pong() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    MailboxProcessor.Start(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop () &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; async { 
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inbox.Receive()
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; msg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Finished &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
                printfn &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;pong finished&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ()
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Msg(n, outbox) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
                printfn &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;pong received ping&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                outbox.Post &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Msg(n, inbox)
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; loop() }
                    
        loop())

ping &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; pong() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ignore
System.Console.ReadLine() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ignore&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;It’s a fairly straight forward and follows the mold of Erlang quite nicely.&amp;nbsp; Scala as well also follows this mold in &lt;a href="http://www.scala-lang.org/node/242" mce_href="http://www.scala-lang.org/node/242"&gt;their Ping-Pong example&lt;/a&gt;.&amp;nbsp; Using the &lt;a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/actor" mce_href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/actor"&gt;actor package&lt;/a&gt; in Haskell, we can also simulate the &lt;a href="http://gist.github.com/111482" mce_href="http://gist.github.com/111482"&gt;Ping-Pong example&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Of the criticisms mentioned in the post, I have no real issues with these such as the issues with no clear endpoints and how to distinguish the actors from one another.&amp;nbsp; The type safety of Axum does give us some advantages as to define an explicit contract between actors.&lt;/p&gt;

&lt;p&gt;Getting back to the issue at hand, what could we do to clean up our example in Axum?&lt;/p&gt;

&lt;h2&gt;Cleaning Up with Dataflow Networks&lt;/h2&gt;

&lt;p&gt;In our previous example, where we had the two agents communicating with each other, the Ping and the Pong, we pretty much driven by the data received from other agents.&amp;nbsp; Instead, we can turn our attention to using dataflow networks, which is to be driven only by the availability of data entering our network and the computations that are performed as we move through this network.&lt;/p&gt;

&lt;p&gt;Using Axum, we have several operators that can help us.&amp;nbsp; The Forward Operator ( ==&amp;gt; ) sends each message produced by the source to the target.&amp;nbsp; We also have the Forward Once ( –&amp;gt; ) operator which forwards a message from the source to the target, then disconnects after the first message.&amp;nbsp; Using these operators, we can build pretty rich pipelines for our data to flow, but just as well, we could use it to build an event-driven system where we respond to events.&lt;/p&gt;

&lt;p&gt;Let’s take a look at the rewrite of the main agent in which we create the endpoints for Ping and then send the message on the HowMany port.&amp;nbsp; After that, we then forward the received value of Done to the Done state.&amp;nbsp; Much cleaner than before.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ffecc17d-f0c9-46fc-98e7-0c886d0659dc" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Concurrency;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Microsoft.Axum;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Concurrency.Messaging;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Program : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Application
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Program()
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Create instance of ping and send msg&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; chan &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ping.CreateInNewDomain();
        chan::HowMany &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
        chan::Done &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;==&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Done;
    }
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Next up, let’s take a look at the Ping agent.&amp;nbsp; You’ll note no change in our PingPongStatus channel and the associated ports.&amp;nbsp; The interesting part is in the Ping constructor in which we forward the message from the HowMany port to the Process method which takes the number of iterations to perform.&amp;nbsp; In Process method, we create the Pong instance and communication channels, then we loop through our number of iterations to send the Signal to the Ping port, and receive the return Signal on the Pong port.&amp;nbsp; After the loop, we send a message to the Done to close the interaction and then return a Signal to end our Ping.&amp;nbsp; Below is the implementation of the code.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:20b73a32-a8cf-401f-811a-558ff64feccd" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPongStatus
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;input&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HowMany : Signal;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal Done;
    Start: { HowMany &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; End; }
}

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ping : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPongStatus
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ping()
    {
        PrimaryChannel::HowMany &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;==&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Process;
    }
   
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal Process(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters)
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Create pong&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; chan &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Pong.CreateInNewDomain();      
        
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Send pings and receive pongs&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters; i&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;++&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
        {
            chan::Ping &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(chan::Pong);
            Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;ping received pong&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
        }
        chan::Done &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
    }  
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;You’ll notice that we’re no longer interested in sending the number of iterations to the Pong.&amp;nbsp; It shouldn’t be needed and instead, we can send the Signal message on the Done port when we’re complete.&amp;nbsp; The Pong can listen in an infinite loop until it receives the message on the Done port.&lt;/p&gt;

&lt;p&gt;Lastly, let’s take a look at the Pong agent.&amp;nbsp; We’ll clean up the channel to rid ourselves of the HowMany port.&amp;nbsp; Instead, all we need are Signals to communicate at this point inside the infinite loop that I mentioned above.&amp;nbsp; Let’s take a look at the code required.&amp;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8cdd4273-10cf-43f3-9dd9-d261ddc6ae76" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPong
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;input&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal Done;
    
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;input&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal Ping;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal Pong;
}

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Pong : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPong
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Pong()
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;while&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        {
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PrimaryChannel::Done:
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PrimaryChannel::Ping: 
            Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;pong received ping&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
            PrimaryChannel::Pong &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;break&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;    
        }
    }
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;As you can see, inside the Pong constructor, we’re performing an infinite loop in which we call receive each time, then we can react to the messages coming on the specified ports, in this case, the Done and the Ping ports.&amp;nbsp; If we receive a message on the Ping port, we then send a Signal back on the Pong port and then break.&amp;nbsp; Else, if we receive a message on the Done port, then we exit the agent.&amp;nbsp; This approach is a bit more cleaner than our previous attempt at the Ping-Pong example.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;As I indicated before, are other angles I wish to tackle with this example, including using asynchronous methods, ordered interaction points and so on, which I will get into in subsequent posts.&amp;nbsp; With this little cleanup, I hope this shows some of the power of the language that we can indeed create concise, yet safe concurrent apps using message passing.&amp;nbsp; &lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx"&gt;Download it&lt;/a&gt;, use it in anger, and give the team &lt;a href="http://social.msdn.microsoft.com/Forums/en/axum." mce_href="http://social.msdn.microsoft.com/Forums/en/axum."&gt;feedback on the MSDN forum&lt;/a&gt;.&amp;nbsp; Once again, with your help we can help shape the future of an actor based concurrency model on .NET.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7087877" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=yWURrJ5ngJk:B6Q1pC0Fv8s:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=yWURrJ5ngJk:B6Q1pC0Fv8s:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=yWURrJ5ngJk:B6Q1pC0Fv8s:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=yWURrJ5ngJk:B6Q1pC0Fv8s:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=yWURrJ5ngJk:B6Q1pC0Fv8s:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=yWURrJ5ngJk:B6Q1pC0Fv8s:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/yWURrJ5ngJk" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7087877</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/05/15/axum-ping-pong-with-dataflow-networks.aspx</feedburner:origLink></item><item><title>Axum – Introduction and Ping Pong Example</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/8F-X_62NNvA/axum-introduction-and-ping-pong-example.aspx</link><category>Concurrency</category><category>Axum</category><category>Erlang</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Mon, 11 May 2009 22:37:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7083483</guid><description>&lt;p&gt;As it was &lt;a href="http://blogs.msdn.com/maestroteam/archive/2009/05/07/the-first-axum-bits-are-now-available.aspx" mce_href="http://blogs.msdn.com/maestroteam/archive/2009/05/07/the-first-axum-bits-are-now-available.aspx"&gt;announced last week&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx"&gt;Axum, a .NET Domain Specific Language&lt;/a&gt; around safe, scalable parallel programming through the actor model and message passing was released to the world as a CTP.&amp;nbsp; It was noted, that although this is an initial release, that this is still an incubation project which may or may not lead to a final product.&amp;nbsp; This post will serve as a kind of introduction to the language and some basic concepts.&lt;/p&gt;

&lt;h2&gt;Just A Brief Introduction&lt;/h2&gt;

&lt;p&gt;Before we get started, I wanted to make sure that we had a basic introduction to the language.&amp;nbsp; The question often comes up, why another .NET language?&amp;nbsp; Many people feel that with such others as C#, VB.NET, now F#, IronPython and IronRuby, that maybe we have enough languages already to solve most of our programming needs.&amp;nbsp; But there remains one important, yet untackled problem around making coordinated parallelization easy.&amp;nbsp; Sure, we have such things as the Parallel Extensions for .NET being baked into .NET 4.0, which gives us the Task Parallel Library, Parallel LINQ and so on, but none of these really address the intricate nature of coordinated parallelization.&lt;/p&gt;

&lt;p&gt;Typically, when we’re developing these parallel applications, we could partition the data as usual and then scatter them out as parallel tasks.&amp;nbsp; Some jobs were meant for solutions such as this, but others might require a bit more coordination.&amp;nbsp; The Axum language, built upon the &lt;a href="http://msdn.microsoft.com/en-us/library/bb648752.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb648752.aspx"&gt;Microsoft Concurrency and Coordination Runtime&lt;/a&gt; from the Microsoft Robotics Studio, allows for a pretty natural language around such concepts as messaging, side effect free programming, but just as well, free yourself of many concurrency related bugs.&amp;nbsp; Because isolation is built-in as a default, one of the major sources of concurrency related bugs is taken away.&amp;nbsp; Just as well, when we design our applications with messaging in mind, we are building a scalable solution where concurrency isn’t an afterthought.&lt;/p&gt;

&lt;p&gt;So, the final word is, why a language?&amp;nbsp; Built-in isolation, easy to understand syntax around messaging, easy asynchronous programming model and many other benefits we get by stepping outside of the C# bounds.&amp;nbsp; The real question then becomes, so what?&amp;nbsp; What can we do with it?&lt;/p&gt;

&lt;h2&gt;The Erlang Way&lt;/h2&gt;

&lt;p&gt;When learning language concepts, I find it easier to spend some time in the language which most strongly implements that concept.&amp;nbsp; This way, I learn the concepts best when they are first-class citizens in the space.&amp;nbsp; For learning functional programming, I don’t think it’s as wise to spend your time hacking on C# as to instead learn on a more functional language such as Haskell.&amp;nbsp; Often when people ask how best to learn FP concepts, often Haskell comes into the equation and has obviously improved my F# code as well as some of my C# as well.&amp;nbsp; The same applies to a language with actor model concurrency as a built-in concept, where I can learn from my time spent in Erlang as a good precursor to what I can do in Axum.&lt;/p&gt;

&lt;p&gt;One of the really elementary things to do in Erlang is a simple &lt;a href="http://erlang.org/doc/getting_started/conc_prog.html" mce_href="http://erlang.org/doc/getting_started/conc_prog.html"&gt;ping-pong example&lt;/a&gt;.&amp;nbsp; In this simple example, we create two processes, a pinger and a ponger, and send messages between them a number of times.&amp;nbsp; Such code might look like the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ba40d1c8-6aee-4910-ae2e-720e4a871eb7" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-module&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(tut15).

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-export&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;([start&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, ping&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, pong&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]).

ping(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, Pong_PID) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    Pong_PID &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; finished,
    io:format(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;ping finished~n&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, []);

ping(N, Pong_PID) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    Pong_PID &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; {ping, self()},
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        pong &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
            io:format(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Ping received pong~n&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, [])
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;end&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,
    ping(N &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, Pong_PID).

pong() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        finished &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
            io:format(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Pong finished~n&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, []);
        {ping, Ping_PID} &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
            io:format(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Pong received ping~n&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, []),
            Ping_PID &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; pong,
            pong()
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;end&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.

start() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    Pong_PID &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; spawn(tut15, pong, []),
    spawn(tut15, ping, [&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, Pong_PID]).&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;From this example, we have two functions, a ping and a pong.&amp;nbsp; Our ping has an arity with two arguments, the iteration number and the Process ID (PID) of the pong.&amp;nbsp; If we are at zero iterations left, then we sent the finished message back to the pong’s PID, else we send a ping to the pong, then receive our pong message and then recurse to decrement the iteration number.&amp;nbsp; Pretty simple, elegant and straight forward example.&amp;nbsp; Running this example, we might get the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c1b75ed7-1e3c-47fb-bbbb-e731bf8d4170" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Erlang R13B (erts&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;5.7&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) [smp:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;] [rq:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;] [async&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;threads:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]

Eshell V5.&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;7.1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  (abort with &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;G)
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; c(tut15).
{ok,tut15}
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; tut15:start().
Pong received ping
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0.40&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
Ping received pong
Pong received ping
Ping received pong
Pong received ping
Ping received pong
ping finished
Pong finished&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The &amp;lt;0.40.0&amp;gt; value you see above is the return value from the start() function which is nothing more than the PID of the ping process.&amp;nbsp; Now that we’ve seen this in action in Erlang, how could we do the same in Axum?&lt;/p&gt;

&lt;h2&gt;The Axum Solution&lt;/h2&gt;

&lt;p&gt;We have a couple of options when implementing this ping-pong example in Axum.&amp;nbsp; First, we have approach using channels, and another using an ordered interaction point.&amp;nbsp; Let’s walk through each of these solutions as a possible implementation.&amp;nbsp; The Axum Programming Guide is a great reference just in case you want to dive deeper into some of these topics.&lt;/p&gt;

&lt;p&gt;Before we get started here, let’s first take a look at a simple hello world example to get you familiar with the syntax.&amp;nbsp; Below is a quick example of getting the command line arguments, and then setting the channel to say we are finished.&amp;nbsp; Alternatively, we could set the error code &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9538c87d-3785-4275-92d5-6f85b6cb3624" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Microsoft.Axum;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Program : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Application
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Program()
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Wait for the command line arguments&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; cmdLine &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PrimaryChannel::CommandLine;
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; cmdArgs &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(cmdLine);  
        
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Tell the application we're done&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        PrimaryChannel::Done &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
    }
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The first piece you will notice is the lack of the word class, and instead the keyword agent.&amp;nbsp; The agent concept comes from the actor model of concurrency in which it is an autonomous entity that communicates with others via messages they receives from these other “actors” and can spawn off other “actors” as well.&amp;nbsp; These agents aren’t like normal classes in which you have fields that other classes can modify, nor have any direct method calls you can make.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;The second piece you’ll notice after the agent declaration is that our Program implements the Microsoft.Axum.Application Channel.&amp;nbsp; When we implement a channel, we are attached at the implementing end of that channel and we become the server of messages on that channel.&amp;nbsp; This channel has a public property called PrimaryChannel which gives access to the channel being implemented.&amp;nbsp; The double-colon operator gives us access to the channel’s port.&lt;/p&gt;

&lt;p&gt;Now to actually dive through the agent constructor, we wait for the command line arguments to be passed.&amp;nbsp; This receive is a blocking call, so we will wait until we receive the data from the CommandLine port, which in this case will be a string array.&amp;nbsp; We then tell the application we are done by sending a message through the &amp;lt;—operator to the Done port, which is done asynchronously, as opposed to the receive.&amp;nbsp; Now that we looked at a basic example, let’s move on to a channel based approach.&lt;/p&gt;

&lt;h2&gt;The Channels Example&lt;/h2&gt;

&lt;p&gt;The first example we’re going to look at is using channels and protocols.&amp;nbsp; In this example, we will use some of the above constructs with the addition of channel protocols.&amp;nbsp; First, let’s look at the main code which starts the program:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3c4099d3-8c55-48b5-97a5-85e88a5771bf" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Concurrency;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Collections.Generic;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Microsoft.Axum;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Concurrency.Messaging;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Program : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Application
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Program()
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Wait for our command args&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; cmdArgs &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(PrimaryChannel::CommandLine);
        
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Set the iterations to be some number&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
        
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Create instance of ping and send msg&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; chan &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ping.CreateInNewDomain();
        chan::HowMany &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters;
        
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Receive how many done&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(chan::Done);
        
        PrimaryChannel::Done &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
    }
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This code first waits for our command arguments, then we set the iterations to 3 for now, but we could set it as high as we wish, maybe higher than 5000 if we care.&amp;nbsp; Sure, that’s a high number, but our system should be good for it.&amp;nbsp; We then call Ping.CreateInNewDomain() which instantiates two ends of the PingPongStatus channel, creates an instance of Ping that implements the channel, then returns the using end of the channel.&amp;nbsp; After this, we send a message on the HowMany port with our number of iterations desired.&amp;nbsp; Then, we’ll block until we’ve received our message on the Done port to indicate that we are finished.&amp;nbsp; Finally, we wrap up with setting the overall Done on the PrimaryChannel to indicate our program completion.&amp;nbsp; Let’s move onto the Ping implementation next.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a9523749-e169-4113-aa77-cec2050be31a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPongStatus
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;input&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HowMany;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Done;
    
    Start: { HowMany, Done &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; End; }
}

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ping : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPongStatus
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ping()
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; How many are we supposed to do?&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(PrimaryChannel::HowMany);
        
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Create pong and send how many to do&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; chan &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Pong.CreateInNewDomain();      
        chan::HowMany &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters;
        
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Send pings and receive pongs&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters; i&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;++&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
        {
            chan::Ping &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(chan::Pong);
            Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Ping received Pong&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
        }
        
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; How many did Pong process?&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; pongIters &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(chan::Done);

        PrimaryChannel::Done &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
    }  
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;First, we created a PingPongStatus channel to get an input of how many to process, and then we have an output message which says we’re complete.&amp;nbsp; You’ll notice there’s something more to our channel which begins with Start:.&amp;nbsp; This is an implementation of a protocol, which allows us to specify the order in which messages are sent to which ports.&amp;nbsp; All cases start with the Start as we have above, and then we transition to the HowMany port, then from the Done, it signifies the end of our interaction on this channel.&lt;/p&gt;

&lt;p&gt;Now to analyze the Ping agent.&amp;nbsp; Quite simply, we receive the number of iterations message from our Program agent from above.&amp;nbsp; From there, we call Pong.CreateInNewDomain() which instantiates two ends of the PingPong channel, creates an instance of Pong that implements the channel, then returns the using end of the channel.&amp;nbsp; We send a message to the HowMany port to specify the number of iterations our ponger should do.&amp;nbsp; Going through each iteration, we send a message on the Ping port of a Signal, receive the message back on the Pong port.&amp;nbsp; Finally, we receive the number of iterations that our pong did and then set our Done flag to 0.&amp;nbsp; Now, let’s move onto our Pong implementation.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:47173b24-eb68-45b2-a747-2c08e42bc703" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPong
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;input&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HowMany;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Done;
    
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;input&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal Ping;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal Pong;
}

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;agent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Pong : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;channel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PingPong
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Pong()
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Get how many we're supposed to do&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(PrimaryChannel::HowMany);

        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;

        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Receive our ping and send back our pong&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (; i &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; iters; i&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;++&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
        {
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;receive&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(PrimaryChannel::Ping);
            Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Pong received Ping&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
            PrimaryChannel::Pong &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Signal.Value;    
        }
    
        PrimaryChannel::Done &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; i;
    }
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Notice that we have defined our PingPong channel to have ports to indicate how many to do, an output of how many we did, plus an input of a ping and an output of a pong.&amp;nbsp; Pretty straight forward and self explanatory.&amp;nbsp; Our Pong agent receives the number of iterations we’re supposed to do.&amp;nbsp; Then we loop through our iterations, receiving a ping and sending a pong.&amp;nbsp; After we finish the loop, we send a message on the Done port to indicate the number of iterations done.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;If we stick with our three iterations, the output from our application should look like the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:33a971dd-4bf0-4dbf-b1db-71d11a267536" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Pong received Ping
Ping Received Pong
Pong received Ping
Ping Received Pong
Pong received Ping
Ping Received Pong&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Pretty straight forward and easy example as we dip our toe into what we can do with Axum.&amp;nbsp; You can find the complete source code &lt;a href="http://gist.github.com/110275" mce_href="http://gist.github.com/110275"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;There are other angles I wish to tackle with this example, including using asynchronous methods, ordered interaction points and so on, which I will get into in subsequent posts.&amp;nbsp; Until then, I hope this brief foray into some of the capabilities of Axum will inspire you to give Axum a look.&amp;nbsp; &lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx"&gt;Download it&lt;/a&gt;, use it in anger, and give the team &lt;a href="http://social.msdn.microsoft.com/Forums/en/axum." mce_href="http://social.msdn.microsoft.com/Forums/en/axum."&gt;feedback on the MSDN forum&lt;/a&gt;.&amp;nbsp; With your help we can help shape the future of an actor based concurrency model on .NET.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7083483" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=8F-X_62NNvA:vW2zknOpouI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=8F-X_62NNvA:vW2zknOpouI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=8F-X_62NNvA:vW2zknOpouI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=8F-X_62NNvA:vW2zknOpouI:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=8F-X_62NNvA:vW2zknOpouI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=8F-X_62NNvA:vW2zknOpouI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/8F-X_62NNvA" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7083483</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/05/12/axum-introduction-and-ping-pong-example.aspx</feedburner:origLink></item><item><title>Axum Hits CTP</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/2bAr9SqL0_Y/axum-hits-ctp.aspx</link><category>Concurrency</category><category>Axum</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Fri, 08 May 2009 11:06:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7078771</guid><description>&lt;p&gt;As I relayed in &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/05/03/axum-reactive-framework-and-other-lang-net-items.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/05/03/axum-reactive-framework-and-other-lang-net-items.aspx"&gt;an earlier post&lt;/a&gt; about the soon availability of Axum, well, &lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx"&gt;today is the day&lt;/a&gt;.&amp;nbsp; I noted before that Microsoft has still not decided whether to release this as a real project, and needs feedback from users like yourself.&amp;nbsp; On the Axum site there is a great video on how to build your first Axum application.&amp;nbsp; Over time, I hope to add a few to the picture myself as actor model concurrency to me is highly interesting for many problems in today’s environment.&amp;nbsp; You can read more of the announcement at the &lt;a href="http://blogs.msdn.com/maestroteam/archive/2009/05/07/the-first-axum-bits-are-now-available.aspx" mce_href="http://blogs.msdn.com/maestroteam/archive/2009/05/07/the-first-axum-bits-are-now-available.aspx"&gt;Axum Team Blog&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Feedback Required&lt;/h2&gt;

&lt;p&gt;Josh Phillips, the PM of the Axum team has asked for pointed feedback for this release.&amp;nbsp; These include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Learning curve&lt;/strong&gt;:&amp;nbsp; How complex do you find it to do simple things in Axum?&amp;nbsp; Do you think the value it provides is worth the investment in learning and understanding the language?&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Form&lt;/strong&gt;: Do you think that Axum is better off as a language or would you be just as happy with a library-based solution?&amp;nbsp; &lt;br&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Interop&lt;/strong&gt;: How hard was it to consume objects that were created in other .NET languages?&amp;nbsp; Was it easy to place isolation guarantees on these objects?&amp;nbsp; If not, how would you change the process?

    &lt;br&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Async pattern&lt;/strong&gt;:&amp;nbsp; What do you think of asynchronous methods?&amp;nbsp; Do they make the APM pattern easier?

    &lt;br&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Legacy&lt;/strong&gt;:&amp;nbsp; What issues are there with porting legacy applications?&amp;nbsp; Any blockers or things we could do to make it easier?

    &lt;br&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Platform fit&lt;/strong&gt;: How does Axum fit in with the rest of .NET?&amp;nbsp; For graphical applications, did you face any challenges with the UI?&amp;nbsp; &lt;br&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Scalability&lt;/strong&gt;:&amp;nbsp; Were you able to easily write applications that scale from cores to the cluster to the cloud?&amp;nbsp; Is the unified model all it's cracked up to be?

    &lt;br&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Tools&lt;/strong&gt;:&amp;nbsp; What kind of tools (compile-time, run-time, post-run) would make you a more productive Axum programmer?&amp;nbsp; &lt;br&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Love/Hate&lt;/strong&gt;:&amp;nbsp; What's the one thing about Axum that frustrates you to no end?&amp;nbsp; What's the thing you love most about it?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx"&gt;Download it&lt;/a&gt;, use it in anger, and give the team feedback!&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7078771" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=2bAr9SqL0_Y:FhXaLLNbOH0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=2bAr9SqL0_Y:FhXaLLNbOH0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=2bAr9SqL0_Y:FhXaLLNbOH0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=2bAr9SqL0_Y:FhXaLLNbOH0:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=2bAr9SqL0_Y:FhXaLLNbOH0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=2bAr9SqL0_Y:FhXaLLNbOH0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/2bAr9SqL0_Y" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7078771</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/05/08/axum-hits-ctp.aspx</feedburner:origLink></item><item><title>Type Classes Are The Secret Sauce</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/yTgMdbBARpE/type-classes-are-the-secret-sauce.aspx</link><category>F#</category><category>Functional Programming</category><category>Haskell</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Thu, 07 May 2009 23:35:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7077451</guid><description>&lt;p&gt;I came across a recent post on &lt;a href="http://mixedsyntax.wordpress.com/2009/04/28/adding-ruby-and-c-operators-to-f/" mce_href="http://mixedsyntax.wordpress.com/2009/04/28/adding-ruby-and-c-operators-to-f/"&gt;adding Ruby and C# operators to F#&lt;/a&gt; that sparked a few thoughts in my head.&amp;nbsp; The post was good, but yet there were operators that already existed for some of the operations mentioned such as the defaultArg and ( @ ).&amp;nbsp; But what really got me was thinking about type classes again due to the fact that extension operators aren’t currently supported in the language whereas extension events, properties, methods and statics are.&amp;nbsp; I covered this in the past in regards to implementing an approximate equality check for my &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/01/20/functional-programming-unit-testing-using-type-classes.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/01/20/functional-programming-unit-testing-using-type-classes.aspx"&gt;Functional Programming Unit Testing series&lt;/a&gt;, but I want to dive further into that subject a little more in this post.&lt;/p&gt;

&lt;h2&gt;Just What Are Type Classes?&lt;/h2&gt;

&lt;p&gt;As you may note that I do cover a bit of Haskell on this blog, and I think for very good reason.&amp;nbsp; It’s a great language for incubating a lot of interesting ideas.&amp;nbsp; Some of these ideas have been distilled into other languages such as concepts as monads.&amp;nbsp; Type classes is another great feature of the language that I have come to know and love.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;When we talk about implementing functions, we have two main types of polymorphism, parametric and overloading.&amp;nbsp; Talking about parametric polymorphism is the typical programming with generics in which a function allows arguments of arbitrary related types of an unlimited nature.&amp;nbsp; One such example of this is the map function, or the Select method for those in the LINQ land which allows us to specify both the source and destination types which might be just about anything.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:29d30e59-39fb-429b-a93d-34f54d27e081" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Prelude&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; :t map
map :: (a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; b) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; [ a ] &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; [ b ]&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;On the other hand, we have ad-hoc polymorphism or overloading, which depending on its context, can assume more than one type.&amp;nbsp; If the allowable types are limited and must be known ahead of time, this makes is ad-hoc.&amp;nbsp; This polymorphism type is made possible in Haskell through the use of type classes.&amp;nbsp; For example, if we want to check for equality between two given algebraic types, this type must either derive the Eq type class or we create and instance of our own.&amp;nbsp; Let’s make this a little more clear by actually looking at one.&amp;nbsp; Instead of taking a look at the Eq or Equality check, let’s look at the Ord which contains the comparison operators.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:67787438-2c79-4991-abd0-d1a3b9b5cca2" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Prelude&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; :i Ord
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (Eq a) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ord a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;where&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  compare :: a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ordering
  (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) :: a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Bool
  (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) :: a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Bool
  (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) :: a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Bool
  (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) :: a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Bool
  max :: a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; a
  min :: a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; a
      &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;--&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Defined in GHC.Classes&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This type class lays out our basic comparison operators such as greater than, less than, min, max, etc.&amp;nbsp; But, it also has a restriction which states that any implementation of this type class must also have an associated implementation of the Eq type class which checks for equality.&amp;nbsp; You can think of this as generic restrictions.&amp;nbsp; The command we ran above, the info command also not only gave us information about the type class, but also some implementations such as the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1478ec34-9dc6-4e14-a4f6-8d7300d09388" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;instance&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (Ord a) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ord (Maybe a) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;--&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Defined in Data.Maybe&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;instance&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (Ord a, Ord b) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ord (Either a b)
  &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;--&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Defined in Data.Either&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;instance&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ord Integer &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;--&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Defined in efined in GHC.Base&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;instance&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ord Char &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;--&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Defined in GHC.Base&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;instance&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (Ord a) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ord [a] &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;--&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Defined in GHC.Base&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;instance&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ord Int &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;--&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Defined in GHC.Base&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;In order to provide that functionality such as ordering checking, we must create an instance of the Ord class and implement some members.&amp;nbsp; We needn’t implement all, just the ones we need for our given scenario.&amp;nbsp; To give you a sense of how this works, let’s take a look at the implementation of Ord for the Bool type.&amp;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:aa857cc4-26c0-4890-99c4-6d2ed027e2ec" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;instance&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Ord Bool &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;where&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    compare &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;False&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;True&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; LT
    compare &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;True&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;False&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GT
    compare _     _     &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; EQ&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Now we have the compare function available to use with the Bool type.&amp;nbsp; Pretty nice and easy.&amp;nbsp; Because of this, we can determine whether one is greater than the other such as the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:82277a80-9447-4fc4-9b15-8b4aa7b67c69" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Prelude&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; compare &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;True&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;False&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
GT&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Besides the Eq and the Ord I’ve already mentioned, there are others such as:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Show – Converting values to strings&lt;/li&gt;

  &lt;li&gt;Read – Converting strings to other values (opposite of Show)&lt;/li&gt;

  &lt;li&gt;Num – Numeric operations such as addition, subtraction, multiplication, etc&lt;/li&gt;

  &lt;li&gt;Enum – Enumeration operations&lt;/li&gt;

  &lt;li&gt;Fractional - Division&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the list goes on.&amp;nbsp; But what I 'like about this, is the opt-in nature of providing a given set of operations and implementing only what you need.&amp;nbsp; Also, it allows everything to be open so that I can create new instances of a type class for a given type as I see fit, even outside of the module in which it was created.&amp;nbsp; If you want to go deeper on the subject, &lt;a href="http://byorgey.wordpress.com/" mce_href="http://byorgey.wordpress.com/"&gt;Brent Yorgey&lt;/a&gt; has a great article called the &lt;a href="http://www.haskell.org/sitewiki/images/8/85/TMR-Issue13.pdf" mce_href="http://www.haskell.org/sitewiki/images/8/85/TMR-Issue13.pdf"&gt;Typeclassopedia&lt;/a&gt; in the Monad Reader Issue 13.&lt;/p&gt;

&lt;h2&gt;A Poor Man’s Type Class in F#&lt;/h2&gt;

&lt;p&gt;Ah, but alas we do not have type classes in F#.&amp;nbsp; It would be great if we did, but I’m sure it might take some changes to the CLR to make this happen such as allowing for higher-kinded polymorphism, which I’ve covered in the past.&amp;nbsp; As it is, creating generic functions such as the basic math ones can prove to be rather difficult even with the CLR generics.&amp;nbsp; What it essentially does is checks to see if that given operator exists for your given class among other things.&amp;nbsp; But, what if you don’t own that class, how could you possibly extend it this way, for example things like adding two Colors?&amp;nbsp; That’s the power of type classes.&lt;/p&gt;

&lt;p&gt;Although not supported natively, we can add that behavior to our classes.&amp;nbsp; Take for example checking for approximate equality for floating point arithmetic.&amp;nbsp; As we know, the precise nature of checking for equality of floating points isn’t the same as for integers, and instead must be checked if the difference is less than epsilon.&amp;nbsp; Let’s define an interface for what that might look like:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a4750405-154c-40be-bb7d-1179b7253c66" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Collections.Generic

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Define the type class and it's global associations table&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IApproxEq&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt; = &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;abstract&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; approxEqual    : &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a -&amp;gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; bool
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;abstract&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; approxNotEqual : &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a -&amp;gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; bool&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What we have here is the beginning of a type class as to define two functions, one for equality and one for inequality.&amp;nbsp; Next, we need to have some sort of repository for our instances that we declare so that we can dynamically look them up and perform the given operation.&amp;nbsp; That might look like the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6bf93804-daf8-4cf5-aafd-86c3eb34f2bb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;AbstractClass&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]
[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Sealed&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ApproxEqualAssociations &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; associations &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Dictionary&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Type, obj&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Add&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt;(approxEqual : IApproxEq&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; associations.Add(typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt;, approxEqual)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt;() = &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; associations.TryGetValue(typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt;) with &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, assoc &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; assoc :&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IApproxEq&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, _    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; failwithf &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Type %O does not have an implementation of IApproxEq&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This solution gives us a global singleton in a way of dealing with these operators.&amp;nbsp; As we shouldn’t be adding to them frequently, and instead reading, it shouldn’t be too much of an issue, but it is an issue nonetheless.&amp;nbsp; Now that we have this repository of them, let’s declare ones for both single precision floating point and double precision floating points.&amp;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cb763fcf-6737-4488-aba9-affc2ce42451" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SingleApproxEqual() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; epsilon &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0.001f&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IApproxEq&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;float32&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.approxEqual x x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; = abs(x - x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; epsilon
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.approxNotEqual x x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; = abs(x - x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; epsilon  
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DoubleApproxEqual() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; epsilon &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0.001&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IApproxEq&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;float&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.approxEqual x x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; = abs(x - x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; epsilon
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.approxNotEqual x x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; = abs(x - x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; epsilon      
ApproxEqualAssociations.Add(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SingleApproxEqual())       
ApproxEqualAssociations.Add(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DoubleApproxEqual())&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Once we’ve created these two implementations and added them to our associations table, it’s time to put some syntactic sugar on top to make them a real infix operator.&amp;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8f7250b6-a9a0-4562-9e50-31a77d69c4c9" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) x x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; = ApproxEqualAssociations.Get().approxEqual x x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) x x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt; = ApproxEqualAssociations.Get().approxNotEqual x x&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Now we have an extensible operator that can be added to at any given time.&amp;nbsp; Say if we wanted to say that some colors are considered approximately equal?&amp;nbsp; It wouldn’t be hard to implement something like that and add it to the associations table.&amp;nbsp; We can now test our behavior such as the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e7aab5a4-3120-442d-b880-cf651a92b88f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;4.000001f&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;4.0f&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Testing with float32&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : bool &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;5.1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/~&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;5.01&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Testing with float&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : bool &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The true power of this approach is that it opens up the function for future implementations that may want to enlist in our behavior.&amp;nbsp; Take this in contrast to the typical way we create operators in F# which lock us in to one type, although it can be circumvented, but it’s not well known and not well documented.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:dd85aef8-e109-48d6-9ab0-910a4c5b93d8" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) x y &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (x &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; y) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (x &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; y);;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ) : int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; int &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; int&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;As you can see, we’re locked into the integer implementation which isn’t extensible.&amp;nbsp; I noted above there are ways around this and if you look through the F# implementation, you will find some pretty interesting code for how they implemented the standard operators.&lt;/p&gt;

&lt;h2&gt;What Are the Limitations?&lt;/h2&gt;

&lt;p&gt;So like everything, there are limitations.&amp;nbsp; For example, there is no such thing as compile time safety, which doesn’t actually bother many people, especially those in the dynamically typed world.&amp;nbsp; Another issue is of course the maintenance of this global association table per type class can also be a little much as it might grow over time.&amp;nbsp; It’s not the ideal solution, but it’s a solution.&amp;nbsp; Do I think that F# should incorporate this?&amp;nbsp; Absolutely!&amp;nbsp; I really do like this language feature of type classes in Haskell and there are other lessons other languages can take from it.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7077451" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=yTgMdbBARpE:gTE0K3NM2-s:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=yTgMdbBARpE:gTE0K3NM2-s:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=yTgMdbBARpE:gTE0K3NM2-s:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=yTgMdbBARpE:gTE0K3NM2-s:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=yTgMdbBARpE:gTE0K3NM2-s:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=yTgMdbBARpE:gTE0K3NM2-s:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/yTgMdbBARpE" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7077451</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/05/08/type-classes-are-the-secret-sauce.aspx</feedburner:origLink></item></channel></rss>
