<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Matthew Podwysocki's Blog</title><link>http://weblogs.asp.net/podwysocki/default.aspx</link><description>Architect, Develop, Inspire...</description><language>en</language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><geo:lat>38.977109</geo:lat><geo:long>-77.385278</geo:long><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/MatthewPodwysockisBlog" type="application/rss+xml" /><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 V</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/e7Tpd2NsxQE/introduction-to-the-reactive-framework-part-v.aspx</link><category>C#</category><category>Concurrency</category><category>Reactive Framework</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Wed, 18 Nov 2009 14:48:00 PST</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7259680</guid><description>&lt;p&gt;In the previous post of the Introduction to the Reactive Framework series, we covered how to create new IObservable&amp;lt;T&amp;gt; instances, either from scratch or from existing sequences.&amp;nbsp; What this allowed us to do was turn an operation which was previously interactive, such as iterating over a collection, to a reactive, where I could have multiple listeners reacting to the same collection concurrently.&amp;nbsp; This has nice implications for allowing us to scale our application in interesting ways.&amp;nbsp; This time, let’s take another angle on making asynchronous programming easier.&lt;/p&gt;

&lt;p&gt;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/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx"&gt;Part I – Introduction&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx"&gt;Part II – Duality of Enumerable/Observable&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/12/introduction-to-the-reactive-framework-part-iii.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/12/introduction-to-the-reactive-framework-part-iii.aspx"&gt;Part III – From Events to Observables&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/15/introduction-to-the-reactive-framework-part-iv.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/15/introduction-to-the-reactive-framework-part-iv.aspx"&gt;Part IV – From Enumerables to Observables&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;From Asynchronous To Observables&lt;/h2&gt;

&lt;p&gt;In the first post in the series, we talked about some of the problems we face with asynchronous programming today.&amp;nbsp; For example, for any given operation, how do we handle exceptions, or handle cancellation, or even manage the lifetime of resources?&amp;nbsp; Let’s take a look at the story of asynchronous programming today.&amp;nbsp; For example, let’s take the fairly straight forward approach of sending a tweet in Twitter, but with the added pressure of non-blocking calls for all IO operations.&amp;nbsp; Now we took an example which should have been straight forward, and now it looks something like this anti-pattern:&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6d00f6bb-d89f-4d21-87e9-284327b63e6b" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; WebRequest.Create(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;http://twitter.com/statuses/update.xml&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
request.Credentials &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; NetworkCredential(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;bar&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
request.Method &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;POST&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;

request.BeginGetRequestStream(rsAr &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; requestStream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.EndGetRequestStream(rsAr);
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; tweet &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HttpUtility.HtmlEncode(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;status=Hello from an async method #bacon&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; bytes &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Encoding.UTF8.GetBytes(tweet);
    requestStream.BeginWrite(bytes, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, bytes.Length, beginWriteAr &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    {
        requestStream.EndWrite(beginWriteAr);
        request.BeginGetResponse(responseAr &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        {
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.EndGetResponse(responseAr);
            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; responseStream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response.GetResponseStream();

            &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; readBuffer &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;byte&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[response.ContentLength];
            responseStream.BeginRead(readBuffer, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, readBuffer.Length, readAr &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
            {
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; read &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; responseStream.EndRead(readAr);
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; readText &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Encoding.UTF8.GetString(readBuffer);
                ResultsText.Text &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; readText;
            }, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
        }, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
    }, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
}, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Not only is this code an absolute mess, but it has a number of bad practices, including not handling cleanup of our resources, as well as not handling exceptions, cancellation checking, or even checking if our buffer was completely read.&amp;nbsp; Adding those constraints to the above code would make it a bit more unreadable.&amp;nbsp; The situation we run into is that programming in a non-blocking fashion has caused all sorts of heartache where we become so wrapped up in the non-blocking code that we lose sight easily of the domain logic we should have been interested in, in the first place.&lt;/p&gt;

&lt;p&gt;I’ve covered in the past that with F# async workflows, solving this is a rather easy task and that kind of rich functionality has been unavailable to C# developers directly.&amp;nbsp; But, with the Reactive Extensions for .NET, we have the ability to also take advantage of composable asynchronous actions as well.&amp;nbsp; So, where do we get started?&lt;/p&gt;

&lt;p&gt;Let’s take a simple example of creating a function and then invoking it asynchronously.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:742903f2-e391-48c3-86d0-554ce2d7c2c3" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Func&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; add &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (x, y) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; x &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; y;

add.BeginInvoke(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;4&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, ar &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; result &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; add.EndInvoke(ar);
    Console.WriteLine(result);
}, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Many asynchronous methods in .NET are written in this way that their signature has a BeginXXX and EndXXX where the XXX is the method name that is being executed asynchronously.&amp;nbsp; The Begin takes the arguments to execute the method, an AsyncCallback which is an action that takes an IAsyncResult and returns nothing, and finally object state.&amp;nbsp; The End method takes the IAsyncResult which is passed in from the AsyncCallback in order to retrieve the value of the asynchronous call.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7b4bfa8d-cb53-4313-8e25-6ab5c1244331" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; BeginXXX(args, AsyncCallback, state);

Value EndXXX(IAsyncResult);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Luckily, the Reactive Extensions give us a couple of ways to make this happen.&amp;nbsp; First, let’s take an existing method such as the System.IO.Stream.WriteAsync.&amp;nbsp; In this example, we’ll create an extension method which allows us to turn this asynchronous method into a IObservable&amp;lt;T&amp;gt;.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:66b21f24-0090-4362-a498-9b8fd0e8eddb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IObservable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Unit&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; WriteAsync(
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Stream stream, 
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;byte&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[] buffer, 
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; offset, 
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; count)
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Observable.FromAsyncPattern&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;byte&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[], &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(
        stream.BeginWrite,
        stream.EndWrite)(buffer, offset, count);
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Because the Write method returns void, we have issues in C# as we cannot have a generic type as being void.&amp;nbsp; To get around this, much like F#, the Reactive Extensions introduced the Unit type to express that we have no value.&amp;nbsp; Quite frankly this is the way I think C# should have been to allow for this and not need these unnecessary hacks.&amp;nbsp; Anyways, the Observable class has a method called FromAsyncPattern which takes in both the BeginXXX and EndXXX and returns a Func delegate that takes the arguments required to execute it.&amp;nbsp; In this case, the Observable.FromAsyncPattern returns a Func which takes the buffer, offset and count and returns an IObservable&amp;lt;Unit&amp;gt;.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;There is also another way in that we can take our existing add function from above and using an extension method called ToAsync, can also work the IObservable magic.&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5840af4f-e707-4492-88b1-bbb3f46abb9c" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Func&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; add &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (x, y) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; x &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; y;
Func&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, IObservable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; obvervableAdd &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; add.ToAsync();

IObservable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; result &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; added &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; obvervableAdd(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;4&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
                          &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; added;
IDisposable sub &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; result
    .SubscribeOnDispatcher()
    .Subscribe(added &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Results.Text &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; added.ToString());&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;In this instance, we took our existing add method and then turned it into an asynchronous method by using the ToAsync extension method.&amp;nbsp; That returns a Func delegate which takes our two arguments and returns an IObservable&amp;lt;int&amp;gt; which contains our asynchronous result.&amp;nbsp; We can then take our new observableAdd and execute it inside of a LINQ statement, then subscribe to it on the Dispatcher thread in order to rid ourselves of synchronization context issues in WPF.&lt;/p&gt;

&lt;p&gt;Going back to our original example here, let’s see how we might follow some of the ideas presented here into creating a way to send a tweet completely in a LINQ statement.&amp;nbsp; &lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e04b7ec5-7fd2-435f-b65a-526ac5711c24" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; tweetObservable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
        CreateWebRequest(
            &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;http://twitter.com/statuses/update.xml&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, 
            userName, 
            password, 
            &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;POST&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; tweet &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; HttpUtility.HtmlEncode(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;status= Hi! #bacon&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; buffer &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Encoding.UTF8.GetBytes(tweet)
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; _ &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.SetContentLength(buffer.Length)
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; requestStream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.GetRequestStreamAsync()
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; __ &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; requestStream.WriteAsync(buffer, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, buffer.Length)
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.GetResponseAsync()
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; responseStream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response.GetResponseStream()
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; StreamReader(responseStream)
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader.ReadToEnd();
    
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; tweetsObservable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; tweetObservable
    .Throttle(TimeSpan.FromMinutes(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;))
    .Repeat(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;25&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;We can create extension methods to the WebRequest for GetRequestStream and GetResponse to enable observable behavior via the Begin/End methods that the WebRequest exposes.&amp;nbsp; As well, we can extend the System.IO.Stream as well to both write and read streams asynchronously.&amp;nbsp; We have other more trivial methods such as setting the content length and creating a web request as an observable as well, and in order to do that was covered in the previous post using the Observable.Create method.&amp;nbsp; After we create this IObservable value, we can then throttle it to every three minutes and to repeat 25 times.&amp;nbsp; One concern we might have is around the resource usage and ensuring the cleanup, and we’ll cover that next time.&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 as well as managing resources, and turning Tasks Observables.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7259680" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=e7Tpd2NsxQE:5d_oLAaxvos:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=e7Tpd2NsxQE:5d_oLAaxvos:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=e7Tpd2NsxQE:5d_oLAaxvos:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=e7Tpd2NsxQE:5d_oLAaxvos:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=e7Tpd2NsxQE:5d_oLAaxvos:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=e7Tpd2NsxQE:5d_oLAaxvos:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/e7Tpd2NsxQE" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7259680</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/11/18/introduction-to-the-reactive-framework-part-v.aspx</feedburner:origLink></item><item><title>Reactive Extensions for .NET (Rx) released on DevLabs</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/mP91j_lRtV8/reactive-extensions-for-net-rx-released-on-devlabs.aspx</link><category>C#</category><category>F#</category><category>Event-based Programming</category><category>Reactive Framework</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Tue, 17 Nov 2009 14:51:00 PST</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7258361</guid><description>&lt;p&gt;As you may have noticed, I’ve been blogging a bit about the &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/tags/Reactive+Framework/default.aspx"&gt;Reactive Framework&lt;/a&gt;, now the Reactive Extensions for .NET (Rx) and how it will change the way we approach asynchronous and event-based programming.&amp;nbsp; Just as LINQ to Objects changed the way we deal with collections, The Reactive Extensions will change the way we deal with asynchronous and event-based programming.&amp;nbsp; Fast forward to today, &lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Rx has been released on Microsoft DevLabs&lt;/a&gt; for not only .NET 4 Beta 2, but also .NET 3.5 SP1 and Silverlight 3 as well.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;If you’re not familiar with Rx, it is a library for composing asynchronous and event based programs together using observable collections (IObservable&amp;lt;T&amp;gt;).&amp;nbsp; This is especially important in the development of Silverlight, which disallows blocking networking and threading calls from happening and instead forces you into an asynchronous model, but just as well, it’s quite useful for other applications such as traditional desktop applications and web applications.&amp;nbsp; With that comes its own set of challenges.&amp;nbsp; How do I compose these asynchronous events together?&amp;nbsp; How do I handle exceptions?&amp;nbsp; How about cancellation?&lt;/p&gt;

&lt;p&gt;The Reactive Extensions, the contain a superset of the standard LINQ operators we’ve come to know and use quite frequently in our applications which are based not off the IEnumerable&amp;lt;T&amp;gt;, but instead IObservable&amp;lt;T&amp;gt;.&amp;nbsp; The IObservable&amp;lt;T&amp;gt;/IObserver&amp;lt;T&amp;gt;, being the mathematical dual of IEnumerable&amp;lt;T&amp;gt;/IEnumerator&amp;lt;T&amp;gt; allows us to go from the pull model to the push model with ease.&amp;nbsp; With that, we can create abstractions over asynchronous methods, events, Parallel Extensions for .NET Tasks, StreamInsight, F# First Class Events and more.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Download it today&lt;/a&gt; and give the team feedback.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7258361" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mP91j_lRtV8:O-_9Cd_dIAc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mP91j_lRtV8:O-_9Cd_dIAc:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=mP91j_lRtV8:O-_9Cd_dIAc:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mP91j_lRtV8:O-_9Cd_dIAc:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mP91j_lRtV8:O-_9Cd_dIAc:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=mP91j_lRtV8:O-_9Cd_dIAc:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/mP91j_lRtV8" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7258361</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/11/17/reactive-extensions-for-net-rx-released-on-devlabs.aspx</feedburner:origLink></item><item><title>Introduction to the Reactive Framework Part IV</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/daC7Yk74l-Y/introduction-to-the-reactive-framework-part-iv.aspx</link><category>C#</category><category>Event-based Programming</category><category>Reactive Framework</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Sun, 15 Nov 2009 15:55:00 PST</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7255978</guid><description>&lt;p&gt;In the previous post in this series, we covered how to turn .NET events into first class values through IObservable instances.&amp;nbsp; By doing so, we were able to do much more interesting things than just subscribe and unsubscribe, instead we were able to create a mouse drag event with little effort through composition.&amp;nbsp; In this post, let’s look at going from push to pull by turning collections into IObservable&amp;lt;T&amp;gt; instances.&lt;/p&gt;

&lt;p&gt;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/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx"&gt;Part I – Introduction&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx"&gt;Part II – Duality of Enumerable/Observable&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/12/introduction-to-the-reactive-framework-part-iii.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/12/introduction-to-the-reactive-framework-part-iii.aspx"&gt;Part III – From Events to Observables&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;From IEnumerables To Observables&lt;/h2&gt;

&lt;p&gt;As you may remember from the first post and second post in this series, we covered going from IEnumerable&amp;lt;T&amp;gt; to IObservable&amp;lt;T&amp;gt; and the duality between the two.&amp;nbsp; But, how can we switch back and forth between them?&amp;nbsp; Remember that we can create an IObservable from a couple of angles.&amp;nbsp; Let’s first create one by using the chaining together Empty and StartsWith combinators on the Observable class to create an IObservable&amp;lt;T&amp;gt;.&amp;nbsp; Note that this is not the publicly available release of the Reactive Framework, so bear with any name changes.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:30e46f2e-546c-4f63-8d74-f1342c3681a5" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Observable.Empty&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;()
    .StartWith(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
    .StartWith(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
    .StartWith(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This allows me to start out with an empty IObservable&amp;lt;int&amp;gt; and then pre-append the value to the front of the instance, so in this case it would render 3, 2, 1.&amp;nbsp; Next, let’s look at how we might generate a sequence IObservable from 1 to 10.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c0eff709-5d87-4b5d-93ae-8fbd7c8f23b4" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Observable.Generate(
    &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,                        &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Initial value&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,    &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Predicate&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; value,            &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Selector&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);    &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Iterator&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;observable.Subscribe(Console.WriteLine);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;In this instance, we supply an initial value, then a predicate to determine when to stop, a result selector, and then finally an iterator.&amp;nbsp; There are plenty of overloads as well on the Generate to suit your needs when creating IObservable&amp;lt;T&amp;gt; values.&amp;nbsp; But, what about taking existing IEnumerable&amp;lt;T&amp;gt; values and converting them to IObservable&amp;lt;T&amp;gt; values?&amp;nbsp; One could use the the Create function which allows us to create arbitrary IObservable&amp;lt;T&amp;gt; instances.&amp;nbsp; &lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c928ec0b-99da-4182-b765-9f79f6dd8748" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; enumerable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Observable.Create&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(observer &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;try&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; item &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; enumerable)
            observer.OnNext(item);
        observer.OnCompleted();
    }
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;catch&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (Exception exception)
    {
        observer.OnError(exception);
    }

    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; () &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; { };
});
observable.Subscribe(
    value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Console.WriteLine(value),
    exn   &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Console.WriteLine(exn.ToString()));&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What we’re doing in the above code is using the Create method which gives us an IObserver&amp;lt;T&amp;gt; that we can post values to.&amp;nbsp; In this case, we’re iterating through a collection and for each item, we’re calling OnNext.&amp;nbsp; At the end of the iteration, we call the OnCompleted method which indicates that there are no more values.&amp;nbsp; If there were some sort of exception when iterating the values, it would be caught and the exception posted to the observer.&amp;nbsp; We can then decide how to handle it during our Subscribe method call, if at all.&amp;nbsp; Finally, we return an Action which is to be called once we unsubscribe from the given IObserver&amp;lt;T&amp;gt;, which in this instance, we do nothing.&amp;nbsp; That’s all fine and good, but there is an easier way of course, by using the ToObservable extension method.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0c2f0204-ae18-45bc-b489-c828f699bf53" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; enumerable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; enumerable.ToObservable();
observable.Subscribe(Console.WriteLine);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Going from IEnumerable&amp;lt;T&amp;gt; to IObservable&amp;lt;T&amp;gt; as you an see is quite easy.&amp;nbsp; Just as well, going back is easy as well through either of two methods, one being calling the ToEnumerable extension method, and the other being the GetEnumerator method.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7c1f7a89-5a72-4827-9af4-819f44b622d9" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; To Observable&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; enumerable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Enumerable.Range(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;10&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; enumerable.ToObservable();

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; And back again&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; enumerableAgain &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observable.ToEnumerable();
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; enumerator &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observable.GetEnumerator();&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This ultimately leaves us in control of when we want to switch between the pull and push model, depending on our circumstances.&amp;nbsp; It’s sort of like a buy one get one free in that respect.&amp;nbsp; What makes this interesting is to take a collection and spread it out to multiple instances, for example reading in a file and then you could have multiple subscriptions each process the data in turn.&amp;nbsp; Those possibilities are endless if we want to do certain actions concurrently.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a8e645ee-5a69-423c-971b-4e0dceb0113d" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; text &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; File.ReadAllLines(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;DocumentToProcess.txt&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; textObservable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; text.AsObservable();

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Process concurrently&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; textLetterSub &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; text.Subscribe(CountLetters);
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; textWordSub &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; text.Subscribe(CountWords);
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; textVowelSub &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; text.Subscribe(CountVowels);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;There are also more things to come when dealing with collections that we’ll get into in later posts in this series, but this is enough for now.&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 as well as turning Tasks and Asynchronous Methods into Observables.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7255978" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=daC7Yk74l-Y:doWMx_HMJV0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=daC7Yk74l-Y:doWMx_HMJV0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=daC7Yk74l-Y:doWMx_HMJV0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=daC7Yk74l-Y:doWMx_HMJV0:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=daC7Yk74l-Y:doWMx_HMJV0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=daC7Yk74l-Y:doWMx_HMJV0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/daC7Yk74l-Y" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7255978</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/11/15/introduction-to-the-reactive-framework-part-iv.aspx</feedburner:origLink></item><item><title>Introduction to the Reactive Framework Part III</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/0GmiBFMh5AU/introduction-to-the-reactive-framework-part-iii.aspx</link><category>C#</category><category>Event-based Programming</category><category>Reactive Framework</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Thu, 12 Nov 2009 20:19:00 PST</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7253107</guid><description>&lt;p&gt;In my previous post in the Introduction to the Reactive Framework series, I discussed the duality between the IEnumerable/IEnumerator and IObservable/IObserver and how that enables us to generalize the ideas from LINQ to Objects to LINQ to Observables/Events.&amp;nbsp; After the last post, I was asked how we go from events to Observables, so let’s tackle that in this post.&lt;/p&gt;

&lt;p&gt;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/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/10/19/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx"&gt;Part I – Introduction&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx" mce_href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/11/03/introduction-to-the-reactive-framework-part-ii.aspx"&gt;Part II – Duality of Enumerable/Observable&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;From Events...&lt;/h2&gt;

&lt;p&gt;Let’s start out by looking at the way we’d usually do event-based programming in .NET.&amp;nbsp; First, if we have the need to pass data as arguments to our event, we’ll need to create a class which inherits from EventArgs, and in this case we have a class called SomeEventArgs.&amp;nbsp; This class holds nothing more than just a simple object value.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e12e2e80-0bb7-4138-b3f9-cce8a4073979" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeEventArgs : EventArgs {
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Value { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;set&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; }

    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeEventArgs(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; value) {
        Value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; value
    }
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Then we’d have a class which exposes an event with an EventHandler of our SomeEventArgs.&amp;nbsp; At some point along the way we’d have a method which invokes our event, keeping in mind that we need to check for nulls before invoking.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cad38561-d515-4291-bbcc-6a8e1fd0f73e" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeClass
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;event&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; EventHandler&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeEvent;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;event&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; EventHandler&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OtherEvent;
    
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; InvokeEvent(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; value) {
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; handler &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeEvent;
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(handler &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
            handler(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeEventArgs(value));
    }
    &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Other event invoking code here&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Now to do something useful with it, you can subscribe and unsubscribe by using the += and –= operators.&amp;nbsp; In this case, I actually declared a method called SomeHandler which will react to the event arguments that I give it.&amp;nbsp; &lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:965935b3-ce54-4d98-a324-12e3dbf0b444" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; someClass &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeClass();

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Add handler&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;someClass.SomeEvent &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeHandler;

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Remove handler&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;someClass.SomeEvent &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeHandler;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Our SomeHandler method must follow the signature with the sender as an object and the EventArgs derived class, whatever it might be.&amp;nbsp; At this point we react to anything that we’re subscribed to in this method.&amp;nbsp; &lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:52b5075c-37dc-486a-a84c-92e9c4962726" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeHandler(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; sender, SomeEventArgs e) {
    &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Some handler code&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The problem with this solution is that it isn’t quite compositional.&amp;nbsp; What do I mean by that?&amp;nbsp; What if I want to combine this SomeEvent firing with another event?&amp;nbsp; Or if I want to ensure that an event gets posted on a certain synchronization context?&amp;nbsp; Or if I want to throttle the firing of an event to a certain threshold?&amp;nbsp; All of those are a tad more difficult using standard out of the box .NET events, but, we can with Observables.&amp;nbsp; So, how do we get there?&lt;/p&gt;

&lt;h2&gt;To Observables&lt;/h2&gt;

&lt;p&gt;Unlike F#, we don’t get Observables for free when we use events, so we have a little work ahead of us.&amp;nbsp; In order for us to go from events to Observables , we simply need to use the Observable.FromEvent method.&amp;nbsp; There are several flavors that we can use.&amp;nbsp; The first option is to use a little “magic string” juice to wrap our event in an IObservable.&amp;nbsp; This method takes the object to bind for the event and the event name as a string.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c9edcb3b-eae2-4bac-9444-35daecc57ee1" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; EventExtensions {
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IObservable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Event&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GetSomeEvent(
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeClass someClass) {
    
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Observable.FromEvent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(
        someClass, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;SomeEvent&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This method then returns to us an IObservable&amp;lt;Event&amp;lt;SomeEventArgs&amp;gt;&amp;gt; which allows us to get access to the EventArgs and the Sender through their respective properties on the Event class.&amp;nbsp; Personally, I’m not a fan of magic strings at all and if I can get away with not using them, all the better.&amp;nbsp; Luckily, there are other options.&amp;nbsp; The next option is to use Action&amp;lt;EventHandler&amp;lt;EventArgs&amp;gt;&amp;gt; arguments for both the subscribe and unsubscribe.&amp;nbsp; Keep in mind that this only works for those events which are declared to use EventHandler&amp;lt;TEventArgs&amp;gt;.&amp;nbsp; &lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2385afb4-95b9-4a14-99ae-4a68ce60f160" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; EventExtensions {
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IObservable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Event&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GetSomeEvent(
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SomeClass someClass) {
    
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Observable.FromEvent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;SomeEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(
        h &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; someClass.SomeEvent &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; h,
        h &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; someClass.SomeEvent &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; h);
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;If your given event doesn’t have the signature of EventHandler&amp;lt;TEventArgs&amp;gt;, you can still take advantage of the Reactive Framework as well.&amp;nbsp; In this instance, let’s extend the WPF IInputElement to wrap the MouseMove event as an IObservable&amp;lt;Event&amp;lt;MouseEventArgs&amp;gt;&amp;gt;.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8ba746cd-1754-49bc-956e-eda162de353a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IObservable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Event&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MouseEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GetMouseMove(
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IInputElement inputElement)
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Observable.FromEvent(
        (EventHandler&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MouseEventArgs&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; h) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MouseEventHandler(h),
        h &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inputElement.MouseMove &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; h,
        h &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; inputElement.MouseMove &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; h);
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What this does for us behind the scenes is create an IObservable/IObserver to handle the events as they happen.&amp;nbsp; Each invocation of the event causes the OnNext to be fired with the Event&amp;lt;TEventArgs&amp;gt; argument.&amp;nbsp; In this case, the OnComplete and OnException are not implemented as part of the IObserver instance.&lt;/p&gt;

&lt;h2&gt;An Example&lt;/h2&gt;

&lt;p&gt;Let’s create a little example of drawing on a canvas in WPF.&amp;nbsp; In order to do that, we’ll need not only the MouseMove extension as we have above, but also MouseLeftButtonDown and MouseLeftButtonUp.&amp;nbsp; Each of those extension methods should follow the same pattern as above in terms of needing the extra parameter to convert the EventHandler&amp;lt;MouseButtonEventArgs&amp;gt; to a MouseButtonEventHandler.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;First, we need to capture position of the mouse during our mouse moves.&amp;nbsp; After that, we’ll need to zip together the mouse move and another mouse move that is one ahead in order to capture a delta.&amp;nbsp; Finally, we’ll capture the mouse downs and the mouse deltas until the mouse ups happen.&amp;nbsp; Listed below is code to make that happen.&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5147173a-2e9d-4704-9a65-68927643a5c8" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mouseMoves &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mm &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mainCanvas.GetMouseMove()
                 let location &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mm.EventArgs.GetPosition(mainCanvas)
                 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; { location.X, location.Y};
                 
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mouseDiffs &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mouseMoves
    .Skip(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
    .Zip(mouseMoves, (l, r) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; {X1 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; l.X, Y1 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; l.Y, X2 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; r.X, Y2 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; r.Y});

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mouseDrag &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; _  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mainCanvas.GetMouseLeftButtonDown()
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; md &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mouseDiffs.Until(
                    mainCanvas.GetMouseLeftButtonUp())
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; md;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The code should read rather straight forward as we attach to the mainCanvas object in order to draw.&amp;nbsp; The interesting functions are the mouseDiffs and the mouseDrag.&amp;nbsp; The mouseDiffs takes the mouseMoves and then skips 1 ahead and zips it with another mouseMoves, and projects to hold both left and right values.&amp;nbsp; The mouseDrag function takes events from left mouse button down and then from mouseDiffs until the left mouse button up Observable happens.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;Now that we have our mouseDrag, what can we do with it?&amp;nbsp; Well, let’s go ahead and draw on our canvas:&lt;/p&gt;

&lt;div style="padding: 0px; margin: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:90804030-430c-4f44-93b7-41393630a39d" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; overflow: auto;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mouseSub &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mouseDrag.Subscribe(item &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; line &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Line
                  {
                      Stroke &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Brushes.LightSteelBlue,
                      X1 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; item.X1,
                      X2 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; item.X2,
                      Y1 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; item.Y1,
                      Y2 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; item.Y2,
                      StrokeThickness &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;5&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                  };
    mainCanvas.Children.Add(line);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Our mouseSub subscription creates a new line with our coordinates from zipped mouseDrag Observable and then adds it to our canvas.&amp;nbsp; Consider how little code was required to make this happen, and try doing that with standard .NET events.&amp;nbsp; Quite frankly, this kind of solution makes event-based programming using .NET events expressive enough to be usable.&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;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7253107" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=0GmiBFMh5AU:fwV0JBwoPQ0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=0GmiBFMh5AU:fwV0JBwoPQ0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=0GmiBFMh5AU:fwV0JBwoPQ0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=0GmiBFMh5AU:fwV0JBwoPQ0:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=0GmiBFMh5AU:fwV0JBwoPQ0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=0GmiBFMh5AU:fwV0JBwoPQ0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/0GmiBFMh5AU" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7253107</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/11/12/introduction-to-the-reactive-framework-part-iii.aspx</feedburner:origLink></item><item><title>[ANN] DC ALT.NET – 11/24/2009 “Stop Talking, Start Teaching” with Jeff Casimir</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/Bj5RaqMo828/ann-dc-alt-net-11-24-2009-stop-talking-start-teaching-with-jeff-casimir.aspx</link><category>ALT.NET</category><category>User Groups</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Tue, 10 Nov 2009 11:49:00 PST</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7250799</guid><description>&lt;P&gt;As a group, we’ve been focusing a lot on technology on such things as JavaScript, MongoDB and so on.&amp;nbsp; This month, I’d like to take a step back as a group and instead focus on some soft skills.&amp;nbsp; When I started this group back in 2007, I had the idea that this group would help others not only become better technologists in a wide array of technologies, but also better teachers as well.&amp;nbsp; On November 24th, we have Jeff Casimir to help us understand how to become a better teacher with his presentation “Stop Talking, Start Teaching: 5 Rules for Successful Presentations and Demos”.&amp;nbsp; Listed below are the details:&lt;/P&gt;
&lt;H2&gt;Stop Talking, Start Teaching: 5 Rules for Successful Presentations &amp;amp; Demos&lt;/H2&gt;
&lt;P&gt;Some of the most important work we do as programmers involves pitching ideas. Whether it's inside the team, searching for funders, or running demos for potential clients, we all need to present ideas and cultivate an audience.&amp;nbsp; The past decade has seen the rise of the presentation as an important communication medium, but few people do it well. There are many resources on how to make good looking slides, but we need to focus on the method, structure, and content.&lt;/P&gt;
&lt;P&gt;We don't need to be "speakers" – we need to be teachers. In this session we'll explore the five most essential rules to successful teaching and how they apply to you.&lt;/P&gt;
&lt;P&gt;Attendees will, by the end of the session, have a framework for designing and critiquing their work and that of others. This new understanding will make them better teammates and more successful salespeople.&lt;/P&gt;
&lt;H2&gt;About Jeff Casimir&lt;/H2&gt;
&lt;P&gt;After majoring in Computer Systems Engineering I joined Teach for America and began a career in education. I taught high school Computer Science for four years before moving into school administration. As a Vice Principal I was responsible for evaluating and hiring new teachers, observing and coaching&amp;nbsp; existing teachers, and defining much of the school's academic process. Most of all, I hate boring presentations. I want these smart, experienced people to Stop Talking, and Start Teaching.&lt;/P&gt;
&lt;P&gt;Hope to see you there!&lt;/P&gt;&lt;A href="http://www.eventbrite.com/event/485519201?ref=ebtn" target=_blank mce_href="http://www.eventbrite.com/event/485519201?ref=ebtn"&gt;&lt;IMG border=0 src="http://www.eventbrite.com/registerbutton?eid=485519201" mce_src="http://www.eventbrite.com/registerbutton?eid=485519201"&gt;&lt;/A&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7250799" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=Bj5RaqMo828:KsrVuDlqS9U:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=Bj5RaqMo828:KsrVuDlqS9U:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=Bj5RaqMo828:KsrVuDlqS9U:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=Bj5RaqMo828:KsrVuDlqS9U:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=Bj5RaqMo828:KsrVuDlqS9U:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=Bj5RaqMo828:KsrVuDlqS9U:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/Bj5RaqMo828" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7250799</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/11/10/ann-dc-alt-net-11-24-2009-stop-talking-start-teaching-with-jeff-casimir.aspx</feedburner:origLink></item><item><title>Introduction to the Reactive Framework Part II</title><link>http://feedproxy.google.com/~r/MatthewPodwysockisBlog/~3/C7QibbKkkMA/introduction-to-the-reactive-framework-part-ii.aspx</link><category>C#</category><category>Event-based Programming</category><category>Reactive Framework</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Mon, 02 Nov 2009 21:28:00 PST</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7245686</guid><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" mce_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" mce_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" mce_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/" mce_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" mce_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;
&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;
&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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:78c27d02-2831-4956-a74e-25506fe8b415" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IEnumerable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br&gt;{&lt;br&gt;    IEnumerator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GetEnumerator();&lt;br&gt;} &lt;br&gt;&lt;br&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IEnumerator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; : IDisposable&lt;br&gt;{&lt;br&gt;    T Current { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; }&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MoveNext();&lt;br&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a22fc1cf-6ce7-41d9-b977-1c914f6dea63" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IObserver&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br&gt;{&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OnCompleted();&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OnError(Exception exception);&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OnNext(T value);&lt;br&gt;} &lt;br&gt;&lt;br&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IObservable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br&gt;{&lt;br&gt;    IDisposable Subscribe(IObserver&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observer);&lt;br&gt;}&lt;br&gt;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;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://s3.amazonaws.com/CodeBetter/CommunityServer.Blogs.Components.WeblogFiles/matthew/podwysocki/image_02D24D1F.png?AWSAccessKeyId=0KMA35HT86EVXB99Z302&amp;amp;Expires=1257319297&amp;amp;Signature=5oBVUyfpqj5kNOGTNx1J8kaoE90%3d"&gt;&lt;img src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_494F3D27.png" style="border-width: 0px; display: inline;" title="image" alt="image" border="0" height="83" width="244"&gt;&lt;/a&gt; 
&lt;/p&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;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 src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/matthew.podwysocki/image_5F00_thumb_5F00_21A8C0FD.png" style="border-width: 0px; display: inline;" title="image" alt="image" border="0" height="123" width="244"&gt;&lt;/a&gt; 
&lt;/p&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;
&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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:31648a45-a756-4dad-876e-eb6beff8ac03" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IEnumerator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; : IDisposable&lt;br&gt;{&lt;br&gt;    T Current { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; } &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; throws exception&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MoveNext();&lt;br&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;What 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d43100c1-d9db-443e-a5e1-bbac9d2b880f" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IDualEnumerator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br&gt;{&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; SetCurrent(T value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Exception ex);&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MoveNext(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; canMove);&lt;br&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:644732de-0d0e-424d-8a79-f2e41f88677f" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IDualEnumerator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br&gt;{&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Yield(T value);&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Throw(Exception ex);&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MoveNext(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; canMove);&lt;br&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Now that we have 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:67e3bcd0-71a6-4f8a-b2a7-38b4e6cfe217" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IDualEnumerator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br&gt;{&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Yield(T value);&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Throw(Exception ex);&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Break();&lt;br&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;What 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:64b72f33-4825-40a2-bbd3-9dc86c9d9445" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IObserver&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br&gt;{   &lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OnNext(T value);&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OnError(Exception exception);&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OnCompleted();&lt;br&gt;} &lt;br&gt;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;What 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:de589912-3310-4d59-8687-6f512c6b547a" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IEnumerable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br&gt;{&lt;br&gt;    IEnumerator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GetEnumerator();&lt;br&gt;} &lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;This 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b4d287df-8902-4317-bcd8-02d136ddbfa1" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IDualEnumerable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br&gt;{&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Attach(IObserver&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observer);&lt;br&gt;    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Detach(IObserver&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observer);&lt;br&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;If 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e3753e03-8bdf-460e-a35d-0b9c26aef767" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IObservable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br&gt;{&lt;br&gt;    IDisposable Subscribe(IObserver&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observer);&lt;br&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Now that we’ve 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;/p&gt;
&lt;ul&gt;
&lt;li&gt;Aggregate 
&lt;/li&gt;
&lt;li&gt;First/FirstOrDefault 
&lt;/li&gt;
&lt;li&gt;GroupBy 
&lt;/li&gt;
&lt;li&gt;Join 
&lt;/li&gt;
&lt;li&gt;Last/LastOrDefault 
&lt;/li&gt;
&lt;li&gt;Select 
&lt;/li&gt;
&lt;li&gt;SelectMany 
&lt;/li&gt;
&lt;li&gt;Single/SingleOrDefault 
&lt;/li&gt;
&lt;li&gt;Skip/SkipWhile 
&lt;/li&gt;
&lt;li&gt;Take/TakeWhile 
&lt;/li&gt;
&lt;li&gt;Where 
&lt;/li&gt;
&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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5b6433cf-f1f2-4c82-9d79-72eb72d9fbed" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; worker &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; BackgroundWorker();&lt;br&gt;&lt;br&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; percentage &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; progress &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; worker.ProgressChangedEvent()&lt;br&gt;                           .Until(worker.RunWorkerCompletedEvent())&lt;br&gt;                 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; progress.EventArgs.ProgressPercentage;&lt;br&gt;&lt;br&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; subscription &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; percentage.Subscribe(&lt;br&gt;    p &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Percentage Complete: {0}&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, p));&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:38465fdf-0cb4-41e9-afd5-0468654547a4" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;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;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7245686" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=C7QibbKkkMA:fv0YmKldJh0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=C7QibbKkkMA:fv0YmKldJh0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=C7QibbKkkMA:fv0YmKldJh0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=C7QibbKkkMA:fv0YmKldJh0:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=C7QibbKkkMA:fv0YmKldJh0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=C7QibbKkkMA:fv0YmKldJh0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/C7QibbKkkMA" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7245686</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/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/MatthewPodwysockisBlog/~3/MEWplNnbGdI/f-october-2009-ctp-beta2-f-rx-together-at-last.aspx</link><category>F#</category><category>Functional Programming</category><category>Event-based Programming</category><category>Reactive Framework</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Mon, 19 Oct 2009 16:01:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7233351</guid><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;nbsp; 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/" mce_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" mce_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" mce_href="http://msdn.microsoft.com/en-us/library/dd783449(VS.100).aspx"&gt;IObserver&amp;lt;T&amp;gt;&lt;/A&gt;.&amp;nbsp; 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" mce_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" mce_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" mce_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;nbsp; 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;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Delegate when &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&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; 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;'&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;'&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;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Delegate,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Args when &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Delegate : delegate&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;'&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;'&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;'&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;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Delegate&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;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;nbsp; Both of these gave us some pretty rich options when dealing with first class events.&amp;nbsp; 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;nbsp; 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;nbsp; 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;nbsp; 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;'&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;'&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;'&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;'&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;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Delegate when &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&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; 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;'&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;'&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;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Delegate,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;Args when &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Delegate : delegate&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;'&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;'&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;'&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;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Args&amp;gt;  &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;What 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;nbsp; 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;nbsp; 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;nbsp; Much like the Event module, the Observable module includes add, filter, map, partition, merge, choose, and scan.&amp;nbsp; 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;nbsp; 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;nbsp; 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;nbsp; This allows us to subscribe to an observable, which in turn will return an IDisposable.&amp;nbsp; 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;nbsp; 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;nbsp; Well, now we can treat observables much the same as we would for F# first class events.&amp;nbsp; 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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Event Sample&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: #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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Over (%d, %d)&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Under (%d, %d)&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;As you can see from this code, we’re treating our Observables no differently than we would our F# First Class Events.&amp;nbsp; With the inheritance chain of the IEvent from the IObservable, we get seamless integration between the two.&amp;nbsp; 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;nbsp; 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;nbsp; This release for &lt;A href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx" mce_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" mce_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" mce_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;nbsp; The integration of the IObservable/IObserver is not to be overlooked as one of them.&amp;nbsp; Download them today and give the team feedback!&amp;nbsp; Now back to our Observables series.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7233351" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=MEWplNnbGdI:isIBHRGmK3U:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=MEWplNnbGdI:isIBHRGmK3U:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=MEWplNnbGdI:isIBHRGmK3U:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=MEWplNnbGdI:isIBHRGmK3U:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=MEWplNnbGdI:isIBHRGmK3U:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=MEWplNnbGdI:isIBHRGmK3U:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/MEWplNnbGdI" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7233351</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/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/MatthewPodwysockisBlog/~3/RmWUHPz6u_Y/ann-dc-alt-net-10-22-2009-mongodb-with-david-james.aspx</link><category>ALT.NET</category><category>User Groups</category><category>NOSQL</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Thu, 15 Oct 2009 20:40:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7231077</guid><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/" mce_href="http://www.mongodb.org/"&gt;MongoDB&lt;/a&gt; with &lt;a href="http://twitter.com/djsunlight" mce_href="http://twitter.com/djsunlight"&gt;David James&lt;/a&gt;.&amp;nbsp; Stay tuned to the &lt;a href="http://tech.groups.yahoo.com/group/dcaltnet/" mce_href="http://tech.groups.yahoo.com/group/dcaltnet/"&gt;mailing list&lt;/a&gt; for up to date details.&amp;nbsp; 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/" mce_href="https://nosqleast.com/2009/"&gt;NoSQLEast conference&lt;/a&gt;.&amp;nbsp; This meeting, we'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;nbsp; Like other document-oriented databases, MongoDB is designed for problems without heavy transactional requirements that aren't easily solved by traditional relational databases, including problems which require the database to span many servers.&amp;nbsp; 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" mce_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;nbsp; And yes, there is even &lt;a href="http://www.claassen.net/geek/blog/2009/09/linq2mongodb-building-linq-provider-for.html" mce_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'll have a hacking session for those interested, so be sure to install &lt;a href="http://www.mongodb.org/display/DOCS/Downloads" mce_href="http://www.mongodb.org/display/DOCS/Downloads"&gt;MongoDB&lt;/a&gt; beforehand.&amp;nbsp; 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" mce_href="http://www.eventbrite.com/event/464904542?ref=ebtn" target="_blank"&gt;&lt;img src="http://www.eventbrite.com/registerbutton?eid=464904542" style="border-width: 0px;" mce_src="http://www.eventbrite.com/registerbutton?eid=464904542" border="0"&gt;&lt;/a&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7231077" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=RmWUHPz6u_Y:1H8XziwFyVM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=RmWUHPz6u_Y:1H8XziwFyVM:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=RmWUHPz6u_Y:1H8XziwFyVM:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=RmWUHPz6u_Y:1H8XziwFyVM:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=RmWUHPz6u_Y:1H8XziwFyVM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=RmWUHPz6u_Y:1H8XziwFyVM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/RmWUHPz6u_Y" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7231077</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/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/MatthewPodwysockisBlog/~3/mCUfJ3zKkWU/f-first-class-events-composing-events-until-others.aspx</link><category>F#</category><category>Functional Programming</category><category>Event-based Programming</category><category>Reactive Framework</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Wed, 14 Oct 2009 22:47:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7230326</guid><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" mce_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" mce_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;nbsp; In particular, this comment was in regards to implementing the until combinator using F# First Class Events.&amp;nbsp; 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;nbsp; 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;nbsp; We then have to subscribe to the main event and only trigger when the flag is set to true.&amp;nbsp; 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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;In order for us to modify the fire flag, we must use a ref cell.&amp;nbsp; The reason being is that local mutables are always stack allocated which is why you cannot capture them inside a closure.&amp;nbsp; One comment that was made about this solution was that we added two subscriptions for creating this single event.&amp;nbsp; 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" mce_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;nbsp; 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;nbsp; In my previous post, we used the WebClient to track the progress changed of a download until the download has completed.&amp;nbsp; 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;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Del when &lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;'&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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;foo&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: #800000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;bar&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; 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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;%d%% Complete&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;%s&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; args.Result)   &lt;/SPAN&gt;&lt;/PRE&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;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;nbsp; This follows the design of the Reactive Framework, and quite frankly, makes a lot of sense.&amp;nbsp; 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;nbsp; 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;nbsp; What if we want to create our mouse drag event like we had from the previous post using the Reactive Framework?&amp;nbsp; 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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&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;nbsp; 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;nbsp; 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;nbsp; &lt;/P&gt;
&lt;P&gt;In order to make this happen, we need to implement a bind/collect combinator.&amp;nbsp; 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;'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;a&amp;gt; -&amp;gt; (&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;'&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;'&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;'&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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Where the M is some monadic type.&amp;nbsp; 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;nbsp; 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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And our approach to doing this with events should be no different.&amp;nbsp; 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;nbsp; 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;nbsp; 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;nbsp; Secondly, we’ll need to implement the collect combinator much like we had above, but this time for events.&amp;nbsp; 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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we have a 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;nbsp; In this case, we only need the bind and not the return for this instance.&amp;nbsp; 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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;Now that we have this defined, F# gives us some syntactic sugar much like the do notation in Haskell.&amp;nbsp; 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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And there you have it!&amp;nbsp; We 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;nbsp; 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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;And now we can 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;nbsp; One of the driving ideas behind the Reactive Framework in terms of composable events does in fact come from F#.&amp;nbsp; 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;nbsp; Now back to the Reactive Framework series.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7230326" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mCUfJ3zKkWU:HNjkrKGAsoo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mCUfJ3zKkWU:HNjkrKGAsoo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=mCUfJ3zKkWU:HNjkrKGAsoo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mCUfJ3zKkWU:HNjkrKGAsoo:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=mCUfJ3zKkWU:HNjkrKGAsoo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=mCUfJ3zKkWU:HNjkrKGAsoo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/mCUfJ3zKkWU" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7230326</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/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/MatthewPodwysockisBlog/~3/IQUYww2vm0Y/introducing-the-reactive-framework-part-i.aspx</link><category>C#</category><category>Concurrency</category><category>Reactive Framework</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Tue, 13 Oct 2009 22:31:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7229430</guid><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;nbsp; 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;nbsp; &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;nbsp; 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/" mce_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/" mce_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/" mce_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/" mce_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;nbsp; The first question to ask yourself is “How easy is it in today’s frameworks to do asynchronous programming?”&amp;nbsp; 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;nbsp; The need is actually quite great especially given our multi-core revolution.&amp;nbsp; 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;nbsp;&amp;nbsp; Let’s look at the standard ways of doing asynchronous programming currently on the .NET platform.&amp;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e29cf8b3-9489-43e9-abd5-8a4cb686a471" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; WebRequest.Create(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;http://twitter.com/statuses/friends_timeline.xml&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
request.Credentials &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; NetworkCredential(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;bar&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);
request.BeginGetResponse(ar &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; request.EndGetResponse(ar))
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; stream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; response.GetResponseStream())
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; reader &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; StreamReader(stream))
        PublishResult(reader.ReadToEnd()); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Do something with the result&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;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;nbsp; The problem with both is that they weren’t easy to handle exceptions and cancellation.&amp;nbsp; By the time .NET 2.0 came around, there was a new approach to doing asynchronous programming that in fact handled such concerns.&amp;nbsp; Such classes as the BackgroundWorker, WebClient and WCF service clients followed this approach.&amp;nbsp; For example, we could download a user’s timeline from Twitter using the WebClient class.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e256482a-ecb8-41bc-b16f-b9f04f078ab0" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; wc &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; WebClient 
    {Credentials &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; NetworkCredential(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;bar&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)};
wc.DownloadStringCompleted &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (o, e) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(e.Cancelled)
    {
        PublishCancel();
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Handle cancellation&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(e.Error &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
    {
        PublishException(e.Error);
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Handle error&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }

    PublishResult(e.Result); &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Handle success&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;};&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&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;nbsp; 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;nbsp; How could I merge this event with another event, or say stop listening after a certain interval or another event happens?&amp;nbsp; Unlike in F#, in C#, we have no real easy way to deal with a lot of these situations.&amp;nbsp; 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;nbsp; Let’s look at a quick example of how it’s different before we go into detail about what it is.&amp;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f51fc536-a5ac-41a1-8fc4-f9087d93e47f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; wc &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; WebClient 
    { Credentials &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; NetworkCredential(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;bar&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) };
    
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Watch progress only until download complete&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; progress &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; wc.DownloadProgressChangedEvent()
    .Until(wc.DownloadStringCompletedEvent());

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Subscribe which creates IDisposable handlers
&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Handle success and exceptional cases&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; progressHandler &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; progress.Subscribe(
    e &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PublishProgress(e.EventArgs.ProgressPercentage));
    
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; downloadHandler &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; wc.DownloadStringCompletedEvent().Subscribe(
    e &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PublishResults(e.EventArgs.Result),
    exception &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PublishException(exception));

...

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Much later in the program, we can dispose&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;progressHandler.Dispose();
downloadHandler.Dispose();&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What we did was allow us to handle exceptional cases as well as the success case on the next invocation of the observable.&amp;nbsp; I create extension methods which I will cover later, but I want you to get the gist of how this might work.&amp;nbsp; 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;nbsp; 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;nbsp; &lt;/p&gt;

&lt;p&gt;To give one more example about composability, how about creating a simple mouse drag event?&amp;nbsp; How might we do that given the current state of eventing in .NET?&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ef5ade8e-e9a0-4d1b-8bf4-231a8d8944a2" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;event&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MouseEventHandler MouseDrag;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; InitializeMouseDrag()
{
    MouseDrag &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (o, e) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PublishMouseDrag(o, e);

    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; fired &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
    MouseDown &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;delegate&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; { fired &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; };
    MouseUp &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;delegate&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; { fired &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; };
    MouseMove &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (o, e) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    {
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (fired &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MouseDrag &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
            MouseDrag(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, e);
    };
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What we’re doing is determining whether our mouse down and mouse move are firing together and if they are, publish the coordinate to the MouseDrag event.&amp;nbsp; From this code, it’s rather clumsy in order to do this.&amp;nbsp; Luckily, the Reactive Framework gives us a better alternative for handling such a thing.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:72650a47-c2c4-48f2-944e-722fe219e817" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Create mouse drag&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mouseDrag &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; md &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.GetMouseDown()
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mm &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.GetMouseMove().Until(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.GetMouseUp())
                &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mm;

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Subscribe&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; handler &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; mouseDrag.Subscribe(
    e &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; PublishMouseDrag(e.EventArgs.Location));

...

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Clean up when we're done with the event&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;handler.Dispose();&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;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;nbsp; 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;nbsp; Since the Reactive Framework has been shipped as part of &lt;a href="http://www.codeplex.com/silverlight" mce_href="http://www.codeplex.com/silverlight"&gt;Silverlight 3 Toolkit&lt;/a&gt;, there has been a bit of interest.&amp;nbsp; 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;nbsp; When we talk about interactive programming, we’re talking about asking for something and getting it in return.&amp;nbsp; One common pattern in this world is the iterator pattern.&amp;nbsp; 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;nbsp; 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;nbsp; In contrast, with reactive programming, we register interest that we want sushi, and as they become available, the chef hands us them asynchronously.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;To put this in code terms, let’s show the interactive version of our sushi model.&amp;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6ff78f7d-5fec-41cc-b27a-33b21a3c70dd" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IEnumerable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GetSushi()
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Toro&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Maki&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Uni&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
}

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Pull each sushi piece from the conveyor&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; piece &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GetSushi())
    ConsumeSushi(piece);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Inside the IEnumerable&amp;lt;T&amp;gt;, we have our items waiting to go, but will only move when we ask for them.&amp;nbsp; The inverse would be where we indicate we’re hungry by subscribing to the observable which contains our sushi.&amp;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d072506b-aa36-41a3-91cc-136b62f87cf5" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Build up our menu&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IObservable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GetSushi()
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Observable.Cons(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Toro&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, 
        Observable.Cons(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Maki&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, 
        Observable.Return(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Uni&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)));
}

&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Eat them as soon as they come to us&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; sushiSubscription &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GetSushi().Subscribe(
    piece &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ConsumeSushi(piece));&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;In the above example, I built up our observable menu of sushi using the Cons and Return static methods on the Observable class.&amp;nbsp; 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;nbsp; &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;nbsp; In our above example, the &lt;a href="http://en.wikipedia.org/wiki/Iterator_pattern" mce_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;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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a9642f4f-9e28-41d4-88ec-3d18b0cdc69b" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IEnumerable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
{
    IEnumerator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; GetEnumerator();
}

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IEnumerator&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; : IDisposable
{
    T Current { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; }
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; MoveNext();
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The 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. &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" mce_href="http://en.wikipedia.org/wiki/Observer_pattern"&gt;Observer Pattern&lt;/a&gt;.&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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eb87332f-a4a0-4182-aed3-37e37b274743" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IObserver&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OnCompleted();
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OnError(Exception exception);
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; OnNext(T value);
}

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IObservable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
{
    IDisposable Subscribe(IObserver&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; observer);
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What we 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;nbsp; 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;nbsp; 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;nbsp; To not only manage exceptions and cancellation, but also to make events composable is yet another challenge.&amp;nbsp; 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; There is a lot to cover including the mathematical duality, what it is underneath the covers and so on.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7229430" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=IQUYww2vm0Y:dO5b7d3R4vM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=IQUYww2vm0Y:dO5b7d3R4vM:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=IQUYww2vm0Y:dO5b7d3R4vM:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=IQUYww2vm0Y:dO5b7d3R4vM:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=IQUYww2vm0Y:dO5b7d3R4vM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=IQUYww2vm0Y:dO5b7d3R4vM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/IQUYww2vm0Y" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7229430</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/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/MatthewPodwysockisBlog/~3/p-Hk3m4BkME/functional-programming-fundamentals-lectures.aspx</link><category>F#</category><category>Functional Programming</category><category>Haskell</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Thu, 08 Oct 2009 21:39:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7225924</guid><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/" mce_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;nbsp; 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;nbsp; With that, Erik with the help of Charles, have kicked off the Channel 9 lecture series on Functional Programming Fundamentals.&amp;nbsp; 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/%7Egmh/" mce_href="http://www.cs.nott.ac.uk/~gmh/"&gt;Graham Hutton’s excellent Programming in Haskell&lt;/a&gt; book.&amp;nbsp; 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;nbsp; 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/" mce_href="http://www.haskell.org/onlinereport/"&gt;Haskell 98 Report&lt;/a&gt;.&amp;nbsp; 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/" mce_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;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7225924" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=p-Hk3m4BkME:9Fmo7068NTE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=p-Hk3m4BkME:9Fmo7068NTE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=p-Hk3m4BkME:9Fmo7068NTE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=p-Hk3m4BkME:9Fmo7068NTE:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=p-Hk3m4BkME:9Fmo7068NTE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=p-Hk3m4BkME:9Fmo7068NTE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/p-Hk3m4BkME" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7225924</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/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/MatthewPodwysockisBlog/~3/PJ4IDnaphxQ/generically-constraining-f-part-iii.aspx</link><category>F#</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Thu, 01 Oct 2009 22:27:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7221468</guid><description>&lt;p&gt;In &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/09/27/generically-constraining-f-part-ii.aspx" mce_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;nbsp; I showed some of the basics and how you might use them to your advantage.&amp;nbsp; We covered the type constraint, null constraint and explicit member constraint.&amp;nbsp; 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;nbsp; In order to use this, we must use the &amp;lt;type&amp;gt; : (new : unit –&amp;gt; &amp;lt;type&amp;gt;).&amp;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b9a7fd4a-609b-44ba-98c2-b516d46ac7db" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Security.Cryptography

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; generateHash&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a when &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;and&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a :&amp;gt; HashAlgorithm&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; path &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; algo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a()&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;use&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; stream &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; File.OpenRead path  
  algo.ComputeHash stream
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; BitConverter.ToString
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; s &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; s.Replace(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What this code does is first declares that our type must have a default constructor and must derive from System.Security.Cryptography.HashAlgorithm.&amp;nbsp; 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;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:eb37b593-6ae1-4bb7-82fb-b210290ae1de" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; generateHash&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;MD5Cng&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;notepad.exe&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;D3002BDDC758A762736730DE399920E5&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;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;nbsp; 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;nbsp; Instead, F# chose the more apt name of not struct.&amp;nbsp; Let’s look through an example of using this to initialize a reference type sequence.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d2a1fb1a-2449-43a1-858f-1d39680b0e91" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; createRefSeq&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a when &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : not struct&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; : &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a -&amp;gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a seq &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  Seq.singleton&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Now we can verify the behavior of this such as the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:224ad885-f12c-484d-acb1-207e119eec0a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; createRefSeq &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : seq&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; seq [&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; createRefSeq (Func&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;_&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; () &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; DateTime.Now));;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : seq&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Func&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;DateTime&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  seq [System.Func`&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[System.DateTime] {Method &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.DateTime Invoke();
                                       Target &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; FSI_0061&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;it@&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;90&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;4&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;}]
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; createRefSeq &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;

  createRefSeq &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;--------------^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;

stdin(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;92&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;15&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;): error FS0001: A generic construct requires that the &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
have reference semantics, but it does not, i.e. it &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;is&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; a struct&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;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;nbsp; We covered this constraint as part of how we could solve Jon Skeet’s problem of Enum.GetValues.&amp;nbsp; Let’s revisit that solution with a twist of getting the name from a value.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b71fc6ab-abf1-4d82-9864-39b755e7d149" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getEnumName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a,&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b when &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a :&amp;gt; Enum &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;and&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a :&amp;gt; ValueType&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;and&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a : enum&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
 (value:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a) =&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  Enum.GetName(typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt;, value)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt; And now we can test our behavior such as the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a66dacec-758d-4988-bb71-e181d5a49b70" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getEnumName DayOfWeek.Saturday;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Saturday&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;h2&gt;Delegate Constraint&lt;/h2&gt;

&lt;p&gt;Lastly, the delegate constraint constrains the generic type to be a delegate.&amp;nbsp; This constraint, probably the least used of the constraints, can in fact be put to good use.&amp;nbsp; 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;nbsp; 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;nbsp; Let’s go through that solution again.&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9d5327b6-6563-452d-8883-e2431735cac7" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IEvent&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Del,&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Args when &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Del : delegate&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Args,unit&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
                       &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;and&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Del :&amp;gt; Delegate &amp;gt; with&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; this.Subscribe(d) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    this.AddHandler(d)
    { &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IDisposable &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
        &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; x.Dispose() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
          this.RemoveHandler(d) }&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What this allows us to do is add a subscription that we can unsubscribe from easily via the IDisposable object we hand back.&amp;nbsp; Let’s test this a subscription to a Windows Form Click event and then unsubscribe:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:54ad5a62-e70f-4a3f-8200-a5f5b6bb6c87" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Windows.Forms
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; f &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Form(Visible&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, TopMost&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; count &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ref &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; h &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; f.Click.Subscribe(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;fun&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; _ _ &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; count :&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;count &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;; printfn &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;%d&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;count);;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; h : IDisposable

&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;4&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;5&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; h.Dispose();;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;What we did was create a quick form and then add our handler.&amp;nbsp; After a few clicks, we get bored, and then decide to dispose of our little counter.&amp;nbsp; 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;nbsp; 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;nbsp; Many of these explicit type signatures, although nice to specify, can be inferred by the F# compiler a lot of the time.&amp;nbsp; With the last segment of first class events, we’ll next step into the Reactive Framework.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7221468" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=PJ4IDnaphxQ:jnNfcLaiI_c:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=PJ4IDnaphxQ:jnNfcLaiI_c:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=PJ4IDnaphxQ:jnNfcLaiI_c:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=PJ4IDnaphxQ:jnNfcLaiI_c:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=PJ4IDnaphxQ:jnNfcLaiI_c:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=PJ4IDnaphxQ:jnNfcLaiI_c:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/PJ4IDnaphxQ" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7221468</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/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/MatthewPodwysockisBlog/~3/hcuwaEPhtlw/generically-constraining-f-part-ii.aspx</link><category>F#</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Sun, 27 Sep 2009 19:47:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7218727</guid><description>&lt;p&gt;In the previous post, we talked about the basic problems with C# generic restrictions.&amp;nbsp; We covered how it was possible in fact in the CLR to do these restrictions and sure enough the F# language supports them.&amp;nbsp; This time, we’ll go deeper into what kinds of generic restrictions are possible and how we might use them.&amp;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:982673ab-eaf1-4b10-9a40-65ad712bc7bf" class="wlWriterSmartContent"&gt;
  &lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getValues&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a when &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;struct&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;and&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a :&amp;gt; Enum&amp;gt;() =&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  Enum.GetValues(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt;) :?&amp;gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a array&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;In 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;nbsp; Alternatively, we could also use the F# enum constraint as well, such as the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2102bcf0-55f3-4624-bb75-38eb2d17b720" class="wlWriterSmartContent"&gt;
  &lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getValues&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a,&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b when &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a : struct and &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : enum&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;b&amp;gt; and &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a :&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Enum&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  Enum.GetValues(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt;) :?&amp;gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a array&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;And then we could execute as before:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c7d6f530-eaa5-4cb5-a445-3a68eb138516" class="wlWriterSmartContent"&gt;
  &lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.IO;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getValues&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;FileAttributes, int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;();;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : FileAttributes array &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  [&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;ReadOnly; Hidden; System; Directory; Archive; Device; Normal; Temporary;
    SparseFile; ReparsePoint; Compressed; Offline; NotContentIndexed;
    Encrypted&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]&lt;/span&gt;&lt;/pre&gt;
&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Both of these examples work when exported to C# as well.&amp;nbsp; 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;nbsp; 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;nbsp; In the case of interfaces, the provided type must implement said interface.&amp;nbsp; 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;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c5396a8c-04fe-4621-9b43-7fb3e9f97f3f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; drawLine&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a when &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a :&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; IAddChild&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; stroke (x1, y1) (x2, y2) (e : &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a) =&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; line &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; LineGeometry(StartPoint &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Point(x1, y1), EndPoint &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Point(x2, y2))
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; path &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Path(Stroke &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; stroke, StrokeThickness &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;5&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;., Data &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; line)
  e.AddChild(path)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;By using the :&amp;gt; operator, we constrain our ‘a type that it must implement the IAddChild interface.&amp;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d77c4543-0e2e-498d-8f6e-d0541ce9589d" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; drawLine stroke (x1, y1) (x2, y2) (e : #IAddChild) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; line &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; LineGeometry(StartPoint &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Point(x1, y1), EndPoint &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Point(x2, y2))
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; path &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Path(Stroke &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; stroke, StrokeThickness &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;5&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;., Data &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; line)
  e.AddChild(path)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;h2&gt;Null Constraint&lt;/h2&gt;

&lt;p&gt;In this constraint, the provided type must support being set to null.&amp;nbsp; 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;nbsp; By using the ‘a : null syntax, we can say that ‘a is a nullable reference.&amp;nbsp; Note that this doesn’t work for Nullable&amp;lt;T&amp;gt; types either.&amp;nbsp; Let’s look at a quick example of using the null constraint:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0d88a728-e004-4d5e-bf55-6ae67c8e337d" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; printValue&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a when &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (arg:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a) =&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; arg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;then&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    printfn &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;You gave me nothing&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    printfn &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Value: %A&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; arg&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt; Then we could test the behavior by giving it several examples such as the following:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:41c1aa9c-622f-4136-9070-91e6f27f39ec" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; printValue &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;hello&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
Value: &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;hello&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ()
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; printValue &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;
You gave me nothing&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;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;nbsp; This means that we could in fact do duck typing in such a way that our argument must support a certain member function.&amp;nbsp; I’ve covered this in the past, but let’s review.&amp;nbsp; One example would be to create a function which allows us to invoke implicit operators, which are by default not supported by F#.&amp;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:73e780e7-0839-410e-aa47-2bba018da181" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;inline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; implicit&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b when &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
  arg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a) arg)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This allows me to specify that my ^a type must support a static method called op_Implicit, simply known as the implicit operator.&amp;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e19032f8-9e11-445f-a725-8da77beeae24" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;inline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; implicit arg &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
  (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; op_Implicit : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a) arg)&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt; We can test the behavior of this by using the implicit operator built into LINQ to XML.&amp;nbsp; Using the library is not exactly as easy as it is in C# due to the implicit operator from string to XName.&amp;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c62385cc-83e3-4b67-9046-64fcedc080d4" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Xml.Linq;;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ) : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XNamespace &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; implicit;;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ( &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ) : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XNamespace)

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; foo &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;;

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; foo : XName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; foo&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&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;nbsp; Before we wrap up this section, let’s try one more example which supports multiple multiple methods.&amp;nbsp; In this case, we’ll extract both the name as an XName, and the value from a given LINQ to XML object.&amp;nbsp; 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;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:96a4013e-3b35-4aa1-91d5-f6e57d891566" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Xml.Linq

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;inline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getNameValue&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a when &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; get_Name  : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XName ) &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;and&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
                                 &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; get_Value : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
 xItem &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; name &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; get_Name  : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XName ) xItem)
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; get_Value : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) xItem)
  name, value&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;In this case, we constrain our generic that it must implement both a Name and Value property getter.&amp;nbsp; Then we invoke both the name and value functions and return them as a tuple.&amp;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:af9fd451-3b85-4c4b-aeca-cb2d240ec657" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System.Xml.Linq

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;inline&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getNameValue xItem &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; 
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; name &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; get_Name  : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XName ) xItem)
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;member&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; get_Value : unit &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) xItem)
  name, value&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;You can begin to see the power in this where we are no longer constrained by inheritance, and instead which methods our type supports.&amp;nbsp; 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;nbsp; Let’s walk through some simple examples using it:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:564e458b-9452-4dcd-a30a-0e00c7351b01" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getNameValue (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XElement(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;bar&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;));;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : XName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (bar {LocalName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;bar&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
                                Namespace &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ;
                                NamespaceName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;}, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getNameValue (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XAttribute(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;!!&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;+&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;bar&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;));;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;val&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; it : XName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;*&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (foo {LocalName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;foo&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;
                                Namespace &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; ;
                                NamespaceName &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;""&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;;}, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;bar&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;)
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getNameValue (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XDocument());;

  getNameValue (&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; XDocument());;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;--------------^^^^^^^^^^^^^^^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;

stdin(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;238&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;15&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;): error FS0001: The &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;type&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;XDocument&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; does not support any operators
 named &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;get_Name&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Now 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;nbsp; 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;nbsp; In the next part, we’ll cover the rest of the generic constraints that F# supports.&amp;nbsp; &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7218727" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=hcuwaEPhtlw:mUht3DCqKB4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=hcuwaEPhtlw:mUht3DCqKB4:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=hcuwaEPhtlw:mUht3DCqKB4:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=hcuwaEPhtlw:mUht3DCqKB4:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=hcuwaEPhtlw:mUht3DCqKB4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=hcuwaEPhtlw:mUht3DCqKB4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/hcuwaEPhtlw" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7218727</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/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/MatthewPodwysockisBlog/~3/Y1AHlBxPHUc/pondering-axum-f.aspx</link><category>F#</category><category>Concurrency</category><category>Functional Programming</category><category>Axum</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Fri, 25 Sep 2009 13:39:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7216647</guid><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" mce_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;nbsp; 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;nbsp; Overall, the goals of Axum to provide an agent-based concurrency oriented system is important as we consider the emerging hardware trends.&amp;nbsp; Many of these ideas would in fact benefit most languages, whether mainstream or not.&amp;nbsp; 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;nbsp; 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;nbsp; Creating and maintaining languages is a large effort, not to be undertaken lightly at any organization.&amp;nbsp; 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;nbsp; &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;nbsp; 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;nbsp; 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;nbsp; 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;nbsp; Now that F# is a first-class citizen within Visual Studio going forward, there can be a strong case made.&amp;nbsp; Given some of the features of F#, which we will discuss, could potentially be a happy marriage with Axum.&amp;nbsp; 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;nbsp; Often we have to protect certain parts by using low level constructs such as locks, semaphores, mutexes.&amp;nbsp; 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;nbsp; Instead, communication through message passing in languages such as Axum and Erlang, immutability is the standard.&amp;nbsp; This way, we can freely reference the values without any worry of whether they will change underneath us.&amp;nbsp; Providing first-class support for immutability goes a long way towards making safe concurrency programming easier for the masses.&amp;nbsp; With the F# language, we get that approach by default and with rich types such as records, discriminated unions, lists, tuples and more.&amp;nbsp; &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;nbsp; 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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;In this case, we have succinctly defined what messages can be processed.&amp;nbsp; 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;nbsp;&amp;nbsp; 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;nbsp; 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;nbsp; In particular, the asynchronous workflows and the mailbox processor could match well with the Axum programming model.&amp;nbsp; Interestingly, &lt;A href="http://blogs.msdn.com/lucabol/" mce_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" mce_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;nbsp; 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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Ping: pong&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; 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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Ping: stop&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&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; 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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Pong: ping %d&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Pong: stop&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;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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&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;nbsp; We have certain limitations here such as we cannot remote these calls, so all interaction happens inside the single application.&amp;nbsp; 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;nbsp; That’s a great question!&amp;nbsp; Some of the concepts from Axum could map perfectly to F# whereas some might need a little more coaxing.&amp;nbsp; Let’s go back to the basic building blocks of Axum.&amp;nbsp; 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;nbsp; &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;nbsp; Agents are defined to run within the isolated space of a domain with each agent on a separate thread of control.&amp;nbsp; These agents are exposed to one another through passing messages back and forth via channels which define ports through which our data flows.&amp;nbsp; 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;nbsp; 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;nbsp; I’ll choose just a couple as they might work nicely out of the box.&amp;nbsp; Let’s first look at how we might define a port which accepts a given message.&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: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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/DIV&gt;
&lt;P&gt;This way, we could define what our payload type for both incoming and outgoing.&amp;nbsp; Another area would be to look how schemas might be done.&amp;nbsp; In our earlier example, we used simple data types such as integers, but what about more complex types?&amp;nbsp; 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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Matthew Podwysocki&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&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;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Microsoft Corporation&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;"&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;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&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;nbsp; 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;nbsp; 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;nbsp; 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;nbsp; What do you think?&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7216647" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=Y1AHlBxPHUc:xSq54-euAUU:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=Y1AHlBxPHUc:xSq54-euAUU:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=Y1AHlBxPHUc:xSq54-euAUU:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=Y1AHlBxPHUc:xSq54-euAUU:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=Y1AHlBxPHUc:xSq54-euAUU:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=Y1AHlBxPHUc:xSq54-euAUU:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/Y1AHlBxPHUc" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7216647</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/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/MatthewPodwysockisBlog/~3/aKVysEt828A/generically-constraining-f-part-i.aspx</link><category>C#</category><category>F#</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Tue, 15 Sep 2009 21:39:00 PDT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7207508</guid><description>&lt;p&gt;Generic constraints inside .NET has always been a fun enterprise, especially given how C# handles them&amp;nbsp; There has been &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2009/09/10/generic-constraints-for-enums-and-delegates.aspx" mce_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;nbsp; 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;nbsp; The example Jon shows is indeed illegal in C#:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2e23abd1-d621-4e95-86bb-397403584b1e" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; T[]GetValues&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;T&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;where&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; T : &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;struct&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, System.Enum
{
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (T[]) Enum.GetValues(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(T));
} &lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;However, as Jon correctly points out, this is indeed supported by the CLR directly.&amp;nbsp; In fact, with our knowledge of &lt;a href="http://msdn.microsoft.com/en-us/library/dd233203%28VS.100%29.aspx" mce_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;nbsp; 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;nbsp; The idea here is to ensure that our T type as above is an enum.&amp;nbsp; 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;nbsp; Remember, &lt;a href="http://msdn.microsoft.com/en-us/library/system.enum.aspx" mce_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;nbsp; &lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5e8b640f-e5c6-4e88-ac39-b98761ab77ad" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;namespace&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Codebetter.Constraints

&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;module&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Constraint &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;

  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; System

  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; getValues&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a, &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b when &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a : enum&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;b&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;() &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;
    Enum.GetValues(typeof&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a&amp;gt;) :?&amp;gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a array&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;As we can see from the above code, it’s rather straight forward.&amp;nbsp; We specify the ‘a must be an enum of an inner type of ‘b.&amp;nbsp; 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;nbsp; Calling this code, we can get arrays of all values.&amp;nbsp; Let’s test in F# interactive:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:96f08204-5ec9-4a22-ace7-a1916cc402eb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Constraint.getValues&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;System.IO.FileAccess,&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;();;
val it : System.IO.FileAccess array &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; [&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Read; Write; ReadWrite&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;|&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;]
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Constraint.getValues&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;();;

  Constraint.getValues&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;();;
  &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;^^^^^^^^^^^^^^^^^^^^&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;

error FS0001: The type &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;is&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; not a .NET &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;enum&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; type&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;In 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;nbsp; But, what about C# here?&amp;nbsp; Could this code transfer?&amp;nbsp; In some C# calling code, we could then use our function as follows:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:527d59ad-838e-41c0-9f25-92d06d4c0511" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Main(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;[] args)
{
    var values &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Constraint.getValues&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;FileAttributes, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;();
    &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(var value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; values) Console.WriteLine(value);
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This will give us the expected results of all values for the FileAttributes enum just as we would through our F# code.&amp;nbsp; An issue arises, however, when we try with a failure case as we had above:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fdf9a60c-c882-4a9e-bb98-9012152f5701" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;var values &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; Constraint.getValues&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;, &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;();
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (var value &lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; values) Console.WriteLine(value);&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This will compile just as our previous example did.&amp;nbsp; 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="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:429d0453-c73f-4365-b733-7b0bb68931f9" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&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: rgb(0, 0, 255);"&gt;in&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; C:\Work\ConstraintLib\Module1.fs:line &lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;19&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;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;nbsp; Other restrictions such as reference type restrictions such as the following work well:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1984b638-21de-42f4-85c1-0b65aee82c7f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; printClass&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a when &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a : not struct&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; (arg:&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;'&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;a) =&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  printfn &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;%A&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; arg&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;And the calling C# code:&lt;/p&gt;

&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a3fbbd21-07fa-4a6c-8f9f-ebfcf2ef0303" class="wlWriterEditableSmartContent"&gt;&lt;pre style="overflow: auto; background-color: rgb(255, 255, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Constraint.printClass(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Hello&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; prints Hello&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;
&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Constraint.printClass(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;3&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; error CS0452&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;By 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;nbsp; There are a few more to talk about before we’re done such as method signature restrictions, constructor restrictions and so forth.&amp;nbsp; 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;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7207508" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=aKVysEt828A:sbPl2Bzn-Qc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=aKVysEt828A:sbPl2Bzn-Qc:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=aKVysEt828A:sbPl2Bzn-Qc:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=aKVysEt828A:sbPl2Bzn-Qc:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?a=aKVysEt828A:sbPl2Bzn-Qc:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/MatthewPodwysockisBlog?i=aKVysEt828A:sbPl2Bzn-Qc:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/aKVysEt828A" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=7207508</wfw:commentRss><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2009/09/16/generically-constraining-f-part-i.aspx</feedburner:origLink></item></channel></rss>
