<?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:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Matthew Podwysocki</title><link>http://codebetter.com/blogs/matthew.podwysocki/default.aspx</link><description>Life of a Functional Programmer</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/MatthewPodwysocki" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item><title>Introduction to the Reactive Framework Part II</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/LTkhG4JcCgE/introduction-to-the-reactive-framework-part-ii.aspx</link><pubDate>Tue, 03 Nov 2009 05:28:37 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:409480</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>6</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=409480</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx#comments</comments><description>&lt;p&gt;In my &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx"&gt;previous post&lt;/a&gt;, I talked a little bit about the parts of the Reactive Framework that are coming to the .NET 4 BCL as well as their implementation in F# as part of F# First Class Events.&amp;nbsp; This time, let’s come back to the Reactive Framework itself from which the IObservable&amp;lt;T&amp;gt; and IObserver&amp;lt;T&amp;gt; originated.&amp;nbsp; As you may remember, you can play with the bits of the Reactive Framework from the &lt;a href="http://www.codeplex.com/Silverlight"&gt;Silverlight 3 Toolkit&lt;/a&gt;.&amp;nbsp; &lt;/p&gt; &lt;p&gt;In the &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/14/introducing-the-reactive-framework-part-i.aspx"&gt;first post in the series&lt;/a&gt;, I gave a basic introduction to the basic problems we have with asynchronous and reactive programming.&amp;nbsp; We covered some of the evolution of the .NET framework with asynchronous programming and where the Reactive Framework fits.&amp;nbsp; This time, I’ll pick up where we left off to talk about one of the important ideas behind the Reactive Framework, the duality of Enumerable and Observable.&lt;/p&gt; &lt;h2&gt;The Duality of Enumerable&amp;nbsp; &amp;amp; Observable&lt;/h2&gt; &lt;p&gt;During &lt;a href="http://channel9.msdn.com/shows/Going+Deep/E2E-Erik-Meijer-and-Wes-Dyer-Reactive-Framework-Rx-Under-the-Hood-1-of-2/"&gt;Erik’s appearances on Channel 9&lt;/a&gt;, an important, yet recurring theme was the &lt;a href="http://en.wikipedia.org/wiki/Duality_theory"&gt;mathematical duality&lt;/a&gt; of the Enumerable to the Observable.&amp;nbsp; What does that mean exactly though?&amp;nbsp; &lt;p&gt;We remember from our first post in the series where we talked about the pull (interactive) versus the push (reactive) model.&amp;nbsp; The pull model, represented by the iterator pattern of IEnumerable&amp;lt;T&amp;gt;/IEnumerator&amp;lt;T&amp;gt; states that we must explicitly call a method in order to get each item from our abstracted collection.&amp;nbsp; On the other hand, our push model, represented by the observable pattern of IObservable&amp;lt;T&amp;gt;/IObserver&amp;lt;T&amp;gt; states that we register an interest through a subscription and then items are subsequently handed to us from some abstracted collection.  &lt;p&gt;First, let’s look at the iterator pattern, which is a common interactive programming pattern that’s used quite frequently inside .NET.&amp;nbsp; This consists of two interfaces, the IEnumerable&amp;lt;T&amp;gt; and the IEnumerator&amp;lt;T&amp;gt;.&amp;nbsp; Listed below are some of the highlights of these two interfaces.&amp;nbsp; I cut down what they are to their essential bits. &lt;/p&gt; &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:78c27d02-2831-4956-a74e-25506fe8b415" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    IEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetEnumerator();
} 

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; : IDisposable
{
    T Current { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;get&lt;/span&gt;&lt;span style="color:#000000;"&gt;; }
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; MoveNext();
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;The IEnumerable&amp;lt;T&amp;gt; interface exposes a single method which returns an IEnumerator&amp;lt;T&amp;gt; to iterate some object.&amp;nbsp; The IEnumerator&amp;lt;T&amp;gt; gives us the ability to get the current item and determine whether there are more items to iterate.&amp;nbsp; &lt;p&gt;To think about basic asynchronous programming, we must consider a basic Gang of Four Pattern, the Observer Pattern.&amp;nbsp; This pattern describes where an object called an Observable, maintains a list of its dependent Observer classes and notifies them automatically of any state changes.&amp;nbsp; In Java, this should be rather familiar territory with the java.util.Observer class and the java.util.Observable interfaces.&amp;nbsp; In the Reactive Framework, this approach is no different, as we have an IObservable&amp;lt;T&amp;gt; interface which maintains a list of dependent IObserver&amp;lt;T&amp;gt; interfaces and notifies them automatically of any state changes.&amp;nbsp; Let’s look at the signatures below: &lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a22fc1cf-6ce7-41d9-b977-1c914f6dea63" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnCompleted();
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnError(Exception exception);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnNext(T value);
} 

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    IDisposable Subscribe(IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; observer);
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;What you may notice, and what you may have seen on some of Erik Meijer’s Channel 9 videos is that there is a duality between the Enumerable and Observable, given the signature of these interfaces.&amp;nbsp; To explain what I mean, let’s first look at the flow of the Enumerator: 
&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_02D24D1F.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_494F3D27.png" width="244" height="83" /&gt;&lt;/a&gt; 
&lt;p&gt;The GetEnumerator function takes no arguments and returns the IEnumerator&amp;lt;T&amp;gt; which has a no-argument function and returns the next item in the list through MoveNext and Current.&amp;nbsp; Now, let’s look at the inverse of it.&amp;nbsp; &lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_2FE739ED.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_21A8C0FD.png" width="244" height="123" /&gt;&lt;/a&gt; 
&lt;p&gt;The duality of the Enumerable solution is quite simple.&amp;nbsp; We have our observer on the left hand side which has an action function which takes a T argument and returns void.&amp;nbsp; The right hand side is the notification that is sent when something happens.&amp;nbsp; This is all managed by the Observable as it maintains a list of all observers so that it can notify them.&amp;nbsp; &lt;p&gt;To make this a little more concrete, let mash these two sets of interfaces together in order to dualize.&amp;nbsp; First, let’s start with the IEnumerator&amp;lt;T&amp;gt; interface.&amp;nbsp; &lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:31648a45-a756-4dad-876e-eb6beff8ac03" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; : IDisposable
{
    T Current { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;get&lt;/span&gt;&lt;span style="color:#000000;"&gt;; } &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; throws exception&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; MoveNext();
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What we’ll notice is that we have two items the Current property with a get accessor and the MoveNext method which takes nothing and returns a boolean that indicates if it can move next.&amp;nbsp; You’ll also notice that our Current property also could throw an exception.&amp;nbsp; If we’re in the Java world, we’d explicitly mark this interface with throws Exception, but as C# doesn’t have that, this comment will do.&amp;nbsp; Now, let’s look to dualize this interface.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d43100c1-d9db-443e-a5e1-bbac9d2b880f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDualEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; SetCurrent(T value &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Exception ex);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; MoveNext(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; canMove);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;At first glance, all we have to do is flip our Current property to have a setter.&amp;nbsp; Because the Current yielded a value or threw an exception, that’s the way we’ll model it for now.&amp;nbsp; Secondly, our MoveNext will now take a boolean to indicate whether we’re done.&amp;nbsp; Let’s refactor this a little bit more and break the SetCurrent into two distinct operations, one for yielding the next value, and one for throwing an exception.&amp;nbsp; &lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:644732de-0d0e-424d-8a79-f2e41f88677f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDualEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Yield(T value);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Throw(Exception ex);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; MoveNext(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; canMove);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now that we have that under control, let’s then move onto our MoveNext method.&amp;nbsp; As you’ll notice, we’ll continue to send in a true until we’re finished pushing values.&amp;nbsp; Instead of calling MoveNext repeatedly, let’s instead call this only once when we’re finished.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:67e3bcd0-71a6-4f8a-b2a7-38b4e6cfe217" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDualEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Yield(T value);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Throw(Exception ex);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Break();
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What we then end up with is pretty much the signature of our IObserver&amp;lt;T&amp;gt;.&amp;nbsp; By renaming our Yield to OnNext, our Throw to OnException and Break to OnCompleted, we’re left with our original IObserver&amp;lt;T&amp;gt; interface.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:64b72f33-4825-40a2-bbd3-9dc86c9d9445" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{   
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnNext(T value);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnError(Exception exception);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnCompleted();
} 
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What about the dual of IEnumerable&amp;lt;T&amp;gt;?&amp;nbsp; Let’s first look at what that interface is.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:de589912-3310-4d59-8687-6f512c6b547a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    IEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetEnumerator();
} &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This interface has a single method called GetEnumerator which returns an IEnumerator&amp;lt;T&amp;gt;.&amp;nbsp; How might we dualize this interface?&amp;nbsp; Well, we already know that the dual of the IEnumerator&amp;lt;T&amp;gt; is IObservable&amp;lt;T&amp;gt;, just as well, we need a way to attach interest in a given dualized Enumerator, so we’ll create a method called Attach.&amp;nbsp; Just as well, when we attach interest, we also need a way to detach our interest for a given observer.&amp;nbsp; That might look like the following.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b4d287df-8902-4317-bcd8-02d136ddbfa1" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDualEnumerable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Attach(IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; observer);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Detach(IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; observer);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we look at the Java interfaces for Observer and Observable, these are pretty close in signature.&amp;nbsp; There ultimately is a problem with this scenario in terms of composition.&amp;nbsp; At each level, it needs to remember which things maps to which so that you could undo any of the observers if need be.&amp;nbsp; Not doing so could lead to space leaks and is generally a bad idea.&amp;nbsp; How could we solve this then?&amp;nbsp; Ultimately, we need a way to track our attachment once we call the Attach through some form of a return value.&amp;nbsp; At this point, the tracking is nothing more than needing the ability to detach our given Observer.&amp;nbsp; If you’ll notice in our IEnumerator&amp;lt;T&amp;gt; interface, it inherits the IDisposable interface which gets one thinking.&amp;nbsp; Could we instead, change our Attach method so that when we call it, we return an IDisposable instead which gives us the ability to track our subscription and be able to clean up after we’re done?&amp;nbsp; That’s exactly the approach that the IObservable&amp;lt;T&amp;gt; takes in the final version of the interface.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e3753e03-8bdf-460e-a35d-0b9c26aef767" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    IDisposable Subscribe(IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; observer);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now that we’ve proven the duality, why does it matter?&amp;nbsp; Because of this duality, the laws that apply to LINQ to Objects now can apply to LINQ to Observables (or Events).&amp;nbsp; And in fact, we can see that through the Observable class which has many of our standard LINQ combinators such as: 
&lt;ul&gt;
&lt;li&gt;Aggregate 
&lt;li&gt;First/FirstOrDefault 
&lt;li&gt;GroupBy 
&lt;li&gt;Join 
&lt;li&gt;Last/LastOrDefault 
&lt;li&gt;Select 
&lt;li&gt;SelectMany 
&lt;li&gt;Single/SingleOrDefault 
&lt;li&gt;Skip/SkipWhile 
&lt;li&gt;Take/TakeWhile 
&lt;li&gt;Where 
&lt;li&gt;Zip&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Because of this duality, we can do such simple things as fire the ProgressChanged event until the RunWorkerCompleted event happens.&lt;/p&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5b6433cf-f1f2-4c82-9d79-72eb72d9fbed" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; worker &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; BackgroundWorker();

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; percentage &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; progress &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; worker.ProgressChangedEvent()
                           .Until(worker.RunWorkerCompletedEvent())
                 &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; progress.EventArgs.ProgressPercentage;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; subscription &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; percentage.Subscribe(
    p &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Console.WriteLine(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Percentage Complete: {0}&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, p));&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:38465fdf-0cb4-41e9-afd5-0468654547a4" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That’s only scratching the surface of the power of the Reactive Framework.&amp;nbsp; We’ll go in depth in the next part of the series on the combinators.&amp;nbsp; We could go into the fact that the Enumerable solution is really just the lazy list monad and the Observable solution is really just the continuation monad, but that’s for another time as well.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;As I’ve stated before, the LiveLabs Reactive Framework gives us the ability to harness reactive programming and treat events as the first class citizens they should have been using LINQ expressions and other standard LINQ combinators.&amp;nbsp; Still, there is much to cover in this series with the standard LINQ combinators, the monadic heritage of this solution and more.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=409480" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/LTkhG4JcCgE" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Event-based+Porgramming/default.aspx">Event-based Porgramming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx</feedburner:origLink></item><item><title>F# October 2009 CTP/Beta2 – F# + Rx Together At Last</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/1H2rMMOT2HU/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx</link><pubDate>Mon, 19 Oct 2009 22:37:24 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:387117</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>12</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=387117</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx#comments</comments><description>&lt;p&gt;Lately, I’ve been covering a lot of F# First Class Events as well as the Reactive Framework which has been leading up to a head as it were.&amp;#160; It &lt;a href="http://channel9.msdn.com/shows/Going+Deep/Kim-Hamilton-and-Wes-Dyer-Inside-NET-Rx-and-IObservableIObserver-in-the-BCL-VS-2010/"&gt;has been announced through Channel 9&lt;/a&gt; that .NET 4 will include the two major interfaces from the Reactive Framework, the &lt;a href="http://msdn.microsoft.com/en-us/library/dd990377%28VS.100%29.aspx"&gt;IObservable&amp;lt;T&amp;gt;&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/dd783449(VS.100).aspx"&gt;IObserver&amp;lt;T&amp;gt;&lt;/a&gt;.&amp;#160; Today, as &lt;a href="http://blogs.msdn.com/dsyme/archive/2009/10/19/visual-studio-2010-beta2-with-f-is-now-available-plus-matching-f-ctp-update-for-vs2008.aspx"&gt;Don Syme announces&lt;/a&gt; with the release of the &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=b55f0532-ac3c-4106-918c-5586a953a7da&amp;amp;displaylang=en"&gt;F# October 2009 CTP&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;F# for Visual Studio 2010 Beta 2&lt;/a&gt;, those two interfaces, the IObservable&amp;lt;T&amp;gt; and IObserver&amp;lt;T&amp;gt; have been integrated into F# First Class Events. &lt;/p&gt;  &lt;p&gt;What does that mean and what are the implications?&lt;/p&gt;  &lt;h2&gt;Mixing in Observables&lt;/h2&gt;  &lt;p&gt;In the previous version of F#, you may remember that we dealt primary with the IEvent interface and if you needed the ability to subscribe and unsubscribe, you would use the IDelegateEvent interface.&amp;#160; Let’s look at how this was implemented in the past.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:71bd7b00-ef31-4a9b-be74-1eddcc9e3223" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDelegateEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Delegate :&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Delegate &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;abstract&lt;/span&gt;&lt;span style="color:#000000;"&gt; AddHandler: handler:&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate -&amp;gt; unit&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;abstract&lt;/span&gt;&lt;span style="color:#000000;"&gt; RemoveHandler: handler:&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate -&amp;gt; unit &lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Args when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate : delegate&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Args,&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;unit&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
                                 &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate :&amp;gt; System.Delegate &amp;gt; =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;abstract&lt;/span&gt;&lt;span style="color:#000000;"&gt; Add: callback:(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Args -&amp;gt; unit) -&amp;gt; unit&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inherit&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDelegateEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you’ll note, we had the ability to add and remove delegate handlers using the AddHandler and RemoveHandler methods with our IDelegateEvent interface and our ability to add a handler via the Add method in the IEvent interface.&amp;#160; Both of these gave us some pretty rich options when dealing with first class events.&amp;#160; When the Reactive Framework interfaces were to be folded in to .NET 4.0, it was only a matter of time before the F# team looked on how to integrate the idea of Observables and F# First Class Events.&amp;#160; WIthin the Visual Studio 2010 Beta 2 download, you’ll find that F# does in fact reference the System.IObservable&amp;lt;T&amp;gt; and System.IObserver&amp;lt;T&amp;gt; interfaces.&amp;#160; If you are still using Visual Studio 2008, these interfaces have also been included as well so that you can take advantage of them.&amp;#160; Now if we are to look at how F# First Class Events work now, let’s go over the inheritance chain.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d7115a12-076a-42d1-b652-c3eb5dd5220c" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;T&amp;gt; =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;abstract&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnNext : value : &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;T -&amp;gt; unit&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;abstract&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnError : error : exn &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;unit&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;abstract&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnCompleted : &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;unit&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;unit&lt;/span&gt;&lt;span style="color:#000000;"&gt;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;T&amp;gt; =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;abstract&lt;/span&gt;&lt;span style="color:#000000;"&gt; Subscribe : observer : IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;T&amp;gt; -&amp;gt; System.IDisposable&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDelegateEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Delegate :&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Delegate &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;abstract&lt;/span&gt;&lt;span style="color:#000000;"&gt; AddHandler: handler:&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate -&amp;gt; unit&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;abstract&lt;/span&gt;&lt;span style="color:#000000;"&gt; RemoveHandler: handler:&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate -&amp;gt; unit &lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Args when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate : delegate&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Args,&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;unit&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
                                 &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate :&amp;gt; System.Delegate &amp;gt; =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inherit&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDelegateEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate&amp;gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inherit&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Args&amp;gt;  &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What you’ll now notice is that we have in code, defined our IObserver&amp;lt;T&amp;gt; and IObservable&amp;lt;T&amp;gt; which either come from the .NET 4.0 base class library or in F# itself for Visual Studio 2008, and then we have our IEvent&amp;lt;TDel,T&amp;gt; inheriting both the IDelegateEvent&amp;lt;TDel&amp;gt; and the IObservable&amp;lt;T&amp;gt; interfaces.&amp;#160; This means that when we’re dealing with First Class Events in F#, we can go back and forth from using the IEvent&amp;lt;TDel,T&amp;gt; and IObservable&amp;lt;T&amp;gt; interfaces, based upon our needs.&amp;#160; So, what does that buy us?&lt;/p&gt;

&lt;h2&gt;Exploring Observables&lt;/h2&gt;

&lt;p&gt;Much like the Event module in F# defines combinators for F# First Class Events, the Observable module defines the standard combinators for using the IObservable&amp;lt;T&amp;gt; interface.&amp;#160; Much like the Event module, the Observable module includes add, filter, map, partition, merge, choose, and scan.&amp;#160; Let’s go over these briefly about what each one is about.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;add&lt;/strong&gt;

    &lt;br /&gt;Create an observer which permanently subscribes to the given observable and which calls the given function for each observation.

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;subscribe&lt;/strong&gt;

    &lt;br /&gt;Create an observer which subscribes to the given observable and which calls the given function for each observation.

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;map
      &lt;br /&gt;&lt;/strong&gt;Return an observable which transforms the observations of the source by the given function. The transformation function is executed once for each subscribed observer. The returned object also propagates error observations arising from the source and completes when the source completes. 

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;filter&lt;/strong&gt;

    &lt;br /&gt;Return an observable which filters the observations of the source by the given function. The observable will see only those observations for which the predicate returns true. The predicate is executed once for each subscribed observer. The returned object also propagates error observations arising from the source and completes when the source completes.

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;scan&lt;/strong&gt;

    &lt;br /&gt;Return an observables which, for each observer, allocates an item of state and applies the given accumulating function to successive values arising from the input. The returned object will trigger observations for each computed state value, excluding the initial value. The returned object propagates all errors arising from the source and completes when the source completes.

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;choose
      &lt;br /&gt;&lt;/strong&gt;Return an observable which chooses a projection of observations from the source using the given function. The returned object will trigger observations x for which the splitter returns Some x. The returned object also propagates all errors arising from the source and completes when the source completes.

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;partition&lt;/strong&gt;

    &lt;br /&gt;Return two observables which partition the observations of the source by the given function. The first will trigger observations for those values for which the predicate returns true. The second will trigger observations for those values where the predicate returns false. The predicate is&amp;#160; executed once for each subscribed observer. Both also propagate all error observations arising from the source and each completes when the source completes. 

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;split&lt;/strong&gt;

    &lt;br /&gt;Return two observables which split the observations of the source by the given function. The first will trigger observations x for which the splitter returns Choice1Of2 x. The second will trigger observations y for which the splitter returns Choice2Of2 y.&amp;#160; The splitter is executed once for each subscribed observer. Both also propagate error observations arising from the source and each completes when the source completes. 

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;merge&lt;/strong&gt;&lt;/li&gt;

  &lt;p&gt;Return an observable for the merged observations from the sources. The returned object propagates success and error values arising from either source and completes when both the sources have completed.&lt;/p&gt;
&lt;/ul&gt;

&lt;p&gt;You’ll notice one difference from the Event module to the Observable module is the inclusion of the subscribe function.&amp;#160; This allows us to subscribe to an observable, which in turn will return an IDisposable.&amp;#160; This IDisposable, as I’ve covered before, allows us to nicely tell the system when we’re no longer interested in receiving notifications instead of having to tell the observer when we want to attach and detach.&amp;#160; We’ll cover more of this in a future post on the Introduction to the Reactive Framework series.&lt;/p&gt;

&lt;p&gt;What does this change buy us exactly?&amp;#160; Well, now we can treat observables much the same as we would for F# first class events.&amp;#160; Now we can take an example from a previous post and show how we can create two observables based upon whether the X and Y coordinate of the mouse is in a certain location as we drag it across our form.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:44566c3d-2ea5-4352-862e-9ed4004dd415" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Create form&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; form &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Form(Visible&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;, TopMost&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Text&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Event Sample&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Create under and over for X and Y coordinates&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; (overEvent, underEvent) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  form.MouseDown
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.merge form.MouseMove
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.filter (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; args &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.Button &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; MouseButtons.Left)
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.map (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; args &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; (args.X, args.Y))
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.partition (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; (x, y) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; x &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;100&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color:#000000;"&gt; y &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;100&lt;/span&gt;&lt;span style="color:#000000;"&gt;)

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Subscribe to each&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; overSubscription &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  overEvent 
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.subscribe (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; (x, y) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Over (%d, %d)&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; x y)
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; underSubscription &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  underEvent 
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.subscribe (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; (x, y) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Under (%d, %d)&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; x y)
  
&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Much later, clean up&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;overSubscription.Dispose()
underSubscription.Dispose()&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see from this code, we’re treating our Observables no differently than we would our F# First Class Events.&amp;#160; With the inheritance chain of the IEvent from the IObservable, we get seamless integration between the two.&amp;#160; Unlike C#, we don’t need to create extension methods to turn our events into observables and instead get that for free.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;What you will notice is that we lack many of the combinators that the Reactive Framework has built in such as Until/WaitUntil, Take/TakeWhile, Skip/SkipWhile among others for which we’ll cover in the Introduction to the Reactive Framework series.&amp;#160; Many of these aren’t hard to implement and we’ll get the added benefit of exception management that the Observables give us.&lt;/p&gt;

&lt;p&gt;It’s pretty interesting to see how F# has evolved from release to release.&amp;#160; This release for &lt;a href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;Visual Studio 2010 Beta 2&lt;/a&gt; and the &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=b55f0532-ac3c-4106-918c-5586a953a7da&amp;amp;displaylang=en"&gt;F# October 2009 CTP&lt;/a&gt; have some nice changes as noted in &lt;a href="http://blogs.msdn.com/dsyme/archive/2009/10/19/release-notes-for-the-f-october-2009-release.aspx"&gt;the release notes&lt;/a&gt;.&amp;#160; The integration of the IObservable/IObserver is not to be overlooked as one of them.&amp;#160; Download them today and give the team feedback!&amp;#160; Now back to our Observables series.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=387117" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/1H2rMMOT2HU" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Concurrency/default.aspx">Concurrency</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Event-based+Porgramming/default.aspx">Event-based Porgramming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx</feedburner:origLink></item><item><title>[ANN] DC ALT.NET 10/22/2009 – MongoDB with David James</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/lThZMQA0a8U/ann-dc-alt-net-10-22-2009-mongodb-with-david-james.aspx</link><pubDate>Fri, 16 Oct 2009 03:38:45 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:381540</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=381540</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/15/ann-dc-alt-net-10-22-2009-mongodb-with-david-james.aspx#comments</comments><description>&lt;p&gt;This month DC ALT.NET will once again move into the not often covered subjects in the .NET world, in covering &lt;a href="http://www.mongodb.org/"&gt;MongoDB&lt;/a&gt; with &lt;a href="http://twitter.com/djsunlight"&gt;David James&lt;/a&gt;.&amp;#160; Stay tuned to the &lt;a href="http://tech.groups.yahoo.com/group/dcaltnet/"&gt;mailing list&lt;/a&gt; for up to date details.&amp;#160; There has been a bit of discussion around non relational databases lumped under the name of NoSQL, especially given the upcoming &lt;a href="https://nosqleast.com/2009/"&gt;NoSQLEast conference&lt;/a&gt;.&amp;#160; This meeting, we&amp;#39;ll dive into MongoDB, one of the NoSQL solutions.&lt;/p&gt;  &lt;p&gt;MongoDB is an open source document-oriented database written in the C++ programming language.&amp;#160; Like other document-oriented databases, MongoDB is designed for problems without heavy transactional requirements that aren&amp;#39;t easily solved by traditional relational databases, including problems which require the database to span many servers.&amp;#160; K. Scott Allen has a great writeup on how to use MongoDB from C# that you can find &lt;a href="http://odetocode.com/Blogs/scott/archive/2009/10/13/experimenting-with-mongodb-from-c.aspx"&gt;here&lt;/a&gt; and is a great read.&amp;#160; And yes, there is even &lt;a href="http://www.claassen.net/geek/blog/2009/09/linq2mongodb-building-linq-provider-for.html"&gt;LINQ support as well&lt;/a&gt;. Come join us and find out what NoSQL alternatives can do for you.&lt;/p&gt;  &lt;p&gt;Also, we&amp;#39;ll have a hacking session for those interested, so be sure to install &lt;a href="http://www.mongodb.org/display/DOCS/Downloads"&gt;MongoDB&lt;/a&gt; beforehand.&amp;#160; The presentation will be mainly in Ruby and JavaScript, but there are C# options available as well.&lt;/p&gt;  &lt;h2&gt;About David James&lt;/h2&gt;  &lt;p&gt;David James is a Web Developer for Sunlight Labs. Prior to joining Sunlight, David founded CommunityGoals, a socially minded startup, and Collective Insight, a technology consultancy. He has worn the hats of entrepreneur, analyst, software developer, and consultant while working in fields including software, energy, and hardware engineering. David has a Master of Public Affairs degree from the LBJ School of Public Affairs, a liberal arts BA degree and an electrical and computer engineering BS degree from The University of Texas at Austin. He enjoys building tools that advance causes such as open government, community development, and environmental and energy policy. David also enjoys playing the piano and cycling and is sometimes known for coaxing friends into sprinting for city limit signs.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Date/Time:&lt;/strong&gt; 10/22/2009 – 7-9PM&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Location:      &lt;br /&gt;&lt;/strong&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/464904542?ref=ebtn" target="_blank"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" border="0" src="http://www.eventbrite.com/registerbutton?eid=464904542" alt="" /&gt;&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=381540" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/lThZMQA0a8U" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/ALT.NET/default.aspx">ALT.NET</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/User+Groups/default.aspx">User Groups</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/NOSQL/default.aspx">NOSQL</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/15/ann-dc-alt-net-10-22-2009-mongodb-with-david-james.aspx</feedburner:origLink></item><item><title>F# First Class Events – Composing Events Until Others</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/r6jvOpi1GTM/f-first-class-events-composing-events-until-others.aspx</link><pubDate>Thu, 15 Oct 2009 05:43:52 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:380545</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>12</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=380545</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/15/f-first-class-events-composing-events-until-others.aspx#comments</comments><description>&lt;p&gt;After &lt;a href="http://weblogs.asp.net/podwysocki/archive/2009/10/14/introducing-the-reactive-framework-part-i.aspx#7229940"&gt;a comment on my last post&lt;/a&gt; in regards to First Class Events in F#, I thought I’d revisit them briefly before going back to the &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/14/introducing-the-reactive-framework-part-i.aspx"&gt;Reactive Framework series&lt;/a&gt;.&amp;#160; In particular, this comment was in regards to implementing the until combinator using F# First Class Events.&amp;#160; As part of my hacking during this series, I came across a rather identical solution to the one that was posted, so I thought it’d be worth exploring.&lt;/p&gt;  &lt;h2&gt;Implementing Until&lt;/h2&gt;  &lt;p&gt;The idea behind the until event combinator is to that we will return values from our event until any notification from the other event occurs.&amp;#160; In order to accomplish this, we must subscribe to the other event and if it has fired, then we reset a flag to not fire.&amp;#160; We then have to subscribe to the main event and only trigger when the flag is set to true.&amp;#160; Let’s look through how this code might look.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1b4604c3-49d5-4a0e-b6fb-6889853f71af" class="wlWriterSmartContent"&gt;   &lt;pre style="background-color:#ffffff;word-wrap:break-word;white-space:pre-wrap;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;module&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;

 &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; until (other: IEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;_,_&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) (source: IEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;_,_&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
   &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; event &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;_&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;()
   &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; fire &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; ref &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;
   other .Add(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; _    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; fire :&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
   source.Add(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; args &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt;fire &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt; event.Trigger args)
   event.Publish&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In order for us to modify the fire flag, we must use a ref cell.&amp;#160; The reason being is that local mutables are always stack allocated which is why you cannot capture them inside a closure.&amp;#160; One comment that was made about this solution was that we added two subscriptions for creating this single event.&amp;#160; That indeed can be a problem as Tess Ferrandez points out about &lt;a href="http://blogs.msdn.com/tess/archive/2006/01/23/net-memory-leak-case-study-the-event-handlers-that-made-the-memory-baloon.aspx"&gt;event handlers that made the memory balloon&lt;/a&gt; where you hook up to static event handlers that long outlive pages.&amp;#160; That’s not necessarily something we’ll address here, and it’s rare that we’ll run into this situation.&lt;/p&gt;

&lt;p&gt;Now that we have this until defined, what can we do with it?&amp;#160; In my previous post, we used the WebClient to track the progress changed of a download until the download has completed.&amp;#160; We’ll use that same logic here using our F# first class events.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a0344d6e-af93-4d52-b0cf-b26b6e1d1ff3" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;word-wrap:break-word;white-space:pre-wrap;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Net

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDelegateEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Del when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Del :&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Delegate &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Subscribe(d) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    this.AddHandler(d)
    { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDisposable &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; disp.Dispose() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
          this.RemoveHandler(d) }

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; wc &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; WebClient(Credentials &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; NetworkCredential(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;bar&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;))

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; progSub &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  wc.DownloadProgressChanged
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event.until wc.DownloadStringCompleted
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; event &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
       event.Subscribe(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; _ args &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;%d%% Complete&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.ProgressPercentage)

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; downloadSub &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  wc.DownloadStringCompleted.Subscribe (&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; _ args &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;%s&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.Result)   &lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here, once again I’m using the extension method I created that allows us to dispose of our event handlers easily through an IDisposable interface.&amp;#160; This follows the design of the Reactive Framework, and quite frankly, makes a lot of sense.&amp;#160; What we’re able to do is create our DownloadProgressChanged subscription which listens until the DownloadStringCompleted event and then we output our percentage complete as it goes along.&amp;#160; The download subscription is much like before in that we subscribe to the event and can dispose at any time later in our program.&lt;/p&gt;

&lt;h2&gt;Adding Some Monadic Magic&lt;/h2&gt;

&lt;p&gt;Let’s add another wrinkle here to our solution.&amp;#160; What if we want to create our mouse drag event like we had from the previous post using the Reactive Framework?&amp;#160; Let’s review the code that was required in Rx to make this happen.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:06e36010-08f3-4d5b-9ff2-a34809b786b6" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;word-wrap:break-word;white-space:pre-wrap;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; mouseDrag &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; md &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.GetMouseDown()
                &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; mm &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.GetMouseMove().Until(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.GetMouseUp())
                &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; mm;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I may have hinted in the previous post, that LINQ to Objects is an implementation of the list monad and the Reactive Framework is an implementation of the continuation monad.&amp;#160; As Erik points out in one of his recent Channel 9 episodes, indeed many of the interesting things we’re finding in programming are coming in monadic form.&amp;#160; We’ll cover exactly what that means in a later post, but what if we could apply the same kind of logic from the above and apply it to F# first class events as well?&amp;#160; &lt;/p&gt;

&lt;p&gt;In order to make this happen, we need to implement a bind/collect combinator.&amp;#160; This bind function follows the basic pattern of:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8924081d-4379-4594-a518-99220e5fa797" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;word-wrap:break-word;white-space:pre-wrap;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; bind : M&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a&amp;gt; -&amp;gt; (&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; M&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;b&amp;gt;) -&amp;gt; M&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;b&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Where the M is some monadic type.&amp;#160; In the case of LINQ to Objects, this is the SelectMany method and the M type is an IEnumerable&amp;lt;T&amp;gt;.&amp;#160; If we were to construct the collect combinator from scratch for an IEnumerable&amp;lt;T&amp;gt;, it might look like the following:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cc37300d-25b4-4ed4-93e4-4a3ba5b92401" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; collect f &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Seq.map f &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Seq.concat&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And our approach to doing this with events should be no different.&amp;#160; In this case, we also need to implement the concat combinator as well as that does not exist in the base libraries but pretty simple to create.&amp;#160; First, with the concat, we need to take in a IEvent of an IEvent and concatenate them together in a way that we get only an IEvent as a return.&amp;#160; In order to do so, we need to add a handle to our source, which in turn listens to the inner events and triggers our new event that we later publish.&amp;#160; Secondly, we’ll need to implement the collect combinator much like we had above, but this time for events.&amp;#160; Below is what the code might look like.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:01d2c307-da0e-4ec0-ab90-88c714456961" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;module&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;

 &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; concat (source: IEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;IEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;_,_&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; event &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;_&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;()
   source.Add (Event.listen event.Trigger)
   event.Publish

 &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; collect f &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
   Event.map f &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; concat&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now that we have a basis for binding together our events, let’s create a monadic builder so that we could write event expressions much as we would for sequence expressions.&amp;#160; In this case, we only need the bind and not the return for this instance.&amp;#160; A simple return wouldn’t make sense to yield a single value through an event.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5eaef450-6d9e-46ec-bd32-8323ed506557" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; EventBuilder() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Bind(m, f) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event.collect f m
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; event &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; EventBuilder()&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now that we have this defined, F# gives us some syntactic sugar much like the do notation in Haskell.&amp;#160; Let’s now add an extension method to the WinForms Control class.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:64ce2140-239a-4690-8b8e-f304d1c8a057" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;word-wrap:break-word;white-space:pre-wrap;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows.Forms.Control &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.MouseDrag &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; event {
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; _ &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.MouseDown
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.MouseMove &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event.until this.MouseUp }&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And there you have it!&amp;#160; We now have a mouse drag event exposed on our control class which tracks only when the mouse button is down and our mouse is moving.&amp;#160; To get an idea of what it’s doing behind the scenes, I’ll also include the non-syntactic sugar version.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:290cb63b-ff26-42af-9de8-393b407b34b7" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;word-wrap:break-word;white-space:pre-wrap;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows.Forms.Control &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.MouseDrag &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
    event.Bind(this.MouseDown, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; _ &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      this.MouseMove &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event.until this.MouseUp)&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And now we can subscribe to this much like any other event and have it track our mouse.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Creating composable events using F# first class events is a pretty compelling story.&amp;#160; One of the driving ideas behind the Reactive Framework in terms of composable events does in fact come from F#.&amp;#160; Come .NET 4.0, it will be interesting on how Rx and F# First Class Events can co-exist and in fact compliment each other.&amp;#160; Now back to the Reactive Framework series.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=380545" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/r6jvOpi1GTM" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Event-based+Porgramming/default.aspx">Event-based Porgramming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/15/f-first-class-events-composing-events-until-others.aspx</feedburner:origLink></item><item><title>Introducing the Reactive Framework Part I</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/QKMuOlx--5I/introducing-the-reactive-framework-part-i.aspx</link><pubDate>Wed, 14 Oct 2009 05:21:45 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:377351</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>16</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=377351</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/14/introducing-the-reactive-framework-part-i.aspx#comments</comments><description>&lt;p&gt;During my series about first class events in F#, I made frequent mention of the LiveLabs Reactive Framework (Rx), without going into much detail as to what it is.&amp;#160; Now that the series is complete and we understand both how F# first class events work, as well as their integration into F# async workflows, let’s take a look at the Reactive Framework in this multi-part series covering the history, implementation details and more.&amp;#160; &lt;/p&gt;  &lt;p&gt;You may already be familiar with the Reactive Framework due to the videos that Erik Meijer, Wes Dyer and Charles Torre have put on Channel 9.&amp;#160; Each of the following are well worth a watch:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Brian-Beckman-and-Erik-Meijer-Inside-the-NET-Reactive-Framework-Rx/"&gt;Inside the .NET Reactive Framework (Rx)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://channel9.msdn.com/shows/Going+Deep/Kim-Hamilton-and-Wes-Dyer-Inside-NET-Rx-and-IObservableIObserver-in-the-BCL-VS-2010/"&gt;Inside .NET Rx and IObservable/IObserver in the BCL (VS 2010)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://channel9.msdn.com/shows/Going+Deep/E2E-Erik-Meijer-and-Wes-Dyer-Reactive-Framework-Rx-Under-the-Hood-1-of-2/"&gt;Reactive Framework (Rx) Underneath the Hood (1 of 2)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://channel9.msdn.com/shows/Going+Deep/E2E-Erik-Meijer-and-Wes-Dyer-Reactive-Framework-Rx-Under-the-Hood-2-of-2/"&gt;Reactive Framework (Rx) Underneath the Hood (2 of 2)&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;What and Why?&lt;/h2&gt;  &lt;p&gt;So, the question of what are the motivations of Rx?&amp;#160; The first question to ask yourself is “How easy is it in today’s frameworks to do asynchronous programming?”&amp;#160; The answer usually that it’s actually quite difficult, to not only manage exceptional cases and cancellation, but also, how do you compose asynchronous events together to create more interesting events?&amp;#160; The need is actually quite great especially given our multi-core revolution.&amp;#160; So, how can we take advantage to do some rich, reactive programming like AJAX-style applications?&lt;/p&gt;  &lt;p&gt;After covering some of my posts on F# async workflows and F# first class events, some of those motivations start to appear, such as how do you make asynchronous programming and reactive programming truly composable?&amp;#160;&amp;#160; Let’s look at the standard ways of doing asynchronous programming currently on the .NET platform.&amp;#160; In the past, we’ve had to write things such as this in order to asynchronously read data from a Twitter feed:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e29cf8b3-9489-43e9-abd5-8a4cb686a471" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; request &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; WebRequest.Create(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;http://twitter.com/statuses/friends_timeline.xml&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
request.Credentials &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; NetworkCredential(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;bar&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
request.BeginGetResponse(ar &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; response &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; request.EndGetResponse(ar))
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; stream &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; response.GetResponseStream())
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; reader &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; StreamReader(stream))
        PublishResult(reader.ReadToEnd()); &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Do something with the result&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;}, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Since the beginning of .NET, we had both events and the Begin/End with an IAsyncResult for doing asynchronous programming, the latter of which we’ve shown above.&amp;#160; The problem with both is that they weren’t easy to handle exceptions and cancellation.&amp;#160; By the time .NET 2.0 came around, there was a new approach to doing asynchronous programming that in fact handled such concerns.&amp;#160; Such classes as the BackgroundWorker, WebClient and WCF service clients followed this approach.&amp;#160; For example, we could download a user’s timeline from Twitter using the WebClient class.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e256482a-ecb8-41bc-b16f-b9f04f078ab0" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; wc &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; WebClient 
    {Credentials &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; NetworkCredential(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;bar&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)};
wc.DownloadStringCompleted &lt;/span&gt;&lt;span style="color:#000000;"&gt;+=&lt;/span&gt;&lt;span style="color:#000000;"&gt; (o, e) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;(e.Cancelled)
    {
        PublishCancel();
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;; &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Handle cancellation&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;    }
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt;(e.Error &lt;/span&gt;&lt;span style="color:#000000;"&gt;!=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
    {
        PublishException(e.Error);
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;; &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Handle error&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;    }

    PublishResult(e.Result); &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Handle success&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we can somewhat elegantly handle not only the result, but the cancellation and exceptional cases as well.&amp;#160; The problem of course is that there is really no way to unsubscribe from this lambda expression, and just as well, the solution really isn’t composable.&amp;#160; How could I merge this event with another event, or say stop listening after a certain interval or another event happens?&amp;#160; Unlike in F#, in C#, we have no real easy way to deal with a lot of these situations.&amp;#160; Not only that, but we could leave dangling events out there which could be a nice memory leak.&lt;/p&gt;

&lt;p&gt;So, how could this Rx thing help?&amp;#160; Let’s look at a quick example of how it’s different before we go into detail about what it is.&amp;#160; In this example, I’ll build upon it to succinctly handle the exceptional case, but not only that, but also watch the progress changed until the download has completed.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f51fc536-a5ac-41a1-8fc4-f9087d93e47f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; wc &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; WebClient 
    { Credentials &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; NetworkCredential(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;bar&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) };
    
&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Watch progress only until download complete&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; progress &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; wc.DownloadProgressChangedEvent()
    .Until(wc.DownloadStringCompletedEvent());

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Subscribe which creates IDisposable handlers
&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Handle success and exceptional cases&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; progressHandler &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; progress.Subscribe(
    e &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; PublishProgress(e.EventArgs.ProgressPercentage));
    
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; downloadHandler &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; wc.DownloadStringCompletedEvent().Subscribe(
    e &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; PublishResults(e.EventArgs.Result),
    exception &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; PublishException(exception));

...

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Much later in the program, we can dispose&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;progressHandler.Dispose();
downloadHandler.Dispose();&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What we did was allow us to handle exceptional cases as well as the success case on the next invocation of the observable.&amp;#160; I create extension methods which I will cover later, but I want you to get the gist of how this might work.&amp;#160; Since we’re only interested in one firing, we can dispose of the progress subscription quite easily since any call to Subscribe returns an IDisposable which allows us to unsubscribe from our event easily.&amp;#160; This is a similar approach in my F# event posts which allowed me to unsubscribe from any event when I call the subscribe extension method I created.&amp;#160; &lt;/p&gt;

&lt;p&gt;To give one more example about composability, how about creating a simple mouse drag event?&amp;#160; How might we do that given the current state of eventing in .NET?&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ef5ade8e-e9a0-4d1b-8bf4-231a8d8944a2" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;event&lt;/span&gt;&lt;span style="color:#000000;"&gt; MouseEventHandler MouseDrag;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; InitializeMouseDrag()
{
    MouseDrag &lt;/span&gt;&lt;span style="color:#000000;"&gt;+=&lt;/span&gt;&lt;span style="color:#000000;"&gt; (o, e) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; PublishMouseDrag(o, e);

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; fired &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;false&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
    MouseDown &lt;/span&gt;&lt;span style="color:#000000;"&gt;+=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;delegate&lt;/span&gt;&lt;span style="color:#000000;"&gt; { fired &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;; };
    MouseUp &lt;/span&gt;&lt;span style="color:#000000;"&gt;+=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;delegate&lt;/span&gt;&lt;span style="color:#000000;"&gt; { fired &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;false&lt;/span&gt;&lt;span style="color:#000000;"&gt;; };
    MouseMove &lt;/span&gt;&lt;span style="color:#000000;"&gt;+=&lt;/span&gt;&lt;span style="color:#000000;"&gt; (o, e) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    {
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (fired &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color:#000000;"&gt; MouseDrag &lt;/span&gt;&lt;span style="color:#000000;"&gt;!=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
            MouseDrag(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;, e);
    };
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What we’re doing is determining whether our mouse down and mouse move are firing together and if they are, publish the coordinate to the MouseDrag event.&amp;#160; From this code, it’s rather clumsy in order to do this.&amp;#160; Luckily, the Reactive Framework gives us a better alternative for handling such a thing.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:72650a47-c2c4-48f2-944e-722fe219e817" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Create mouse drag&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; mouseDrag &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; md &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.GetMouseDown()
                &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;from&lt;/span&gt;&lt;span style="color:#000000;"&gt; mm &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.GetMouseMove().Until(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.GetMouseUp())
                &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;select&lt;/span&gt;&lt;span style="color:#000000;"&gt; mm;

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Subscribe&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; handler &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; mouseDrag.Subscribe(
    e &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; PublishMouseDrag(e.EventArgs.Location));

...

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Clean up when we&amp;#39;re done with the event&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;handler.Dispose();&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When creating this mouse drag observable, I am able to use LINQ expressions to compose my Observables create from extension methods in order to track mouse down and mouse move until mouse up.&amp;#160; Now are we starting to see the power here?&lt;/p&gt;

&lt;p&gt;Now that we got a feel of what it might look like, what is it?&lt;/p&gt;

&lt;h2&gt;The Reactive Framework&lt;/h2&gt;

&lt;p&gt;There has been a lot of talk around the Reactive Framework on both Channel 9 and among the blogs.&amp;#160; Since the Reactive Framework has been shipped as part of &lt;a href="http://www.codeplex.com/silverlight"&gt;Silverlight 3 Toolkit&lt;/a&gt;, there has been a bit of interest.&amp;#160; Just as LINQ to objects changed the way we thought about interactive programming, the Reactive Framework will change the way we think about reactive programming.&lt;/p&gt;

&lt;p&gt;Let’s first cover some basic terms here with interactive and reactive programming.&amp;#160; When we talk about interactive programming, we’re talking about asking for something and getting it in return.&amp;#160; One common pattern in this world is the iterator pattern.&amp;#160; In contrast, when we think about reactive programming, we register interest and then we have items handed to us asynchronously as they become available.&amp;#160; To give a sushi bar analogy, you could think of interactive programming as the conveyer and as it moves we pick another piece of sushi from the line and put it on our plate.&amp;#160; In contrast, with reactive programming, we register interest that we want sushi, and as they become available, the chef hands us them asynchronously.&amp;#160; &lt;/p&gt;

&lt;p&gt;To put this in code terms, let’s show the interactive version of our sushi model.&amp;#160; We have several items that we wish to eat, but we will only have them delivered if we ask for them.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6ff78f7d-5fec-41cc-b27a-33b21a3c70dd" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetSushi()
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;yield&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Toro&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;yield&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Maki&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;yield&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Uni&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
}

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Pull each sushi piece from the conveyor&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; piece &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetSushi())
    ConsumeSushi(piece);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Inside the IEnumerable&amp;lt;T&amp;gt;, we have our items waiting to go, but will only move when we ask for them.&amp;#160; The inverse would be where we indicate we’re hungry by subscribing to the observable which contains our sushi.&amp;#160; At that point, the house gives us sushi when they are ready and we don’t have to ask for each piece, instead they are pushed to us.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d072506b-aa36-41a3-91cc-136b62f87cf5" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Build up our menu&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetSushi()
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; Observable.Cons(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Toro&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, 
        Observable.Cons(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Maki&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, 
        Observable.Return(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Uni&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)));
}

&lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Eat them as soon as they come to us&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;var&lt;/span&gt;&lt;span style="color:#000000;"&gt; sushiSubscription &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetSushi().Subscribe(
    piece &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; ConsumeSushi(piece));&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the above example, I built up our observable menu of sushi using the Cons and Return static methods on the Observable class.&amp;#160; Then at the bottom, I subscribe that I want to eat them, so I call subscribe passing in my action which fires on each item in our observable.&amp;#160; &lt;/p&gt;

&lt;p&gt;The reason I focus on this, is that there is a certain duality to push and pull, and especially given the way they were designed.&amp;#160; In our above example, the &lt;a href="http://en.wikipedia.org/wiki/Iterator_pattern"&gt;Iterator pattern&lt;/a&gt; is a common interactive programming pattern that’s used quite frequently inside .NET.&amp;#160; This consists of two interfaces, the IEnumerable&amp;lt;T&amp;gt; and the IEnumerator&amp;lt;T&amp;gt;.&amp;#160; Listed below are some of the highlights of these two interfaces.&amp;#160; I cut down what they are to their essential bits.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a9642f4f-9e28-41d4-88ec-3d18b0cdc69b" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    IEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetEnumerator();
}

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEnumerator&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; : IDisposable
{
    T Current { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;get&lt;/span&gt;&lt;span style="color:#000000;"&gt;; }
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; MoveNext();
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The IEnumerable&amp;lt;T&amp;gt; interface exposes a single method which returns an IEnumerator&amp;lt;T&amp;gt; to iterate some object.&amp;#160; The IEnumerator&amp;lt;T&amp;gt; gives us the ability to get the current item and determine whether there are more items to iterate. &lt;/p&gt;

&lt;p&gt;To think about basic asynchronous programming which we’re doing with our observables, we must consider a basic Gang of Four Pattern, the &lt;a href="http://en.wikipedia.org/wiki/Observer_pattern"&gt;Observer Pattern&lt;/a&gt;.&amp;#160; This pattern describes where an object called an Observable, maintains a list of its dependent Observer classes and notifies them automatically of any state changes.&amp;#160; In Java, this should be rather familiar territory with the java.util.Observer class and the java.util.Observable interfaces.&amp;#160; In the Reactive Framework, this approach is no different, as we have an IObservable&amp;lt;T&amp;gt; interface which maintains a list of dependent IObserver&amp;lt;T&amp;gt; interfaces and notifies them automatically of any state changes.&amp;#160; Let’s look at the signatures below:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eb87332f-a4a0-4182-aed3-37e37b274743" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnCompleted();
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnError(Exception exception);
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; OnNext(T value);
}

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color:#000000;"&gt; IObservable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
{
    IDisposable Subscribe(IObserver&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; observer);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What we also get is much like the System.Linq.Enumerable class contains many extension methods for handling IEnumerable&amp;lt;T&amp;gt;, we have the System.Linq.Observable class which contains many of those same extension methods for IObservable&amp;lt;T&amp;gt; instances.&amp;#160; We’ll go into depth of what they are later in this series.&lt;/p&gt;

&lt;p&gt;What you may notice, and what you may have seen on some of Erik Meijer’s Channel 9 videos is that there is a mathematical duality between the Enumerable and Observable, given the signature of these interfaces, as well as the ideas between push and pull.&amp;#160; I’ll go into exactly what that means in the next post.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;SImply stated, asynchronous programming is hard.&amp;#160; To not only manage exceptions and cancellation, but also to make events composable is yet another challenge.&amp;#160; The LiveLabs Reactive Framework gives us the ability to harness reactive programming and treat events as the first class citizens they should have been using LINQ expressions and other standard LINQ combinators.&amp;#160; There is a lot to cover including the mathematical duality, what it is underneath the covers and so on.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=377351" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/QKMuOlx--5I" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Concurrency/default.aspx">Concurrency</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/14/introducing-the-reactive-framework-part-i.aspx</feedburner:origLink></item><item><title>Functional Programming Fundamentals Lectures</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/DHNwSDBsxXY/functional-programming-fundamentals-lectures.aspx</link><pubDate>Fri, 09 Oct 2009 04:38:39 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:370936</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=370936</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/09/functional-programming-fundamentals-lectures.aspx#comments</comments><description>&lt;p&gt;Earlier this year, I had the privilege to record &lt;a href="http://channel9.msdn.com/shows/Going+Deep/Erik-Meijer-and-Matthew-Podwysocki-Perspectives-on-Functional-Programming/"&gt;a Channel 9 video session&lt;/a&gt; covering functional programming basics with Erik Meijer and Charles Torre.&amp;#160; We covered some of the basics, but at some point you need to go back to the fundamentals in order to truly learn this paradigm.&amp;#160; With that, Erik with the help of Charles, have kicked off the Channel 9 lecture series on Functional Programming Fundamentals.&amp;#160; This series is meant to be a journey into some of the ideas behind functional programming, following along with &lt;a href="http://www.cs.nott.ac.uk/~gmh/"&gt;Graham Hutton’s excellent Programming in Haskell&lt;/a&gt; book.&amp;#160; Many of the concepts described in this series are in Haskell first, but many apply as well to F#, and even C# and VB.NET.&amp;#160; What better way to talk about Haskell and functional programming than to learn from one of the authors of the &lt;a href="http://www.haskell.org/onlinereport/"&gt;Haskell 98 Report&lt;/a&gt;.&amp;#160; I highly encourage everyone to check out this series!&lt;/p&gt;  &lt;p&gt;You can find the links for the &lt;a href="http://channel9.msdn.com/tags/C9+Lectures/"&gt;Channel 9 Lecture Series&lt;/a&gt; to see all the videos.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=370936" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/DHNwSDBsxXY" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Haskell/default.aspx">Haskell</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/09/functional-programming-fundamentals-lectures.aspx</feedburner:origLink></item><item><title>Generically Constraining F# – Part III</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/6vLd5zWctHY/generically-constraining-f-part-iii.aspx</link><pubDate>Fri, 02 Oct 2009 05:14:47 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:367868</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=367868</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/02/generically-constraining-f-part-iii.aspx#comments</comments><description>&lt;p&gt;In &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/27/generically-constraining-f-part-ii.aspx"&gt;the previous post&lt;/a&gt;, we talked about some of the generic restrictions that you could do in F#.&amp;#160; I showed some of the basics and how you might use them to your advantage.&amp;#160; We covered the type constraint, null constraint and explicit member constraint.&amp;#160; This time, we’ll cover the rest of them including:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Constructor constraint&lt;/li&gt;    &lt;li&gt;Reference type constraint&lt;/li&gt;    &lt;li&gt;Enumeration type constraint&lt;/li&gt;    &lt;li&gt;Delegate type constraint&lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Constructor contstraint&lt;/h2&gt;  &lt;p&gt;The constructor constraint in F# is identical to that of C# which states that the specified type must have a default constructor.&amp;#160; In order to use this, we must use the &amp;lt;type&amp;gt; : (new : unit –&amp;gt; &amp;lt;type&amp;gt;).&amp;#160; Let’s walk through a basic example of using this in addition to the type constraint to generate a hash of a given file.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b9a7fd4a-609b-44ba-98c2-b516d46ac7db" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Security.Cryptography

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; generateHash&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : ( &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a)&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;                    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a :&amp;gt; HashAlgorithm&amp;gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt; path &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; algo &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a()&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;use&lt;/span&gt;&lt;span style="color:#000000;"&gt; stream &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; File.OpenRead path  
  algo.ComputeHash stream
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; BitConverter.ToString
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; s &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; s.Replace(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;-&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What this code does is first declares that our type must have a default constructor and must derive from System.Security.Cryptography.HashAlgorithm.&amp;#160; We can create our new generic type much as we would in other languages that support such a feature and then use it to generate our hash.&amp;#160; Let’s verify the behavior by generating an MD5 hash of notepad.exe using the Cryptography Next Generation MD5 class.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eb37b593-6ae1-4bb7-82fb-b210290ae1de" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; generateHash&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;MD5Cng&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;notepad.exe&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;D3002BDDC758A762736730DE399920E5&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Reference Type Constraint&lt;/h2&gt;

&lt;p&gt;Much like the constructor constraint, this constraint is also identical to the C# class constraint.&amp;#160; In C#, this restraint is rather oddly named, however, due to the fact that it restricts not only classes, but also interfaces, arrays and delegates.&amp;#160; Instead, F# chose the more apt name of not struct.&amp;#160; Let’s look through an example of using this to initialize a reference type sequence.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d2a1fb1a-2449-43a1-858f-1d39680b0e91" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; createRefSeq&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : not struct&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; : &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a -&amp;gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a seq &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  Seq.singleton&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now we can verify the behavior of this such as the following:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:224ad885-f12c-484d-acb1-207e119eec0a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; createRefSeq &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : seq&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; seq [&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;]
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; createRefSeq (Func&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;_&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; () &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; DateTime.Now));;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : seq&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Func&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;DateTime&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  seq [System.Func`&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;[System.DateTime] {Method &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.DateTime Invoke();
                                       Target &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; FSI_0061&lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt;it@&lt;/span&gt;&lt;span style="color:#800080;"&gt;90&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#800080;"&gt;4&lt;/span&gt;&lt;span style="color:#000000;"&gt;;}]
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; createRefSeq &lt;/span&gt;&lt;span style="color:#800080;"&gt;3&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;

  createRefSeq &lt;/span&gt;&lt;span style="color:#800080;"&gt;3&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;--------------^&lt;/span&gt;&lt;span style="color:#000000;"&gt;

stdin(&lt;/span&gt;&lt;span style="color:#800080;"&gt;92&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;/span&gt;&lt;span style="color:#800080;"&gt;15&lt;/span&gt;&lt;span style="color:#000000;"&gt;): error FS0001: A generic construct requires that the &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;int&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
have reference semantics, but it does not, i.e. it &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;is&lt;/span&gt;&lt;span style="color:#000000;"&gt; a struct&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Enumeration Constraint&lt;/h2&gt;

&lt;p&gt;This type constraint, unlike the other two covered in this post cannot be expressed in the current C# version.&amp;#160; We covered this constraint as part of how we could solve Jon Skeet’s problem of Enum.GetValues.&amp;#160; Let’s revisit that solution with a twist of getting the name from a value.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b71fc6ab-abf1-4d82-9864-39b755e7d149" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; getEnumName&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;b when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a :&amp;gt; Enum &lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;                       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a :&amp;gt; ValueType&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;                       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a : enum&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;b&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 (value:&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a) =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  Enum.GetName(typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a&amp;gt;, value)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt; And now we can test our behavior such as the following:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a66dacec-758d-4988-bb71-e181d5a49b70" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; getEnumName DayOfWeek.Saturday;;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Saturday&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Delegate Constraint&lt;/h2&gt;

&lt;p&gt;Lastly, the delegate constraint constrains the generic type to be a delegate.&amp;#160; This constraint, probably the least used of the constraints, can in fact be put to good use.&amp;#160; In order to use this, we must use the &amp;lt;type&amp;gt; : delegate&amp;lt;args, unit&amp;gt; to declare this restriction.&amp;#160; In my F# First Class Events series, I covered this as a way of adding subscribe capability to F# events in which we can easily subscribe and unsubscribe from an event based upon handing back an IDisposable which on called, removes the event handler.&amp;#160; Let’s go through that solution again.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9d5327b6-6563-452d-8883-e2431735cac7" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; IEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Del,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Args when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Del : delegate&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Args,unit&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
                       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Del :&amp;gt; Delegate &amp;gt; with&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Subscribe(d) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    this.AddHandler(d)
    { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDisposable &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; x.Dispose() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
          this.RemoveHandler(d) }&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What this allows us to do is add a subscription that we can unsubscribe from easily via the IDisposable object we hand back.&amp;#160; Let’s test this a subscription to a Windows Form Click event and then unsubscribe:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:54ad5a62-e70f-4a3f-8200-a5f5b6bb6c87" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows.Forms
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; f &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Form(Visible&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;, TopMost&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; count &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; ref &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; h &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; f.Click.Subscribe(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; _ _ &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; count :&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt;count &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;; printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;%d&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt;count);;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; h : IDisposable

&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#800080;"&gt;2&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#800080;"&gt;3&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#800080;"&gt;4&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#800080;"&gt;5&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; h.Dispose();;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What we did was create a quick form and then add our handler.&amp;#160; After a few clicks, we get bored, and then decide to dispose of our little counter.&amp;#160; This idea is in keeping with the Reactive Framework which I’ll start on in my next series.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;As you can see, the F# language gives us quite a bit of flexibility with generic restrictions.&amp;#160; Going above and beyond what C# already has, we are able to constrain to such things as enum, method signatures, delegates and so on.&amp;#160; Many of these explicit type signatures, although nice to specify, can be inferred by the F# compiler a lot of the time.&amp;#160; With the last segment of first class events, we’ll next step into the Reactive Framework.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=367868" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/6vLd5zWctHY" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/02/generically-constraining-f-part-iii.aspx</feedburner:origLink></item><item><title>Generically Constraining F# – Part II</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/VEUqVl5NI3s/generically-constraining-f-part-ii.aspx</link><pubDate>Mon, 28 Sep 2009 02:32:33 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:367528</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>5</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=367528</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/27/generically-constraining-f-part-ii.aspx#comments</comments><description>&lt;p&gt;In the previous post, we talked about the basic problems with C# generic restrictions.&amp;#160; We covered how it was possible in fact in the CLR to do these restrictions and sure enough the F# language supports them.&amp;#160; This time, we’ll go deeper into what kinds of generic restrictions are possible and how we might use them.&amp;#160; But, before we begin, let’s revisit our previous example.&lt;/p&gt;  &lt;h2&gt;Revisiting Our Previous Example&lt;/h2&gt;  &lt;p&gt;One commenter correctly pointed out that a more literal translation of the Enum.GetValues would in fact export to C#:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:982673ab-eaf1-4b10-9a40-65ad712bc7bf" class="wlWriterSmartContent"&gt;   &lt;pre style="background-color:#ffffff;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; getValues&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;struct&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a :&amp;gt; Enum&amp;gt;() =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  Enum.GetValues(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a&amp;gt;) :?&amp;gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a array&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In this case, we are using a value type and then a type constraint to ensure that it is both a structure and derives from System.Enum.&amp;#160; Alternatively, we could also use the F# enum constraint as well, such as the following:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2102bcf0-55f3-4624-bb75-38eb2d17b720" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; getValues&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;b when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a : struct and &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : enum&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;b&amp;gt; and &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a :&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Enum&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  Enum.GetValues(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a&amp;gt;) :?&amp;gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a array&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And then we could execute as before:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c7d6f530-eaa5-4cb5-a445-3a68eb138516" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.IO;;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; getValues&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;FileAttributes, int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;();;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : FileAttributes array &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  [&lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt;ReadOnly; Hidden; System; Directory; Archive; Device; Normal; Temporary;
    SparseFile; ReparsePoint; Compressed; Offline; NotContentIndexed;
    Encrypted&lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt;]&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Both of these examples work when exported to C# as well.&amp;#160; Now that we’re certain we can get C# to respect our generic restriction, what else could we do?&lt;/p&gt;

&lt;h2&gt;Generic Restrictions&lt;/h2&gt;

&lt;p&gt;The F# language supports quite a few ways that we can constrain generics.&amp;#160; Each one listed below will be gone into detail:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Type constraint &lt;/li&gt;

  &lt;li&gt;Null constraint &lt;/li&gt;

  &lt;li&gt;Explicit member constraint &lt;/li&gt;

  &lt;li&gt;Constructor constraint &lt;/li&gt;

  &lt;li&gt;Reference type constraint &lt;/li&gt;

  &lt;li&gt;Enumeration type constraint &lt;/li&gt;

  &lt;li&gt;Delegate constraint &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Type Constraint&lt;/h2&gt;

&lt;p&gt;A type constraint is where the provided type must be the same or derived from the specified type.&amp;#160; In the case of interfaces, the provided type must implement said interface.&amp;#160; As you saw earlier in the getValues example, we used a type constraint to ensure that our provided type must derive from System.Enum, as all enums do.&amp;#160; In this case, let’s define how to add a line as a child to a given element which implements the IAddChild interface in WPF:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c5396a8c-04fe-4621-9b43-7fb3e9f97f3f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; drawLine&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a :&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; IAddChild&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; stroke (x1, y1) (x2, y2) (e : &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a) =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; line &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; LineGeometry(StartPoint &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Point(x1, y1), EndPoint &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Point(x2, y2))
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; path &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Path(Stroke &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; stroke, StrokeThickness &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;5&lt;/span&gt;&lt;span style="color:#000000;"&gt;., Data &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; line)
  e.AddChild(path)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By using the :&amp;gt; operator, we constrain our ‘a type that it must implement the IAddChild interface.&amp;#160; Alternatively, we could use the #&amp;lt;type&amp;gt; inline to our parameter to mean the same thing, where type means the what the type must inherit from or implement.:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d77c4543-0e2e-498d-8f6e-d0541ce9589d" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; drawLine stroke (x1, y1) (x2, y2) (e : #IAddChild) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; line &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; LineGeometry(StartPoint &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Point(x1, y1), EndPoint &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Point(x2, y2))
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; path &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Path(Stroke &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; stroke, StrokeThickness &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;5&lt;/span&gt;&lt;span style="color:#000000;"&gt;., Data &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; line)
  e.AddChild(path)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Null Constraint&lt;/h2&gt;

&lt;p&gt;In this constraint, the provided type must support being set to null.&amp;#160; This will include all .NET reference types, but excludes value types as well as F# types of lists, tuples, functions, classes, records or union types.&amp;#160; By using the ‘a : null syntax, we can say that ‘a is a nullable reference.&amp;#160; Note that this doesn’t work for Nullable&amp;lt;T&amp;gt; types either.&amp;#160; Let’s look at a quick example of using the null constraint:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0d88a728-e004-4d5e-bf55-6ae67c8e337d" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; printValue&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; (arg:&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a) =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; arg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;You gave me nothing&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Value: %A&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; arg&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt; Then we could test the behavior by giving it several examples such as the following:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:41c1aa9c-622f-4136-9070-91e6f27f39ec" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; printValue &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hello&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;
Value: &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hello&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : unit &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; ()
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; printValue &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;
You gave me nothing&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Explicit Member Constraint&lt;/h2&gt;

&lt;p&gt;The explicit member constraint states that the type argument provided must have a member that has the specified signature.&amp;#160; This means that we could in fact do duck typing in such a way that our argument must support a certain member function.&amp;#160; I’ve covered this in the past, but let’s review.&amp;#160; One example would be to create a function which allows us to invoke implicit operators, which are by default not supported by F#.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:73e780e7-0839-410e-aa47-2bba018da181" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inline&lt;/span&gt;&lt;span style="color:#000000;"&gt; implicit&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a, &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b when &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a)&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
  arg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    (&lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a) arg)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This allows me to specify that my ^a type must support a static method called op_Implicit, simply known as the implicit operator.&amp;#160; Alternatively, we can allow type inference to do the work for us and leave off the top level declaration and declare the function calls inline and the results will be the same.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e19032f8-9e11-445f-a725-8da77beeae24" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inline&lt;/span&gt;&lt;span style="color:#000000;"&gt; implicit arg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  (&lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;b &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a) arg)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt; We can test the behavior of this by using the implicit operator built into LINQ to XML.&amp;#160; Using the library is not exactly as easy as it is in C# due to the implicit operator from string to XName.&amp;#160; In this instance, I’ll use the ( !! ) operator, or the convert dammit operator, to use on the XNamespace class and then append a string to create an XName.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c62385cc-83e3-4b67-9046-64fcedc080d4" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Xml.Linq;;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt; ) : &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XNamespace &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; implicit;;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; ( &lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#000000;"&gt; ) : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XNamespace)

&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; foo &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; foo : XName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; foo&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we created the foo XName by creating an empty XNamespace and appending our “foo”.&amp;#160; Before we wrap up this section, let’s try one more example which supports multiple multiple methods.&amp;#160; In this case, we’ll extract both the name as an XName, and the value from a given LINQ to XML object.&amp;#160; The reason I do this is that the XElement and XAttribute do not share a common ancestor which supports a Name and Value property. &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:96a4013e-3b35-4aa1-91d5-f6e57d891566" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Xml.Linq

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inline&lt;/span&gt;&lt;span style="color:#000000;"&gt; getNameValue&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a when &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; get_Name  : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XName ) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt;
                                 &lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; get_Value : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 xItem &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;  (&lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; get_Name  : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XName ) xItem)
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; value &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; get_Value : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;) xItem)
  name, value&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this case, we constrain our generic that it must implement both a Name and Value property getter.&amp;#160; Then we invoke both the name and value functions and return them as a tuple.&amp;#160; Once again, as with above, we can leave off these constraints and let the type inference magic do the work for us.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:af9fd451-3b85-4c4b-aeca-cb2d240ec657" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Xml.Linq

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;inline&lt;/span&gt;&lt;span style="color:#000000;"&gt; getNameValue xItem &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; name &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;  (&lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; get_Name  : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; XName ) xItem)
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; value &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#000000;"&gt;^&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; get_Value : unit &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;) xItem)
  name, value&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can begin to see the power in this where we are no longer constrained by inheritance, and instead which methods our type supports.&amp;#160; This is different of course than the dynamic type forthcoming in .NET 4.0 as this is statically checked at compile time instead of at runtime.&amp;#160; Let’s walk through some simple examples using it:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:564e458b-9452-4dcd-a30a-0e00c7351b01" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; getNameValue (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; XElement(&lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;bar&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;));;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : XName &lt;/span&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; (bar {LocalName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;bar&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
                                Namespace &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;
                                NamespaceName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;}, &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; getNameValue (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; XAttribute(&lt;/span&gt;&lt;span style="color:#000000;"&gt;!!&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;bar&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;));;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;val&lt;/span&gt;&lt;span style="color:#000000;"&gt; it : XName &lt;/span&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; (foo {LocalName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;foo&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
                                Namespace &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; ;
                                NamespaceName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;}, &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;bar&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; getNameValue (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; XDocument());;

  getNameValue (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; XDocument());;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;--------------^^^^^^^^^^^^^^^&lt;/span&gt;&lt;span style="color:#000000;"&gt;

stdin(&lt;/span&gt;&lt;span style="color:#800080;"&gt;238&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;/span&gt;&lt;span style="color:#800080;"&gt;15&lt;/span&gt;&lt;span style="color:#000000;"&gt;): error FS0001: The &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;XDocument&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt; does not support any operators
 named &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;get_Name&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now you can see it works nicely with XElement and XAttribute, but not with XDocument as it doesn’t support the Name property with a getter accessor, nor does it support a Value property either.&amp;#160; I think that’s enough for this post.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;So far, we’ve covered just a few of the generic type constraints that you can do with F# including type, null constraint and member constraint.&amp;#160; In the next part, we’ll cover the rest of the generic constraints that F# supports.&amp;#160; &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=367528" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/VEUqVl5NI3s" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/27/generically-constraining-f-part-ii.aspx</feedburner:origLink></item><item><title>Pondering Axum + F#</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/-ySfp2G83KE/pondering-axum-f.aspx</link><pubDate>Fri, 25 Sep 2009 19:42:05 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:365447</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>9</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=365447</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/25/pondering-axum-f.aspx#comments</comments><description>&lt;p&gt;It’s been a while since I’ve posted about &lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx"&gt;Axum&lt;/a&gt; as I’ve been posting about other asynchronous and parallel programming models.&amp;#160; After two releases of the Axum standalone language and lots of good user feedback, it’s time to ponder what could be with Axum.&amp;#160; Overall, the goals of Axum to provide an agent-based concurrency oriented system is important as we consider the emerging hardware trends.&amp;#160; Many of these ideas would in fact benefit most languages, whether mainstream or not.&amp;#160; With all the success that it has had, there have also been some issues as well, which leads me to wonder, what else could we do here?&amp;#160; Let step through some of those issues today and see where we could go.&lt;/p&gt;  &lt;h2&gt;Should It Be Its Own Language?&lt;/h2&gt;  &lt;p&gt;One of the highlighted concerns was the need to have yet another language.&amp;#160; Creating and maintaining languages is a large effort, not to be undertaken lightly at any organization.&amp;#160; To base your language on C# while adding additional constructs was a great way to get started and create some buzz and buy-in, but it also brought its own set of problems.&amp;#160; &lt;/p&gt;  &lt;p&gt;One problem is that it is an enormous effort to keep on par with the C# compiler and add these constructs on a continuing basis.&amp;#160; The Spec# project, also based upon C# realized that it wasn’t feasible to keep up with the rapid pace of the language and instead went as a language neutral approach.&amp;#160; Not only keeping up with the language, but to enforce many functional programming constructs that Axum embraces such as immutability and purity are harder to enforce given a language which is built upon object-oriented and imperative approaches, an approach which encourage the encapsulation and manipulation of state.&amp;#160; So, instead of creating a separate language, why not fold these constructs into an existing language instead?&lt;/p&gt;  &lt;h2&gt;The Case For F#&lt;/h2&gt;  &lt;p&gt;Given the impedance mismatch between the C# language and the overall goals of Axum for a safe, isolated, agent-based system that is great for concurrency, why not consider another language such as F#?&amp;#160; Now that F# is a first-class citizen within Visual Studio going forward, there can be a strong case made.&amp;#160; Given some of the features of F#, which we will discuss, could potentially be a happy marriage with Axum.&amp;#160; The F# language and its associated libraries have many of the constructs that are essential to an agent-based messaging system, including immutability (albeit shallow), encouraging side effect free programming, and library functions such as the mailbox processor messaging classes.&lt;/p&gt;  &lt;h2&gt;In Praise Of Immutability&lt;/h2&gt;  &lt;p&gt;In the world of concurrency we find that shared mutable state is evil.&amp;#160; Often we have to protect certain parts by using low level constructs such as locks, semaphores, mutexes.&amp;#160; Because of this, we find that most people tend to avoid concurrency programming as it tends to be too hard to get just right.&amp;#160; Instead, communication through message passing in languages such as Axum and Erlang, immutability is the standard.&amp;#160; This way, we can freely reference the values without any worry of whether they will change underneath us.&amp;#160; Providing first-class support for immutability goes a long way towards making safe concurrency programming easier for the masses.&amp;#160; With the F# language, we get that approach by default and with rich types such as records, discriminated unions, lists, tuples and more.&amp;#160; &lt;/p&gt;  &lt;p&gt;Of particular note, Discriminated Unions are quite useful in messaging systems to define the types of messages your system accepts.&amp;#160; For example, we could model our auction messages quite easily using discriminated unions to define which interactions are legal, such as the following code:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:af8b6f4c-9ece-42ca-851b-71958ff2d310" class="wlWriterSmartContent"&gt;   &lt;pre style="background-color:#ffffff;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuctionMessage &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Offer of &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; MailboxProcessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;AuctionReply&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Inquire of MailboxProcessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;AuctionReply&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt;  AuctionReply &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Status of &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.DateTime
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; BestOffer
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; BeatenOffer of &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuctionConcluded of 
      MailboxProcessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;AuctionReply&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; MailboxProcessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;AuctionReply&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuctionFailed
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuctionOver&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In this case, we have succinctly defined what messages can be processed.&amp;#160; Adding to this, Axum, through the use of data-flow networks and channel protocols could be a nice compliment to determine how these messages are processed.&amp;#160;&amp;#160; Where else could they match up nicely?&lt;/p&gt;

&lt;h2&gt;Built For Concurrency&lt;/h2&gt;

&lt;p&gt;To talk about a potential marriage between F# and Axum, we need to talk about the language and its libraries and how they support concurrency.&amp;#160; From first class events, to asynchronous workflows, to mailbox processors, F# has a wide array of asynchronous and parallel programming libraries available out of the box.&amp;#160; In particular, the asynchronous workflows and the mailbox processor could match well with the Axum programming model.&amp;#160; Interestingly, &lt;a href="http://blogs.msdn.com/lucabol/"&gt;Luca Bolognese&lt;/a&gt; has built upon the mailbox processor for his &lt;a href="http://blogs.msdn.com/lucabol/archive/2009/09/18/lagent-an-agent-framework-in-f-part-ix-counting-words.aspx"&gt;LAgent framework&lt;/a&gt; to show some more interesting scenarios where agent based programming could work.&amp;#160; This includes such things as advanced error handling, hot swapping, implementing MapReduce and so on.&lt;/p&gt;

&lt;p&gt;With little effort, we can model such things as the standard ping-pong example using F# mailboxes:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a25322f1-22dd-4b5b-a3c1-660d19314ebb" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; (&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color:#000000;"&gt;) (m:MailboxProcessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;_&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) msg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; m.Post(msg)
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; PingMessage &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Ping of MailboxProcessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;PongMessage&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Stop
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;and&lt;/span&gt;&lt;span style="color:#000000;"&gt;  PongMessage &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Pong

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; ping iters (pong : MailboxProcessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;PingMessage&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; MailboxProcessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;PongMessage&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; inbox &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
    pong &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color:#000000;"&gt; Ping inbox
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;rec&lt;/span&gt;&lt;span style="color:#000000;"&gt; loop pingsLeft &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; async { 
      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; msg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; inbox.Receive()
      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;match&lt;/span&gt;&lt;span style="color:#000000;"&gt; msg &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Pong &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
          &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; pingsLeft &lt;/span&gt;&lt;span style="color:#000000;"&gt;%&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1000&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt;
            printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Ping: pong&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
          &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; pingsLeft &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt;
            pong &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color:#000000;"&gt; Ping inbox
            &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; loop(pingsLeft &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
          &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;
            printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Ping: stop&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
            pong &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color:#000000;"&gt; Stop
            &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; () }
    loop (iters &lt;/span&gt;&lt;span style="color:#000000;"&gt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;))
            
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; pong () &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; MailboxProcessor&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;PingMessage&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; inbox &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;rec&lt;/span&gt;&lt;span style="color:#000000;"&gt; loop pongCount &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; async { 
      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; msg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; inbox.Receive()
      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;match&lt;/span&gt;&lt;span style="color:#000000;"&gt; msg &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Ping sender &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
          &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; pongCount &lt;/span&gt;&lt;span style="color:#000000;"&gt;%&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1000&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt;
            printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Pong: ping %d&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; pongCount
          sender &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;--&lt;/span&gt;&lt;span style="color:#000000;"&gt; Pong
          &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; loop (pongCount &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;) 
      &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Stop &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Pong: stop&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; () }
    loop &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; ponger &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; pong()
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; pinger &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; ping &lt;/span&gt;&lt;span style="color:#800080;"&gt;100000&lt;/span&gt;&lt;span style="color:#000000;"&gt; ponger
pinger.Start()
ponger.Start()&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And you’ll notice from all of this, there are no mutable values anywhere nor shared global state and all communication happens through message passing.&amp;#160; We have certain limitations here such as we cannot remote these calls, so all interaction happens inside the single application.&amp;#160; Not only that, but how do we enforce no shared state?&lt;/p&gt;

&lt;h2&gt;What Would It Look Like?&lt;/h2&gt;

&lt;p&gt;So, now that we looked at some of the reasons why F# and Axum might be a good marriage, what might it actually look like?&amp;#160; That’s a great question!&amp;#160; Some of the concepts from Axum could map perfectly to F# whereas some might need a little more coaxing.&amp;#160; Let’s go back to the basic building blocks of Axum.&amp;#160; They consist of the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Agents and domains &lt;/li&gt;

  &lt;li&gt;Channels &lt;/li&gt;

  &lt;li&gt;Schema &lt;/li&gt;

  &lt;li&gt;Networks &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first three items are concerned mostly with state isolation and the exchange of information between our isolated regions of our application, while the last item deals more with the how messages are passed.&amp;#160; &lt;/p&gt;

&lt;p&gt;The domain is the most basic isolation concept where all data is encapsulated and only constructors are exposed outside of the domain definition.&amp;#160; Agents are defined to run within the isolated space of a domain with each agent on a separate thread of control.&amp;#160; These agents are exposed to one another through passing messages back and forth via channels which define ports through which our data flows.&amp;#160; The data that passes between our domains can be defined in a schema, similar in nature of XML Schemas, to define structure and rules of our data.&amp;#160; Now to think about message passing, F# already has asynchronous behavior already built in to the mailbox processor, so we could take advantage here and build on top of it and our networks could guide us as to what order messages are passed.&lt;/p&gt;

&lt;p&gt;Let’s look at some possible code examples of what things might look like.&amp;#160; I’ll choose just a couple as they might work nicely out of the box.&amp;#160; Let’s first look at how we might define a port which accepts a given message.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4214dc67-da06-41bb-bc66-72e129322987" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProductInput &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Multiply of &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; Done
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProductOutput &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Product of &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt; AckDone&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This way, we could define what our payload type for both incoming and outgoing.&amp;#160; Another area would be to look how schemas might be done.&amp;#160; In our earlier example, we used simple data types such as integers, but what about more complex types?&amp;#160; Using F#, we could think of using records here to simulate the not only the data, but what is and is not required:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4cd19f3b-99df-4ff0-b64d-2c1cba34fcd5" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; PersonSchema &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
  { Name     : &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    Employer : &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    Age      : &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt; option }

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; matt &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  { Name &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Matthew Podwysocki&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    Employer &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Microsoft Corporation&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    Age &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; None }&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;By marking the fields we require as standard fields and those which are optional as option types works out nicely.&amp;#160; Using F#, we’re able to have concise syntax with a language that is already defined and quite flexible.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;As you can see from this post, a potential marriage between Axum and F# would certainly be a great fit.&amp;#160; Instead of focusing on Axum as a separate language, focusing the effort on top of an existing language which matches many of the design goals can bear a lot of fruit.&amp;#160; I believe Axum is important for the .NET programming stack, giving us more options on how to deal with concurrency and changing hardware architectures.&amp;#160; What do you think?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=365447" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/-ySfp2G83KE" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Concurrency/default.aspx">Concurrency</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Axum/default.aspx">Axum</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/25/pondering-axum-f.aspx</feedburner:origLink></item><item><title>Generically Constraining F# – Part I</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/QxZ4ht9-764/constraining-f.aspx</link><pubDate>Mon, 14 Sep 2009 22:44:29 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:346083</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>3</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=346083</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/14/constraining-f.aspx#comments</comments><description>&lt;p&gt;Generic constraints inside .NET has always been a fun enterprise, especially given how C# handles them&amp;#160; There has been &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2009/09/10/generic-constraints-for-enums-and-delegates.aspx"&gt;some discussion on Jon Skeet’s blog&lt;/a&gt; about the fact that C# does not allow for generic constraints referring to a number of types.&amp;#160; These include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;System.Array &lt;/li&gt;    &lt;li&gt;System.Delegate &lt;/li&gt;    &lt;li&gt;System.Enum &lt;/li&gt;    &lt;li&gt;System.Object &lt;/li&gt;    &lt;li&gt;System.ValueType &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This is indeed a bit unfortunate, as it limits some of the more interesting applications.&amp;#160; The example Jon shows is indeed illegal in C#:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2e23abd1-d621-4e95-86bb-397403584b1e" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; T[]GetValues&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;() &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;where&lt;/span&gt;&lt;span style="color:#000000;"&gt; T : &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;struct&lt;/span&gt;&lt;span style="color:#000000;"&gt;, System.Enum
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; (T[]) Enum.GetValues(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt;(T));
} &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;However, as Jon correctly points out, this is indeed supported by the CLR directly.&amp;#160; In fact, with our knowledge of &lt;a href="http://msdn.microsoft.com/en-us/library/dd233203%28VS.100%29.aspx"&gt;F# constraints&lt;/a&gt;, we can write this exact function in F# without any such issue.&amp;#160; It’s little wonder that F# has learned some of their lessons from C#, but as well as having the language designed by the person who brought generics to .NET also helps.&lt;/p&gt;

&lt;p&gt;Let’s first look at our F# implementation.&amp;#160; The idea here is to ensure that our T type as above is an enum.&amp;#160; In order to do so, we must specify it is of type enum&amp;lt;underlying-type&amp;gt; where the underlying type is most usually an Int32.&amp;#160; Remember, &lt;a href="http://msdn.microsoft.com/en-us/library/system.enum.aspx"&gt;enums can be of any integral besides char&lt;/a&gt;, so that’s why it must be specified.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5e8b640f-e5c6-4e88-ac39-b98761ab77ad" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;namespace&lt;/span&gt;&lt;span style="color:#000000;"&gt; Codebetter.Constraints

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;module&lt;/span&gt;&lt;span style="color:#000000;"&gt; Constraint &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;

  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System

  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; getValues&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a, &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;b when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a : enum&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;b&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    Enum.GetValues(typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a&amp;gt;) :?&amp;gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a array&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;As we can see from the above code, it’s rather straight forward.&amp;#160; We specify the ‘a must be an enum of an inner type of ‘b.&amp;#160; We could have simplified this for an external call to export it using an int as our ‘b, but let’s keep this as generic as possible.&amp;#160; Calling this code, we can get arrays of all values.&amp;#160; Let’s test in F# interactive:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:96f08204-5ec9-4a22-ace7-a1916cc402eb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Constraint.getValues&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;System.IO.FileAccess,&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;();;
val it : System.IO.FileAccess array &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; [&lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt;Read; Write; ReadWrite&lt;/span&gt;&lt;span style="color:#000000;"&gt;|&lt;/span&gt;&lt;span style="color:#000000;"&gt;]
&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Constraint.getValues&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;();;

  Constraint.getValues&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;();;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;^^^^^^^^^^^^^^^^^^^^&lt;/span&gt;&lt;span style="color:#000000;"&gt;

error FS0001: The type &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;string&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;is&lt;/span&gt;&lt;span style="color:#000000;"&gt; not a .NET &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;enum&lt;/span&gt;&lt;span style="color:#000000;"&gt; type&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In our first example, we call with System.IO.FileAccess in which it gives us the three enum values, and in our second case, we tried with a non-enum type of string and sure enough it tells us as much.&amp;#160; But, what about C# here?&amp;#160; Could this code transfer?&amp;#160; In some C# calling code, we could then use our function as follows:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:527d59ad-838e-41c0-9f25-92d06d4c0511" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; Main(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;[] args)
{
    var values &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Constraint.getValues&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;FileAttributes, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;();
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color:#000000;"&gt;(var value &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; values) Console.WriteLine(value);
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will give us the expected results of all values for the FileAttributes enum just as we would through our F# code.&amp;#160; An issue arises, however, when we try with a failure case as we had above:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fdf9a60c-c882-4a9e-bb98-9012152f5701" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;var values &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Constraint.getValues&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;int&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;();
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color:#000000;"&gt; (var value &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; values) Console.WriteLine(value);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will compile just as our previous example did.&amp;#160; When we run this example, however, we get an exception thrown due to Enum.GetValues expecting an enum type as we have below and our constraint not honored.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:429d0453-c73f-4365-b733-7b0bb68931f9" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;Unhandled Exception: System.ArgumentException: 
Type provided must be an Enum.
Parameter name: enumType
   at System.Enum.GetValues(Type enumType)
   at Codebetter.Constraints.Constraint.getValues[a,b]() 
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;in&lt;/span&gt;&lt;span style="color:#000000;"&gt; C:\Work\ConstraintLib\Module1.fs:line &lt;/span&gt;&lt;span style="color:#800080;"&gt;19&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So, the important thing to remember is that even if other languages export generic restrictions such as an enum, C# will not honor this restriction.&amp;#160; Other restrictions such as reference type restrictions such as the following work well:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1984b638-21de-42f4-85c1-0b65aee82c7f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; printClass&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;a : not struct&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; (arg:&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;a) =&lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;%A&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; arg&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And the calling C# code:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a3fbbd21-07fa-4a6c-8f9f-ebfcf2ef0303" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;Constraint.printClass(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Hello&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; prints Hello&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;Constraint.printClass(&lt;/span&gt;&lt;span style="color:#800080;"&gt;3&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; error CS0452&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By the code above, we sure enough have the compiler honoring our generic restrictions in this case.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;We’re just getting started here in this brief series to cover generic constraints.&amp;#160; There are a few more to talk about before we’re done such as method signature restrictions, constructor restrictions and so forth.&amp;#160; I hope at some interation that generic restrictions in C# get revisited to make them as fully features as F#’s.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=346083" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/QxZ4ht9-764" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/C_2300_/default.aspx">C#</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/14/constraining-f.aspx</feedburner:origLink></item><item><title>No:SQL (east) 2009</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/Ldv7KOLL0R8/no-sql-east.aspx</link><pubDate>Thu, 10 Sep 2009 04:28:27 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:344888</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>6</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=344888</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/10/no-sql-east.aspx#comments</comments><description>&lt;p&gt;As you may have noticed, there has been &lt;a href="http://www.computerworld.com/s/article/9135086/No_to_SQL_Anti_database_movement_gains_steam_"&gt;a lot of talk&lt;/a&gt; around &lt;a href="http://en.wikipedia.org/wiki/Nosql"&gt;NOSQL “movement”&lt;/a&gt; lately.&amp;#160; The name, NOSQL was created to describe non-traditional data storage engines and techniques to address concerns of data sets of horizontal scale.&amp;#160; Innovations such as &lt;a href="http://en.wikipedia.org/wiki/BigTable"&gt;Google’s BigTable&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Dynamo_(storage_system)"&gt;Amazon’s Dynamo&lt;/a&gt; have led to a rise in a new wide variety of new technologies and ideas around pointed problems as data sets at scale, like &lt;a href="http://couchdb.apache.org/"&gt;CouchDB&lt;/a&gt;, &lt;a href="http://code.google.com/p/redis/"&gt;Redis&lt;/a&gt;, &lt;a href="http://www.mongodb.org/display/DOCS/Home"&gt;MongoDB&lt;/a&gt;, &lt;a href="http://incubator.apache.org/cassandra/"&gt;Cassandra&lt;/a&gt;, &lt;a href="http://project-voldemort.com/"&gt;Voldemort&lt;/a&gt; and many more.&amp;#160; These NOSQL technologies have little unifying them together as they use such techniques as &lt;a href="http://en.wikipedia.org/wiki/Column-oriented_DBMS"&gt;Column-Oriented&lt;/a&gt;, Key-Value Stores, &lt;a href="http://www.infoq.com/presentations/availability-consistency"&gt;CAP Theorem&lt;/a&gt;, and no one to rule them all, but underlying them is that they are different than the “traditional” RDBMS solutions of SQL Server, Oracle, MySQL and so on.&amp;#160; These NOSQL solutions are starting to turn up in good numbers for specific use cases and not meant to be the end all solutions.&lt;/p&gt;  &lt;p&gt;Together with &lt;a href="http://twitter.com/boorad"&gt;Brad Anderson&lt;/a&gt; and &lt;a href="http://twitter.com/voodootikigod"&gt;Chris Williams&lt;/a&gt;, we’ve launched &lt;a href="https://nosqleast.com/2009/"&gt;No:SQL (east) 2009&lt;/a&gt; which aims to present experiences of different companies using these NOSQL solutions in production.&amp;#160; Following the success this summer of the &lt;a href="http://nosql.eventbrite.com/"&gt;NOSQL Meetup in San Francisco&lt;/a&gt;, we bring the discussion back east to Atlanta, GA from October 28th-30th.&amp;#160; A wide array of experiences will be shared on such projects as Cassandra, Redis, MongoDB, CouchDB as well as the Microsoft Research projects &lt;a href="http://research.microsoft.com/en-us/projects/dryad/default.aspx"&gt;Dryad&lt;/a&gt; and &lt;a href="http://research.microsoft.com/en-us/projects/dryadlinq/default.aspx"&gt;DryadLINQ&lt;/a&gt;.&amp;#160; Some of the highlighted sessions include:&lt;/p&gt;  &lt;ul&gt;   &lt;h4&gt;&lt;/h4&gt;    &lt;li&gt;&lt;a href="https://twitter.com/phatduckk"&gt;Arin Sarkissian&lt;/a&gt; (&lt;a href="http://digg.com"&gt;Digg&lt;/a&gt;): &lt;a href="http://incubator.apache.org/cassandra/"&gt;Cassandra&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://twitter.com/kevsmith"&gt;Kevin Smith&lt;/a&gt; (&lt;a href="http://hypotheticalabs.com"&gt;Hypothetical Labs&lt;/a&gt;): &lt;a href="http://code.google.com/p/redis/"&gt;Redis&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://twitter.com/tensigma"&gt;Don Brown&lt;/a&gt; (&lt;a href="http://twitpay.me"&gt;Twitpay&lt;/a&gt;): &lt;a href="http://hadoop.apache.org/pig/"&gt;Pig&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://twitter.com/chriscurtin"&gt;Chris Curtin&lt;/a&gt; (&lt;a href="http://silverpop.com"&gt;Silverpop&lt;/a&gt;): &lt;a href="http://cascading.org"&gt;Cascading&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://twitter.com/hornbeck"&gt;John Hornbeck&lt;/a&gt; (&lt;a href="http://engineyard.com"&gt;Engine Yard&lt;/a&gt;): &lt;a href="http://mongodb.org"&gt;MongoDB&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://twitter.com/mlmilleratmit"&gt;Mike Miller&lt;/a&gt; (&lt;a href="http://cloudant.com"&gt;Cloudant&lt;/a&gt;): &lt;a href="http://couchdb.apache.org"&gt;CouchDB&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://twitter.com/moonpolysoft"&gt;Cliff Moon&lt;/a&gt; (&lt;a href="http://powerset.com"&gt;Microsoft / Powerset&lt;/a&gt;): &lt;a href="http://wiki.github.com/cliffmoon/dynomite"&gt;Dynomite&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://twitter.com/justinsheehy"&gt;Justin Sheehy&lt;/a&gt; (&lt;a href="http://basho.com"&gt;Basho&lt;/a&gt;): &lt;a href="http://riak.basho.com"&gt;Riak&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://twitter.com/markgunnels"&gt;Mark Gunnels&lt;/a&gt; (&lt;a href="http://catamorphiclabs.com"&gt;Catamorphic Labs&lt;/a&gt;): &lt;a href="http://hadoop.apache.org/hbase/"&gt;HBase&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://twitter.com/timanglade"&gt;Tim Anglade&lt;/a&gt; (&lt;a href="http://af83.com"&gt;af83&lt;/a&gt;): &lt;a href="http://github.com/timanglade/tin/"&gt;tin&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://twitter.com/emileifrem"&gt;Emil Eifrem&lt;/a&gt; (&lt;a href="http://neotechnology.com/"&gt;Neo Technology&lt;/a&gt;): &lt;a href="http://neo4j.org/"&gt;Neo4j&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://research.microsoft.com/en-us/people/yuanbyu/"&gt;Yuan Yu&lt;/a&gt; (&lt;a href="http://research.microsoft.com/"&gt;Microsoft Research&lt;/a&gt;): &lt;a href="http://research.microsoft.com/en-us/projects/dryad/default.aspx"&gt;Dryad&lt;/a&gt; / &lt;a href="http://research.microsoft.com/en-us/projects/dryadlinq/default.aspx"&gt;DryadLINQ&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://twitter.com/geirmagnusson"&gt;Geir Magnusson&lt;/a&gt; (&lt;a href="http://www.gilt.com"&gt;Gilt Groupe&lt;/a&gt;): &lt;a href="http://project-voldemort.com/"&gt;Voldemort&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;More information, including registration, can be found at the site: &lt;a title="https://nosqleast.com/2009/" href="https://nosqleast.com/2009/"&gt;https://nosqleast.com/2009/&lt;/a&gt;.&amp;#160; Hope to see you there!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=344888" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/Ldv7KOLL0R8" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Conferences/default.aspx">Conferences</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/NOSQL/default.aspx">NOSQL</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/10/no-sql-east.aspx</feedburner:origLink></item><item><title>[ANN] DC ALT.NET 9/10/2009 – Webmachine with Kevin Smith</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/gojkeRTk6Gk/ann-dc-alt-net-9-10-2009-webmachine-with-kevin-smith.aspx</link><pubDate>Fri, 04 Sep 2009 19:27:31 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:338830</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>1</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=338830</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/04/ann-dc-alt-net-9-10-2009-webmachine-with-kevin-smith.aspx#comments</comments><description>&lt;p&gt;This month, &lt;a href="http://tech.groups.yahoo.com/group/dcaltnet/"&gt;DC ALT.NET&lt;/a&gt; is moving the meeting a little forward this month and teaming with &lt;a href="http://groups.google.com/group/novalanguages"&gt;NOVALANG&lt;/a&gt; and the &lt;a href="http://www.meetup.com/erlang/"&gt;Erlang Users of Arlington/DC&lt;/a&gt; to talk about the Webmachine REST Toolkit.&amp;#160; We at DC ALT.NET continue to strive to push developers to look outside their normal surroundings and comfort zones and this month is no different.&amp;#160; Details are below: &lt;/p&gt;  &lt;h2&gt;Getting Some REST with Erlang and Webmachine &lt;/h2&gt;  &lt;p&gt;Kevin Smith of &lt;a href="http://hypotheticalabs.com/"&gt;Hypothetical Labs&lt;/a&gt; will be speaking about &lt;a href="http://bitbucket.org/justin/webmachine/wiki/Home"&gt;WebMachine&lt;/a&gt;: an erlang REST toolkit which makes it easy to develop HTTP interfaces using Erlang.&amp;#160; Providing RESTful interfaces is quickly becoming the norm for modern websites. There&amp;#39;s more to REST than handling GETs and POSTs, though. This talk will illustrate how the combination of Erlang and webmachine simplifies and speeds the development of REST resources. &lt;/p&gt;  &lt;h2&gt;About Kevin Smith&lt;/h2&gt;  &lt;p&gt;Kevin Smith has, at various times, been a network administrator, DBA, developer, team lead and trainer over his 14 year career. He ﬁrst learned about Erlang in 2006 via Joe Armstrong&amp;#39;s excellent “Programming Erlang” and has never looked back. He attended the first (and only?) Erlang Studio where he was asked to produce what became the popular PragProg screencast series &amp;quot;Erlang In Practice&amp;quot;. Until recently, Kevin was a developer at Engine Yard where he worked on a distributed systems management application using Erlang. Kevin founded &lt;a href="http://www.hypotheticalabs.com/"&gt;Hypothetical Labs&lt;/a&gt; in April of this year to focus on Erlang full time. He is also the co-author of the upcoming book &amp;quot;Erlang Web Application Development&amp;quot;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Date/Time:&lt;/strong&gt; 9/10/2009 – 7-9PM&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Location:      &lt;br /&gt;&lt;/strong&gt;The Motley Fool     &lt;br /&gt;2000 Duke Street     &lt;br /&gt;Alexandria, VA 22314&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.eventbrite.com/event/418055415?ref=ebtn" target="_blank"&gt;&lt;img style="border-bottom:0px;border-left:0px;border-top:0px;border-right:0px;" border="0" src="http://www.eventbrite.com/registerbutton?eid=418055415" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=338830" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/gojkeRTk6Gk" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/ALT.NET/default.aspx">ALT.NET</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/User+Groups/default.aspx">User Groups</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Web+Development/default.aspx">Web Development</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Erlang/default.aspx">Erlang</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/04/ann-dc-alt-net-9-10-2009-webmachine-with-kevin-smith.aspx</feedburner:origLink></item><item><title>F# First Class Events – Async Workflows + Events Part III</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/f1rP95DboeQ/f-first-class-events-async-workflows-events-part-iii.aspx</link><pubDate>Fri, 04 Sep 2009 04:49:19 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:338712</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>1</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=338712</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/04/f-first-class-events-async-workflows-events-part-iii.aspx#comments</comments><description>&lt;p&gt;So far in this series, I’ve covered a bit about what first class events are in F# and how you might use them.&amp;#160; In the first post, we looked at what a first class events mean and some basic combinators in order to compose events together.&amp;#160; In the second post, we looked at how we might create events and publish them to the world through classes.&amp;#160; And in the third post I talked about how to manage the lifetime of a subscription.&amp;#160; In the fourth installment, I corrected my usage of the old create function and instead to use the Event class to create, trigger and publish events.&amp;#160; In the last part, we’ve been talking about asynchronous workflows and eventing together, and this time we’ll pick up on that discussion.&amp;#160; Before we get started, let’s get caught up to where we are today.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/11/first-class-composable-events-in-f.aspx"&gt;Part 1 – First Class Events&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/18/f-first-class-events-creating-events.aspx"&gt;Part 2 – Creating Events&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/21/f-first-class-events-creating-and-disposing-handlers.aspx"&gt;Part 3 – Creating and Disposing Handlers&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/27/f-first-class-events-changes-on-creating-events.aspx"&gt;Part 4 – Changes on Creating Events&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/31/f-first-class-events-async-workflows-events-part-i.aspx"&gt;Part 5 – Async Workflows + Events Part I&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/02/f-first-class-events-async-workflows-events-part-ii.aspx"&gt;Part 6 – Async Workflows + Events Part II&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Converting Events to Asynchronous Operations&lt;/h2&gt;  &lt;p&gt;One of the last topics to be covered in this section involves the question, “How do we take an event and properly manage the cancellation and error checking?”&amp;#160; Earlier I showed about how we could take the WebClient’s DownloadStringAsync event and partition it into three different events based upon the outcome of the main event.&amp;#160; This time, we’re going to take that event and turn it into an asynchronous operation with proper error checking and cancellation checking.&lt;/p&gt;  &lt;p&gt;In order to make this happen, we need to use the AwaitEvent extension method on the Async&amp;lt;T&amp;gt; class.&amp;#160; Let’s take a first glance at how to do this with a BackgroundWorker class.&amp;#160; This class allows us to do potentially expensive operation in the background and report progress along the way.&amp;#160; Of interest to us is the RunWorkerCompleted event and the RunWorkerAsync method in trying to wrap that in such a way that we can check for errors and cancellations.&amp;#160; First, let’s create an extension method to the BackgroundWorker class to allow for the Async&amp;lt;T&amp;gt; binding.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:849d1776-4de4-4572-82d4-3979eba64a80" class="wlWriterSmartContent"&gt;   &lt;pre style="background-color:#ffffff;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.ComponentModel

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; BackgroundWorker &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.AsyncRunWorker(&lt;/span&gt;&lt;span style="color:#000000;"&gt;?&lt;/span&gt;&lt;span style="color:#000000;"&gt;argument:obj) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    async { &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Run the worker asynchronously&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;            &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; arg &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; defaultArg argument &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;
            this.RunWorkerAsync(arg)
              
            &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Wait for the event to happen&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;            &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; args &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async.AwaitEvent(this.RunWorkerCompleted, 
                                         cancelAction&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;this.CancelAsync)
              
            &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Base our result on what happened&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;            &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; result &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
              &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.Cancelled &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt;
                AsyncCanceled (&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; OperationCanceledException())
              &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;elif&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.Error &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt; AsyncException args.Error
              &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;  AsyncOk args.Result 
            &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; AsyncResult.Commit(result) } &lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;What I’ve done is to create an extension method called AsyncRunWorker which has an optional argument to send to the worker.&amp;#160; I first call the RunWorkerAsync with our argument (null if omitted), then I wait for the RunWorkerCompleted event to fire with our cancel action.&amp;#160; This cancel action allows us to specify some behavior that happens should the operation somehow be canceled.&amp;#160; Next, we base our result upon whether we have a cancellation, an error or lastly, we have a result.&amp;#160; Then we commit that answer as our result.&amp;#160; This function, when called, will produce and Async&amp;lt;obj&amp;gt; as that is the signature of the args.Result.&lt;/p&gt;

&lt;p&gt;We can test the behavior of this by creating a simple worker and running it inside of an async workflow.&amp;#160; In order to do so, we’ll put in a little bit of infrastructure that we had in a previous post talking about subscribing to events.&amp;#160; This way, we can unsubscribe from our event automatically at the end of our async workflow through the use of a using scope.&amp;#160; &lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:61379712-8e0e-48ad-a786-79d72233b134" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;[&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;AutoOpen&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;]
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;module&lt;/span&gt;&lt;span style="color:#000000;"&gt; EventExtensions &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System

  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDelegateEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Del when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Del :&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Delegate &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.Subscribe(d) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      this.AddHandler(d)
      { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; IDisposable &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
          &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; disp.Dispose() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
            this.RemoveHandler(d) }&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;After we’ve defined this, let’s go ahead and now define our async workflow with the BackgroundWorker defined.&amp;#160; In this instance, we’ll create a simple background process that waits for a little bit and then formats the argument into a string as the result.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5dabbff5-6675-4900-94b1-13b8ec1c8ffe" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; workerResult (worker:BackgroundWorker) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  async { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;use&lt;/span&gt;&lt;span style="color:#000000;"&gt; e &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; worker.DoWork.Subscribe(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; _ args &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
                                  Thread.Sleep(&lt;/span&gt;&lt;span style="color:#800080;"&gt;5000&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
                                  args.Result &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; sprintf &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Hello %A&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.Argument)
          &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; worker.AsyncRunWorker (&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Matt&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) }
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async.RunSynchronously
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In this instance, we created a function which takes a BackgroundWorker and inside the async workflow, we subscribe to the DoWork event to sleep for a bit, and then format a string with the argument.&amp;#160; At the end of the async block, I invoke the AsyncRunWorker extension method with my first name as an argument.&amp;#160; Finally, I run it synchronously just to force the evaluation.&amp;#160; Had I been using .NET 4.0, I could have used a Task&amp;lt;T&amp;gt; instead and invoked it as a future.&amp;#160; I’ll cover how that works in a future post.&amp;#160; &lt;/p&gt;

&lt;p&gt;Now that we have our function defined for handling a BackgroundWorker inside the async workflow, let’s tie it all together.&amp;#160; First, we’ll create the worker which supports cancellation.&amp;#160; One item of note is that I included below some pragmas for fixing some thread blocking issues when using F# interactive around the use of SynchronizatonContexts and the Async.RunSynchronously.&amp;#160; If this code is compiled and run as an executable, then it won’t be affected.&amp;#160; I invoke the worker three times and print out the result of each.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6279f6d5-5d07-4769-8480-a7b0cdb66f76" class="wlWriterSmartContent"&gt;
  &lt;pre style="background-color:#ffffff;overflow:auto;"&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; worker &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; BackgroundWorker(WorkerSupportsCancellation &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;)

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;#if&lt;/span&gt;&lt;span style="color:#000000;"&gt; INTERACTIVE&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; context &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SynchronizationContext.Current
SynchronizationContext.SetSynchronizationContext(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;#endif&lt;/span&gt;&lt;span style="color:#000000;"&gt;

workerResult worker &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;%A&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
workerResult worker &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;%A&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
workerResult worker &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;%A&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;#if&lt;/span&gt;&lt;span style="color:#000000;"&gt; INTERACTIVE&lt;/span&gt;&lt;span style="color:#000000;"&gt;
SynchronizationContext.SetSynchronizationContext(context)
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;#endif&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;do&lt;/span&gt;&lt;span style="color:#000000;"&gt; ()&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It’s an interesting exercise, but how about something a bit more useful?&amp;#160;&amp;#160; &lt;/p&gt;

&lt;h2&gt;Extending WebClient&lt;/h2&gt;

&lt;p&gt;In this case, let’s look at extending the System.Net.WebClient to also support F# async workflows for such things as asynchronously opening a reader of a given URL.&amp;#160; If you’ll look at the F# PowerPack library, it already has an extension method for asynchronously downloading a string, such as HTML from a website in WebClient.AsyncDownloadString.&amp;#160; For my example, let’s try the same approach from above to apply it to our situation:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e3ff460f-52a3-4d05-8147-744d016303a1" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.IO
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Net
 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; WebClient &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.AsyncOpenRead (address:Uri) : Async&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Stream&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    async { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; userToken &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;obj&lt;/span&gt;&lt;span style="color:#000000;"&gt;()
            this.OpenReadAsync(address, userToken)

            &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; Loop until we see a reply with the same token&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; rec loop() &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
              async { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; args &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
                Async.AwaitEvent(this.OpenReadCompleted,
                                 cancelAction&lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;this.CancelAsync)
                      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.UserState &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; userToken &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; loop()
                      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;
                        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; result &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
                          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.Cancelled &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
                            AsyncCanceled (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; OperationCanceledException())
                          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;elif&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.Error &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;then&lt;/span&gt;&lt;span style="color:#000000;"&gt; AsyncException args.Error
                          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;  AsyncOk args.Result 
                        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; AsyncResult.Commit(result) }
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; loop() }&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this case, we’re going to create a token in which we can track our particular instance of the async operation.&amp;#160; We call the OpenReadAsync method with our address and our token, and then we loop until we get the callback with our associated token.&amp;#160; Once we do, then we set the result much as we did above.&amp;#160; You’ll notice a familiar trend that most of the async operations on the WebClient can be extended the same way this one was, such as OpenWrite, DownloadXXX and UploadXXX.&lt;/p&gt;

&lt;p&gt;We can now download data from a reader as we have in this example below:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:11bbf7ce-61c2-4a68-bb53-81900ac38420" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.IO
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Net
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Threading

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; readHtml (address : Uri) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  async { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; wc &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; WebClient()
          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;use&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; stream &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; wc.AsyncOpenRead(address)
          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;use&lt;/span&gt;&lt;span style="color:#000000;"&gt; reader &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; StreamReader(stream)
          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; reader.AsyncReadToEnd() }

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;#if&lt;/span&gt;&lt;span style="color:#000000;"&gt; INTERACTIVE&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; context &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SynchronizationContext.Current
SynchronizationContext.SetSynchronizationContext(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;#endif&lt;/span&gt;&lt;span style="color:#000000;"&gt;

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; html &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
  readHtml(Uri &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;http://bing.com&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;) 
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async.RunSynchronously

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;#if&lt;/span&gt;&lt;span style="color:#000000;"&gt; INTERACTIVE&lt;/span&gt;&lt;span style="color:#000000;"&gt;
SynchronizationContext.SetSynchronizationContext(context)
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;#endif&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;do&lt;/span&gt;&lt;span style="color:#000000;"&gt;()&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So, as you can see from above, we simply read the HTML from a page using the WebClient approach instead of the WebRequest way using async workflows.&amp;#160; Once again, we have to deal with the context issues only during F# Interactive.&amp;#160; &lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;From these two examples, we can see how you can take programming models built around events and turn them into async functions that can easily interact with F#’s async workflows.&amp;#160; I think over this past series we’ve covered quite a bit dealing with first class events in F#.&amp;#160; In the next series, we’ll start to look at the Reactive Framework and new ways of thinking around handling events.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=338712" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/f1rP95DboeQ" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Event-based+Porgramming/default.aspx">Event-based Porgramming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/04/f-first-class-events-async-workflows-events-part-iii.aspx</feedburner:origLink></item><item><title>F# First Class Events – Async Workflows + Events Part II</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/8AkclJqD-lc/f-first-class-events-async-workflows-events-part-ii.aspx</link><pubDate>Wed, 02 Sep 2009 15:55:56 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:335808</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>4</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=335808</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/02/f-first-class-events-async-workflows-events-part-ii.aspx#comments</comments><description>&lt;p&gt;So far in this series, I’ve covered a bit about what first class events are in F# and how you might use them.&amp;#160; In the first post, we looked at what a first class events mean and some basic combinators in order to compose events together.&amp;#160; In the second post, we looked at how we might create events and publish them to the world through classes.&amp;#160; And in the third post I talked about how to manage the lifetime of a subscription.&amp;#160; In the fourth installment, I corrected my usage of the old create function and instead to use the Event class to create, trigger and publish events.&amp;#160; Last time, we’ll look at how we can use first class events inside Async Workflows in order to do such items as tracking state.&amp;#160; This time, let’s look at how we could use the Async Workflows together with events in order to draw on a WPF window.&amp;#160; Before we get started, let’s get caught up to where we are today.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/11/first-class-composable-events-in-f.aspx"&gt;Part 1 – First Class Events&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/18/f-first-class-events-creating-events.aspx"&gt;Part 2 – Creating Events&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/21/f-first-class-events-creating-and-disposing-handlers.aspx"&gt;Part 3 – Creating and Disposing Handlers&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/27/f-first-class-events-changes-on-creating-events.aspx"&gt;Part 4 – Changes on Creating Events&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/31/f-first-class-events-async-workflows-events-part-i.aspx"&gt;Part 5 – Async Workflows + Events Part I&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Drawing the Easy Way&lt;/h2&gt;  &lt;p&gt;In our last post, we had a simple click tracker, which in itself is interesting, but not all that useful.&amp;#160; This time, let’s take another approach and create a little drawing application in WPF using the async workflows and events together.&amp;#160; In order to do so, let’s get a little infrastructure out of the way before we get to the real heart of the matter.&amp;#160; First, we need the ability to grab the position of our mouse at any given time.&amp;#160; In order to do so, we’ll get the position relative to our given input element.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:86713f8e-36e0-49f5-8b11-133ee6c7bd76" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Threading
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows.Controls
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows.Input
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows.Markup
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows.Media
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows.Shapes

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; getPosition (e : #IInputElement) (args : #MouseEventArgs) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; pt &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.GetPosition e
  (pt.X, pt.Y)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code is rather straight forward which simply gets the position of our mouse relative to our input element.&amp;#160; Next, we need to create an event which fires while our left mouse button is down and our mouse is moving.&amp;#160; In order to do so, we’ll merge the MouseLeftButtonDown and MouseMove events from our given input element so that it only fires when both happen.&amp;#160; After we define the event, we’ll create an extension method on the IInputElement interface to expose this event.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:48ea2c3b-ba48-46cc-b51e-3c5da6846e4a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; createMouseTracker (e : #IInputElement) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
  e.MouseLeftButtonDown
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event.map (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; args &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; args :&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; MouseEventArgs)
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event.merge e.MouseMove
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event.filter (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; args &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.LeftButton &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; MouseButtonState.Pressed)
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event.map (getPosition e)

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows.IInputElement &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;with&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; this.MouseTrack &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; createMouseTracker this&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The function we defined above first captures the MouseLeftButtonDown event, and then we want to downcast the MouseButtonEventArgs down to the MouseEventArgs so that we can merge it with the MouseMove event, due to the fact that both events must have the same type of arguments.&amp;#160; After that, we ensure that we’re firing only when the left mouse button is down, and then finally we map our mouse event arguments to obtain only the x and y coordinates.&lt;/p&gt;

&lt;p&gt;After defining our event and exposing it as an extension method, we need a way to draw a line from one coordinate to the next.&amp;#160; In order to do so, we’ll simply need our from coordinates, our to coordinates and our element which holds it.&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f298e470-c4c7-4134-9f97-551b5e4cfb04" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; drawLine stroke (x1, y1) (x2, y2) (e : #IAddChild) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; line &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; LineGeometry(StartPoint &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Point(x1, y1), EndPoint &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Point(x2, y2))
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; path &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Path(Stroke &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; stroke, StrokeThickness &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;5&lt;/span&gt;&lt;span style="color:#000000;"&gt;., Data &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; line)
  e.AddChild(path)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Our draw line function from above simply creates a LineGeometry with our starting and end points, then we create a Path to store our line and brush data, and finally add it to our element.&amp;#160; Now, to the heart of the matter.&amp;#160; How might we track our mouse using these Async Workflows and events together?&amp;#160; In the last post, we looked at using the recursive loops to store state, such as the number of times a button was clicked.&amp;#160; This time, we can store the previous coordinates as our loop argument, but in order to make this work, we need to fire it one time and then initialize the loop.&amp;#160; Let’s look at the code and go into detail below:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:aaaa446c-647b-4941-9122-6e4c5115504f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; trackMouse (e : #UIElement) (guiContext : SynchronizationContext) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;rec&lt;/span&gt;&lt;span style="color:#000000;"&gt; firstEvent () &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    async { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; args &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async.AwaitEvent e.MouseTrack
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; loop args } 
  and loop prev &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    async { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; current &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async.AwaitEvent e.MouseTrack
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;do&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async.SwitchToGuiThread guiContext
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;do&lt;/span&gt;&lt;span style="color:#000000;"&gt; drawLine Brushes.Red prev current e
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;do&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async.SwitchToThreadPool()
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; loop current }
  firstEvent()&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see, our track mouse function takes our element and a synchronization context.&amp;#160; This context allows us to switch back and forth from the thread pool to the GUI thread at any point, which can be crucial for UI rendering.&amp;#160; Our first function initializes the loop function by getting the first instance of the loop with the first coordinates.&amp;#160; In the loop function, we await our instance of the event, we switch to the GUI context, draw our line, switch back to the thread pool and then recurse again to await the next instance of our mouse track event.&amp;#160; Now, let’s tie this whole thing together.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:66f3dca7-1ce8-4ec2-8058-f9f77d196592" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; window &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Window(Visibility &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Visibility.Visible)
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; canvas &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Canvas(RenderSize &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; window.RenderSize, 
                        Background &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Brushes.AliceBlue)
window.Content &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color:#000000;"&gt; canvas
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; guiContext &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SynchronizationContext.Current

trackMouse canvas guiContext &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async.Start&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;First, we create our window which holds the application and then we create a canvas which allows us to draw arbitrary shapes upon it.&amp;#160; After getting the GUI context, we can invoke our trackMouse function with our canvas and our context and then asynchronously starting it.&amp;#160; Our end result looks something like the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_1A7FAF22.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="image" border="0" alt="image" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_5A19E5E7.png" width="463" height="276" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You might imagine that we could indeed change colors as well as part of our state.&amp;#160; Of course there’s an issue with once we lift up our mouse to reset the previous coordinate, but that’s an issue for another post.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Once again, using this naive example, we’re able to see that asynchronous workflows and first class events can indeed work nicely together to do simple things such as drawing.&amp;#160; In the next post, we’ll go after how to handle the cancel events with such things as asynchronously downloading data.&amp;#160; After this, we’re not quite finished here as we have a lot more to cover with event-based programming with such things as the Reactive Framework (Reactive LINQ).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=335808" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/8AkclJqD-lc" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Event-based+Porgramming/default.aspx">Event-based Porgramming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/02/f-first-class-events-async-workflows-events-part-ii.aspx</feedburner:origLink></item><item><title>F# First Class Events – Async Workflows + Events Part I</title><link>http://feedproxy.google.com/~r/MatthewPodwysocki/~3/93ZT6eamEyI/f-first-class-events-async-workflows-events-part-i.aspx</link><pubDate>Mon, 31 Aug 2009 20:37:08 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:332658</guid><dc:creator>Matthew.Podwysocki</dc:creator><slash:comments>4</slash:comments><wfw:commentRss>http://codebetter.com/blogs/matthew.podwysocki/rsscomments.aspx?PostID=332658</wfw:commentRss><comments>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/31/f-first-class-events-async-workflows-events-part-i.aspx#comments</comments><description>&lt;p&gt;So far in this series, I’ve covered a bit about what first class events are in F# and how you might use them.&amp;#160; In the first post, we looked at what a first class events mean and some basic combinators in order to compose events together.&amp;#160; In the second post, we looked at how we might create events and publish them to the world through classes.&amp;#160; And in the third post I talked about how to manage the lifetime of a subscription.&amp;#160; Last time, I corrected my usage of the old create function and instead to use the Event class to create, trigger and publish events.&amp;#160; This time, we’ll look at how we can use first class events inside Async Workflows in order to do such items as tracking state.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/11/first-class-composable-events-in-f.aspx"&gt;Part 1 – First Class Events&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/18/f-first-class-events-creating-events.aspx"&gt;Part 2 – Creating Events&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/21/f-first-class-events-creating-and-disposing-handlers.aspx"&gt;Part 3 – Creating and Disposing Handlers&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/27/f-first-class-events-changes-on-creating-events.aspx"&gt;Part 4 – Changes on Creating Events&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Awaiting Events&lt;/h2&gt;  &lt;p&gt;You may have noticed me talk a bit about asynchronous programming and event-based programming both in F#, but what about bringing the power of both of them together?&amp;#160; What if we could somehow interact with first class events inside of async workflows to wait for an event and then continue when that event happens?&amp;#160; Inside the F# PowerPack, there is an extension method to do exactly that.&amp;#160; The AwaitEvent extension method to the Async&amp;lt;T&amp;gt; class takes an event and an optional cancellation action and returns an async binding for our event so that we can wait for a single invocation of the event and then use its event arguments.&amp;#160; Let’s look at the signature below.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bea10d26-550d-4534-8581-9b8a42ea1b00" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;type&lt;/span&gt;&lt;span style="color:#000000;"&gt; Microsoft.FSharp.Control.Async&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;T&amp;gt; with &lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;member&lt;/span&gt;&lt;span style="color:#000000;"&gt; AwaitEvent: 
    IEvent&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;*&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;?&lt;/span&gt;&lt;span style="color:#000000;"&gt;cancelAction : (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;unit&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;unit&lt;/span&gt;&lt;span style="color:#000000;"&gt;) &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;T&amp;gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;      when &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate : delegate&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#000000;"&gt;T,&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;unit&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; and &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Delegate :&amp;gt; System.Delegate &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What you’ll notice is that this returns an asynchronous computation and takes the event, which it listens to once and then disposes once it is completed or cancelled.&amp;#160; If the cancellation action is specified and a cancellation occurs, then the function is executed and will continue to wait for the event, however, if not specified, then the computation is immediately halted.&lt;/p&gt;

&lt;p&gt;Typically, when we’re talking about aggregating data as I’ve stated above, we might consider the scan combinator from the Event module.&amp;#160; This allows us, with a given seed, to aggregate data as it comes in.&amp;#160; Given a quick use of a WPF window, it might look like the following:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fad61395-858a-46fe-89ee-a19034fd4c42" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; window &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Window(Visibility &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Visibility.Visible)
window.MouseDown
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event.scan (&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;fun&lt;/span&gt;&lt;span style="color:#000000;"&gt; acc args &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.ClickCount &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; acc) &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Event.listen (printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Clicked %d time(s)&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This above code allows us to count the number of times a mouse is clicked and display that on F# interactive.&amp;#160; Pretty simple code, but how might we handle cancellation or even how to stop listening for this?&amp;#160; Well, given this example, not so easily.&amp;#160; So, how can we use async workflows instead?&amp;#160; Let’s try the above example using the new technique:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bb584675-d4ae-4100-9b65-750dabd9124d" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;open&lt;/span&gt;&lt;span style="color:#000000;"&gt; System.Windows

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; window &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Window(Visibility &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Visibility.Visible)

&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; trackClicks (e : #UIElement) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; rec loop (n:int) &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    async { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; args &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async.AwaitEvent e.MouseDown
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;let&lt;/span&gt;&lt;span style="color:#000000;"&gt; clickCount &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; args.ClickCount &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; n
            printfn &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Clicked %d time(s)&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; clickCount
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt;!&lt;/span&gt;&lt;span style="color:#000000;"&gt; loop clickCount }
  loop &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;

trackClicks window &lt;/span&gt;&lt;span style="color:#000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; Async.Start&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What this code does is first create a WPF window which we’ll use later for tracking the mouse clicks.&amp;#160; Next, we define a function called trackClicks which takes in an a UIElement for which to track mouse clicks.&amp;#160; Inside, we have a loop, because we remember that these event waiting functions only wait one time in which we seed the click count to zero.&amp;#160; Inside of our loop function, we get the arguments from our MouseDown event so that we can get the number of clicks given to us, we increment the click count, print it, and then return to the loop again so that we keep listening for click events.&amp;#160; Finally, we start listening by calling our trackClicks function with our window argument and then starting it asynchronously.&amp;#160; The output from our little application might look like this from F# Interactive just as it should have for the above example:&lt;/p&gt;

&lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:704bf637-4aea-4353-8ab0-991ce88565b1" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;Clicked &lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt; time(s)
Clicked &lt;/span&gt;&lt;span style="color:#800080;"&gt;3&lt;/span&gt;&lt;span style="color:#000000;"&gt; time(s)
Clicked &lt;/span&gt;&lt;span style="color:#800080;"&gt;6&lt;/span&gt;&lt;span style="color:#000000;"&gt; time(s)
Clicked &lt;/span&gt;&lt;span style="color:#800080;"&gt;7&lt;/span&gt;&lt;span style="color:#000000;"&gt; time(s)
Clicked &lt;/span&gt;&lt;span style="color:#800080;"&gt;8&lt;/span&gt;&lt;span style="color:#000000;"&gt; time(s)
Clicked &lt;/span&gt;&lt;span style="color:#800080;"&gt;10&lt;/span&gt;&lt;span style="color:#000000;"&gt; time(s)
Clicked &lt;/span&gt;&lt;span style="color:#800080;"&gt;13&lt;/span&gt;&lt;span style="color:#000000;"&gt; time(s)
Clicked &lt;/span&gt;&lt;span style="color:#800080;"&gt;17&lt;/span&gt;&lt;span style="color:#000000;"&gt; time(s)
Clicked &lt;/span&gt;&lt;span style="color:#800080;"&gt;18&lt;/span&gt;&lt;span style="color:#000000;"&gt; time(s)
Clicked &lt;/span&gt;&lt;span style="color:#800080;"&gt;20&lt;/span&gt;&lt;span style="color:#000000;"&gt; time(s)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We get the same exact behavior as we would above, but yet gives us some more power over such things as cancellation, alternate paths, etc.&amp;#160; &lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;From this naive example, we’re able to see that asynchronous workflows and first class events can indeed work nicely together.&amp;#160; In the next couple of posts, we’ll go deeper into this subject to explore such areas as drawing on a screen as well as downloading data from a site asynchronously.&amp;#160; After this, we’re not quite finished here as we have a lot more to cover with event-based programming with such things as the Reactive Framework (Reactive LINQ).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=332658" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysocki/~4/93ZT6eamEyI" height="1" width="1"/&gt;</description><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Event-based+Porgramming/default.aspx">Event-based Porgramming</category><category domain="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx">Reactive Framework</category><feedburner:origLink>http://codebetter.com/blogs/matthew.podwysocki/archive/2009/08/31/f-first-class-events-async-workflows-events-part-i.aspx</feedburner:origLink></item></channel></rss>
