<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Luca Bolognese's WebLog</title><link>http://blogs.msdn.com/b/lucabol/</link><description>This blog will be frozen on 2/19/2010. New one started at http://lucabolognese.wordpress.com/</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Luca Bolognese leaves Microsoft</title><link>http://blogs.msdn.com/b/lucabol/archive/2010/02/19/luca-bolognese-leaves-microsoft.aspx</link><pubDate>Fri, 19 Feb 2010 16:58:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9966435</guid><dc:creator>lucabol</dc:creator><slash:comments>15</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucabol/rsscomments.aspx?WeblogPostID=9966435</wfw:commentRss><comments>http://blogs.msdn.com/b/lucabol/archive/2010/02/19/luca-bolognese-leaves-microsoft.aspx#comments</comments><description>&lt;p&gt;This is my last post on this blog. My new blog is here: &lt;a title="http://lucabolognese.wordpress.com/" href="http://lucabolognese.wordpress.com/"&gt;http://lucabolognese.wordpress.com/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I accepted a role as Director for Credit Suisse in London. I’m excited by the opportunity to work in the financial industry, a long-standing desire of mine. I’m also excited to write more F# code and to be closer to Italy, where my extended family is.&lt;/p&gt;  &lt;p&gt;The past ten years in Microsoft have been a wild ride. I’m proud to have been part of ObjectSpaces, Generics, LINQ and F# (and much more …). I’ve been lucky to be able to post on this blog and present at conferences about such innovative technologies. It has all been a lot of fun. I’m sure the next ten years will be as good.&lt;/p&gt;  &lt;p&gt;I’ll see you guys on my new blog.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9966435" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/C_2300_+Programming/">C# Programming</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/Object+Orientation/">Object Orientation</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/Books/">Books</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/Sport/">Sport</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/Miscellanea/">Miscellanea</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/Financial/">Financial</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/-NET+Futures/">.NET Futures</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/F_2300_/">F#</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/Statistics/">Statistics</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/VB/">VB</category></item><item><title>LChart: displaying charts in F# – Part I</title><link>http://blogs.msdn.com/b/lucabol/archive/2010/02/17/lchart-displaying-charts-in-f-part-i.aspx</link><pubDate>Wed, 17 Feb 2010 19:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9965255</guid><dc:creator>lucabol</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucabol/rsscomments.aspx?WeblogPostID=9965255</wfw:commentRss><comments>http://blogs.msdn.com/b/lucabol/archive/2010/02/17/lchart-displaying-charts-in-f-part-i.aspx#comments</comments><description>&lt;P&gt;I want to use F# as a exploratory data analysis language (like &lt;A href="http://www.r-project.org/" mce_href="http://www.r-project.org/"&gt;R&lt;/A&gt;). But I don’t know how to get the same nice graphic capabilities. So I decided to create them. Here is a library to draw charts in F#. It steals ideas from &lt;A href="http://www.amazon.com/Grammar-Graphics-Leland-Wilkinson/dp/0387987746" mce_href="http://www.amazon.com/Grammar-Graphics-Leland-Wilkinson/dp/0387987746"&gt;this book&lt;/A&gt; and &lt;A href="http://had.co.nz/ggplot2/book/" mce_href="http://had.co.nz/ggplot2/book/"&gt;this R package&lt;/A&gt;. It is nothing more than a wrapper on top of the &lt;A href="http://www.microsoft.com/downloads/details.aspx?familyid=130F7986-BF49-4FE5-9CA8-910AE6EA442C&amp;amp;displaylang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?familyid=130F7986-BF49-4FE5-9CA8-910AE6EA442C&amp;amp;displaylang=en"&gt;Microsoft Chart Controls&lt;/A&gt; to give it a more ‘exploratory’ one line calling syntax. It is also rough work in progress: I don’t wrap all the chart types and there are bugs in the ones I wrap. Also the architecture is all wrong (more on this in another post). But it’s a start and it kind of works. Attached the full code.&lt;/P&gt;
&lt;P&gt;I will continue this series in my new blog at wordpress: &lt;A title=http://lucabolognese.wordpress.com/ href="http://lucabolognese.wordpress.com/" mce_href="http://lucabolognese.wordpress.com/"&gt;http://lucabolognese.wordpress.com/&lt;/A&gt;. The reason I need a new blog will be explained in an upcoming post.&lt;/P&gt;
&lt;P&gt;Part II is now &lt;A href="http://lucabolognese.wordpress.com/2010/02/17/lchart-displaying-charts-in-f-part-ii/" mce_href="http://lucabolognese.wordpress.com/2010/02/17/lchart-displaying-charts-in-f-part-ii/"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Ok, let’s start. How do I draw a chart?&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;x = [1.;2.5;3.1;4.;4.8;6.0;7.5;8.;9.1;15.]
&lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;y = [1.6;2.1;1.4;4.;2.3;1.9;2.4;1.4;5.;2.9]

lc.scatter(x, y) |&amp;gt; display&lt;/PRE&gt;
&lt;P&gt;X and Y are just some make up data. &lt;EM&gt;lc&lt;/EM&gt; is the name of a class (????) and &lt;EM&gt;scatter&lt;/EM&gt; is a static method on it. &lt;EM&gt;scatter&lt;/EM&gt; doesn’t display the chart, it just produces a an object that represents the chart. &lt;EM&gt;Display&lt;/EM&gt; displays the chart. The reason for using the bizarre lc static class is that I want it to be short so that it is easy to type in the fsi.exe. At the same time it needs to support optional parameters (which are not supported on top level functions in F#).&lt;/P&gt;
&lt;P&gt;You get a window with this chart on it. You can press CTRL+C to copy it (as I did to post it here).&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_2.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb.png" width=382 height=295 mce_src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;You might want to customize the chart a bit by passing some of these famous optional parameters:&lt;/P&gt;&lt;PRE class=code&gt;lc.scatter(x = x, y = y, markerSize = 10, markerStyle = MarkerStyle.Diamond,
    xname = &lt;SPAN style="COLOR: maroon"&gt;"Players"&lt;/SPAN&gt;, yname = &lt;SPAN style="COLOR: maroon"&gt;"Ratings"&lt;/SPAN&gt;, title = &lt;SPAN style="COLOR: maroon"&gt;"Players' Ratings"&lt;/SPAN&gt;)  |&amp;gt; display     &lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_4.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_1.png" width=382 height=295 mce_src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Or you might want to print different types of charts:&lt;/P&gt;&lt;PRE class=code&gt;lc.line(y = y, markerSize = 10, markerStyle = MarkerStyle.Diamond, xname = &lt;SPAN style="COLOR: maroon"&gt;"Players"&lt;/SPAN&gt;, yname = &lt;SPAN style="COLOR: maroon"&gt;"Ratings"&lt;/SPAN&gt;, title = &lt;SPAN style="COLOR: maroon"&gt;"Players' Ratings"&lt;/SPAN&gt;, isValueShownAsLabel = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;,
    color = Color.Red) |&amp;gt; display       &lt;/PRE&gt;&lt;A href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_6.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_2.png" width=373 height=289 mce_src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_2.png"&gt;&lt;/A&gt; &lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;lc.spline(x = x, y = y, markerSize = 10, markerStyle = MarkerStyle.Diamond, xname = &lt;SPAN style="COLOR: maroon"&gt;"Players"&lt;/SPAN&gt;, yname = &lt;SPAN style="COLOR: maroon"&gt;"Ratings"&lt;/SPAN&gt;,
    title = &lt;SPAN style="COLOR: maroon"&gt;"Players' Ratings"&lt;/SPAN&gt;, isValueShownAsLabel = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;, color = Color.Red) |&amp;gt; display &lt;/PRE&gt;&lt;A href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_8.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_3.png" width=371 height=287 mce_src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_3.png"&gt;&lt;/A&gt; &lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;lc.stepline(x = x, y = y, markerSize = 10, markerStyle = MarkerStyle.Diamond, xname = &lt;SPAN style="COLOR: maroon"&gt;"Players"&lt;/SPAN&gt;, yname = &lt;SPAN style="COLOR: maroon"&gt;"Ratings"&lt;/SPAN&gt;,
    title = &lt;SPAN style="COLOR: maroon"&gt;"Players' Ratings"&lt;/SPAN&gt;, isValueShownAsLabel = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;, color = Color.Red) |&amp;gt; display&lt;/PRE&gt;&lt;A href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_10.png" mce_href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_10.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_4.png" width=372 height=288 mce_src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_4.png"&gt;&lt;/A&gt; &lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;lc.bar(y = y, xname = &lt;SPAN style="COLOR: maroon"&gt;"Players"&lt;/SPAN&gt;, yname = &lt;SPAN style="COLOR: maroon"&gt;"Ratings"&lt;/SPAN&gt;, title = &lt;SPAN style="COLOR: maroon"&gt;"Players' Ratings"&lt;/SPAN&gt;, isValueShownAsLabel = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;,
    drawingStyle = &lt;SPAN style="COLOR: maroon"&gt;"Emboss"&lt;/SPAN&gt;) |&amp;gt; display      &lt;/PRE&gt;&lt;A href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_12.png" mce_href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_12.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_5.png" width=351 height=265 mce_src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_5.png"&gt;&lt;/A&gt; &lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=code&gt;lc.column(y = y, xname = &lt;SPAN style="COLOR: maroon"&gt;"Players"&lt;/SPAN&gt;, yname = &lt;SPAN style="COLOR: maroon"&gt;"Ratings"&lt;/SPAN&gt;, title = &lt;SPAN style="COLOR: maroon"&gt;"Players' Ratings"&lt;/SPAN&gt;,
    isValueShownAsLabel = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;, drawingStyle = &lt;SPAN style="COLOR: maroon"&gt;"Cylinder"&lt;/SPAN&gt;) |&amp;gt; display   &lt;/PRE&gt;&lt;A href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_14.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_6.png" width=375 height=283 mce_src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_6.png"&gt;&lt;/A&gt; &lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;lc.boxplot(y = y, xname = &lt;SPAN style="COLOR: maroon"&gt;"Players"&lt;/SPAN&gt;, yname = &lt;SPAN style="COLOR: maroon"&gt;"Ratings"&lt;/SPAN&gt;, title = &lt;SPAN style="COLOR: maroon"&gt;"Players' Ratings"&lt;/SPAN&gt;, color = Color.Blue, whiskerPercentile = 5, percentile = 30,
    showAverage = &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;, showMedian = &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;, showUnusualValues = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;) |&amp;gt; display    &lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_16.png" mce_href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_16.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_7.png" width=408 height=317 mce_src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_7.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Ok, the last one is weird. You probably want more than one boxplot in a chart. I'll show you how to do that in the next post.&lt;/P&gt;
&lt;P&gt;The next post will be on how to have more than one series on the same chart and more than one chart in the same windows. Something like the below:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_18.png" mce_href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_18.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_8.png" width=445 height=342 mce_src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/LChartdisplayingchartsinFPartI_9F73/image_thumb_8.png"&gt;&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9965255" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-09-96-52-55/ChartPlotter.fsx" length="12406" type="application/fsharp-script" /><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/F_2300_/">F#</category></item><item><title>Excel Financial Functions upgraded to Visual Studio 2010 RC</title><link>http://blogs.msdn.com/b/lucabol/archive/2010/02/16/excel-financial-functions-upgraded-to-visual-studio-2010-rc.aspx</link><pubDate>Tue, 16 Feb 2010 22:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9962311</guid><dc:creator>lucabol</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucabol/rsscomments.aspx?WeblogPostID=9962311</wfw:commentRss><comments>http://blogs.msdn.com/b/lucabol/archive/2010/02/16/excel-financial-functions-upgraded-to-visual-studio-2010-rc.aspx#comments</comments><description>&lt;p&gt;Here is a link to the home page: &lt;a title="http://code.msdn.microsoft.com/FinancialFunctions/Wiki/View.aspx?title=Home" href="http://code.msdn.microsoft.com/FinancialFunctions/Wiki/View.aspx?title=Home"&gt;http://code.msdn.microsoft.com/FinancialFunctions/Wiki/View.aspx?title=Home&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9962311" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/F_2300_/">F#</category></item><item><title>New version of LAgent compatible with VS 2010 RC online</title><link>http://blogs.msdn.com/b/lucabol/archive/2010/02/15/new-version-of-lagent-compatible-with-vs-2010-rc-online.aspx</link><pubDate>Mon, 15 Feb 2010 17:36:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9963719</guid><dc:creator>lucabol</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucabol/rsscomments.aspx?WeblogPostID=9963719</wfw:commentRss><comments>http://blogs.msdn.com/b/lucabol/archive/2010/02/15/new-version-of-lagent-compatible-with-vs-2010-rc-online.aspx#comments</comments><description>&lt;p&gt;My crazy agent framework now works with RC: &lt;a title="http://code.msdn.microsoft.com/LAgent" href="http://code.msdn.microsoft.com/LAgent"&gt;http://code.msdn.microsoft.com/LAgent&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9963719" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/F_2300_/">F#</category></item><item><title>A simpler F# MailboxProcessor</title><link>http://blogs.msdn.com/b/lucabol/archive/2010/02/12/a-simpler-f-mailboxprocessor.aspx</link><pubDate>Fri, 12 Feb 2010 23:29:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9962927</guid><dc:creator>lucabol</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucabol/rsscomments.aspx?WeblogPostID=9962927</wfw:commentRss><comments>http://blogs.msdn.com/b/lucabol/archive/2010/02/12/a-simpler-f-mailboxprocessor.aspx#comments</comments><description>&lt;p&gt;I always forget the pattern to use to create a functioning &lt;em&gt;MailboxProcessor&lt;/em&gt; in F#. I mean, which piece has to be async and how to structure the recursive loop. When I find myself in that kind of a situation situation, my instincts scream at me: “Wrap it and make it work how your mind expects it to work”. So here is a simplification of the paradigm.&lt;/p&gt;  &lt;p&gt;Let’s see how some standard &lt;em&gt;MailboxProcessor&lt;/em&gt; code looks like:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;counter0 =
    MailboxProcessor.Start(&lt;span style="color: blue"&gt;fun &lt;/span&gt;inbox &lt;span style="color: blue"&gt;-&amp;gt;
        let rec &lt;/span&gt;loop n =
            async { 
                    &lt;span style="color: blue"&gt;let! &lt;/span&gt;msg = inbox.Receive()
                    &lt;span style="color: blue"&gt;return! &lt;/span&gt;loop(n+msg) }
        loop 0)&lt;/pre&gt;

&lt;p&gt;This keeps a running sum of the messages it receives. The only part that is really unique to this guy is “n + msg”. All the rest is infrastructure.&lt;/p&gt;

&lt;p&gt;You’d probably prefer to write code like the following:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;counter1 = MailboxProcessor.SpawnAgent( (&lt;span style="color: blue"&gt;fun &lt;/span&gt;msg n &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;msg + n), 0)&lt;/pre&gt;

&lt;p&gt;Yep, just one line of code. But, is it possible? Let’s look at one way of doing it:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;AfterError&amp;lt;'state&amp;gt; =
| ContinueProcessing &lt;span style="color: blue"&gt;of &lt;/span&gt;'state
| StopProcessing
| RestartProcessing
    
&lt;span style="color: blue"&gt;type &lt;/span&gt;MailboxProcessor&amp;lt;'a&amp;gt; &lt;span style="color: blue"&gt;with

    static member public &lt;/span&gt;SpawnAgent&amp;lt;'b&amp;gt;(messageHandler :'a&lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt;'b&lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt;'b,&lt;br /&gt;                                        initialState : 'b, ?timeout:'b &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;int,
                                        ?timeoutHandler:'b &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;AfterError&amp;lt;'b&amp;gt;,&lt;br /&gt;                                        ?errorHandler:&lt;br /&gt;                                            Exception &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'a option &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'b &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;AfterError&amp;lt;'b&amp;gt;)&lt;br /&gt;                                        : MailboxProcessor&amp;lt;'a&amp;gt; =
        &lt;span style="color: blue"&gt;let &lt;/span&gt;timeout = defaultArg timeout (&lt;span style="color: blue"&gt;fun &lt;/span&gt;_ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;-1)
        &lt;span style="color: blue"&gt;let &lt;/span&gt;timeoutHandler = defaultArg timeoutHandler (&lt;span style="color: blue"&gt;fun &lt;/span&gt;state –&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;                                                                  &lt;/span&gt;ContinueProcessing(state))
        &lt;span style="color: blue"&gt;let &lt;/span&gt;errorHandler = defaultArg errorHandler (&lt;span style="color: blue"&gt;fun &lt;/span&gt;_ _ state –&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;                                                                  &lt;/span&gt;ContinueProcessing(state))
        MailboxProcessor.Start(&lt;span style="color: blue"&gt;fun &lt;/span&gt;inbox &lt;span style="color: blue"&gt;-&amp;gt;
            let rec &lt;/span&gt;loop(state) = async {
                &lt;span style="color: blue"&gt;let! &lt;/span&gt;msg = inbox.TryReceive(timeout(state))
                &lt;span style="color: blue"&gt;try
                    match &lt;/span&gt;msg &lt;span style="color: blue"&gt;with
                    &lt;/span&gt;| None      &lt;span style="color: blue"&gt;-&amp;gt; match &lt;/span&gt;timeoutHandler state &lt;span style="color: blue"&gt;with
                                    &lt;/span&gt;| ContinueProcessing(newState)    &lt;span style="color: blue"&gt;-&amp;gt;&lt;br /&gt;                                                                   return! &lt;/span&gt;loop(newState)
                                    | StopProcessing        &lt;span style="color: blue"&gt;-&amp;gt; return &lt;/span&gt;()
                                    | RestartProcessing     &lt;span style="color: blue"&gt;-&amp;gt; return! &lt;/span&gt;loop(initialState)
                    | Some(m)   &lt;span style="color: blue"&gt;-&amp;gt; return! &lt;/span&gt;loop(messageHandler m state)
                &lt;span style="color: blue"&gt;with
                &lt;/span&gt;| ex &lt;span style="color: blue"&gt;-&amp;gt; match &lt;/span&gt;errorHandler ex msg state &lt;span style="color: blue"&gt;with
                        &lt;/span&gt;| ContinueProcessing(newState)    &lt;span style="color: blue"&gt;-&amp;gt; return! &lt;/span&gt;loop(newState)
                        | StopProcessing        &lt;span style="color: blue"&gt;-&amp;gt; return &lt;/span&gt;()
                        | RestartProcessing     &lt;span style="color: blue"&gt;-&amp;gt; return! &lt;/span&gt;loop(initialState)
                }
            loop(initialState))&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The funny formatting is because I have to fit it in the small horizontal space of this blog. In any case, this is just a simple (?) wrapper for the &lt;em&gt;MailboxProcessor&lt;/em&gt; pattern. The function takes two necessary parameters and two optional ones:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;messageHandler&lt;/strong&gt;: a function to execute when a message comes in, it takes the message and the current state as parameters and returns the new state.&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;initialState&lt;/strong&gt;: the initial state for the &lt;em&gt;MailboxProcessor&lt;/em&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;timeoutHandler&lt;/strong&gt;: a function that is executed whenever a timeout occurs. It takes as a parameter the current state and returns one of &lt;em&gt;ContinueProcessing(newState), StopProcessing or RestartProcessing&lt;/em&gt;&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;errorHandler&lt;/strong&gt;: a function that gets call if an exception is generated inside the &lt;em&gt;messageHandler&lt;/em&gt; function. It takes the exception, the message, the current state and returns &lt;em&gt;ContinueProcessing(newState), StopProcessing or RestartProcessing&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example of how to use &lt;em&gt;errorHandler&lt;/em&gt; to implement the CountingAgent in the Expert F# book follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;msg = Increment &lt;span style="color: blue"&gt;of &lt;/span&gt;int | Fetch &lt;span style="color: blue"&gt;of &lt;/span&gt;AsyncReplyChannel&amp;lt;int&amp;gt; | Stop

&lt;span style="color: blue"&gt;exception &lt;/span&gt;StopException

&lt;span style="color: blue"&gt;type &lt;/span&gt;CountingAgent() =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;counter = MailboxProcessor.SpawnAgent((&lt;span style="color: blue"&gt;fun &lt;/span&gt;msg n &lt;span style="color: blue"&gt;-&amp;gt;
                    match &lt;/span&gt;msg &lt;span style="color: blue"&gt;with
                    &lt;/span&gt;| Increment m &lt;span style="color: blue"&gt;-&amp;gt;  &lt;/span&gt;n + m
                    | Stop &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;raise(StopException)
                    | Fetch replyChannel &lt;span style="color: blue"&gt;-&amp;gt;
                        do &lt;/span&gt;replyChannel.Reply(n)
                        n
                  ), 0, errorHandler = (&lt;span style="color: blue"&gt;fun &lt;/span&gt;_ _ _ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;StopProcessing))
    &lt;span style="color: blue"&gt;member &lt;/span&gt;a.Increment(n) = counter.Post(Increment(n))
    &lt;span style="color: blue"&gt;member &lt;/span&gt;a.Stop() = counter.Post(Stop)
    &lt;span style="color: blue"&gt;member &lt;/span&gt;a.Fetch() = counter.PostAndReply(&lt;span style="color: blue"&gt;fun &lt;/span&gt;replyChannel &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;Fetch(replyChannel))    
        
&lt;span style="color: blue"&gt;let &lt;/span&gt;counter2 = CountingAgent()
counter2.Increment(1)
counter2.Fetch()
counter2.Increment(2)
counter2.Fetch()
counter2.Stop()                             &lt;/pre&gt;

&lt;p&gt;Sometimes your agent doesn’t need a state, it is purely stateless. Something as simple as the following:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;echo = MailboxProcessor&amp;lt;_&amp;gt;.SpawnWorker(&lt;span style="color: blue"&gt;fun &lt;/span&gt;msg &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;%s&amp;quot; &lt;/span&gt;msg)&lt;/pre&gt;

&lt;p&gt;You can easily make that happen by using this toned down version of an agent, called worker:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static member public &lt;/span&gt;SpawnWorker(messageHandler,  ?timeout, ?timeoutHandler,?errorHandler) =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;timeout = defaultArg timeout (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;-1)
    &lt;span style="color: blue"&gt;let &lt;/span&gt;timeoutHandler = defaultArg timeoutHandler (&lt;span style="color: blue"&gt;fun &lt;/span&gt;_ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;ContinueProcessing(()))
    &lt;span style="color: blue"&gt;let &lt;/span&gt;errorHandler = defaultArg errorHandler (&lt;span style="color: blue"&gt;fun &lt;/span&gt;_ _ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;ContinueProcessing(()))
    MailboxProcessor.SpawnAgent((&lt;span style="color: blue"&gt;fun &lt;/span&gt;msg _ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;messageHandler msg; ()),&lt;br /&gt;                                 (), timeout, timeoutHandler,&lt;br /&gt;                                 (&lt;span style="color: blue"&gt;fun &lt;/span&gt;ex msg _ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;errorHandler ex msg))&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Given that they are parallel, you might want to run a whole bunch of them at the same time. You might want something that looks like a worker, but that, under the cover, execute each &lt;em&gt;messageHandler&lt;/em&gt; in parallel. Something like:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;msg1 = Message1 | Message2 &lt;span style="color: blue"&gt;of &lt;/span&gt;int | Message3 &lt;span style="color: blue"&gt;of &lt;/span&gt;string
            
&lt;span style="color: blue"&gt;let &lt;/span&gt;a = MailboxProcessor.SpawnParallelWorker(&lt;span style="color: blue"&gt;function
                &lt;/span&gt;| Message1 &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;Message1&amp;quot;&lt;/span&gt;;
                | Message2 n &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;Message2 %i&amp;quot; &lt;/span&gt;n;
                | Message3 _ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;failwith &lt;span style="color: maroon"&gt;&amp;quot;I failed&amp;quot;
                &lt;/span&gt;, 10
                , errorHandler = (&lt;span style="color: blue"&gt;fun &lt;/span&gt;ex _ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;%A&amp;quot; &lt;/span&gt;ex; ContinueProcessing()))


a.Post(Message1)
a.Post(Message2(100))
a.Post(Message3(&lt;span style="color: maroon"&gt;&amp;quot;abc&amp;quot;&lt;/span&gt;))
a.Post(Message2(100))&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;In this example, the different messages, are likely to cause things to print out of order. Notice the number 10 above which is how many workers you want to process your messages. This is implemented by round-robin messages to the various workers:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static member public &lt;/span&gt;SpawnParallelWorker(messageHandler, howMany, ?timeout,&lt;br /&gt;                                                        ?timeoutHandler,?errorHandler) =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;timeout = defaultArg timeout (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;-1)
    &lt;span style="color: blue"&gt;let &lt;/span&gt;timeoutHandler = defaultArg timeoutHandler (&lt;span style="color: blue"&gt;fun &lt;/span&gt;_ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;ContinueProcessing(()))
    &lt;span style="color: blue"&gt;let &lt;/span&gt;errorHandler = defaultArg errorHandler (&lt;span style="color: blue"&gt;fun &lt;/span&gt;_ _ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;ContinueProcessing(()))
    MailboxProcessor&amp;lt;'a&amp;gt;.SpawnAgent((&lt;span style="color: blue"&gt;fun &lt;/span&gt;msg (workers:MailboxProcessor&amp;lt;'a&amp;gt; array, index) &lt;span style="color: blue"&gt;-&amp;gt;
                                        &lt;/span&gt;workers.[index].Post msg
                                        (workers, (index + 1) % howMany))  
                                    , (Array.init howMany&lt;br /&gt;                                      (&lt;span style="color: blue"&gt;fun &lt;/span&gt;_ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;MailboxProcessor&amp;lt;'a&amp;gt;.SpawnWorker(&lt;br /&gt;                                                 messageHandler, timeout, timeoutHandler,&lt;br /&gt;                                                 errorHandler)), 0))&lt;/pre&gt;

&lt;p&gt;One drawback with the current code is that it doesn’t supports cancellations. It should be possible to wrap that too, but I haven’t done it (yet). If you don’t want to cut and paste the code, it is inside the AgentSystem.fs file &lt;a href="http://blogs.msdn.com/lucabol/archive/2009/06/12/lagent-an-agent-framework-in-f-part-iii-default-error-management.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9962927" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/F_2300_/">F#</category></item><item><title>The F#unctional Londoners Meetup Group</title><link>http://blogs.msdn.com/b/lucabol/archive/2010/02/11/the-f-unctional-londoners-meetup-group.aspx</link><pubDate>Thu, 11 Feb 2010 17:24:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9962054</guid><dc:creator>lucabol</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucabol/rsscomments.aspx?WeblogPostID=9962054</wfw:commentRss><comments>http://blogs.msdn.com/b/lucabol/archive/2010/02/11/the-f-unctional-londoners-meetup-group.aspx#comments</comments><description>&lt;p&gt;If you live in London and work with F#, you should join &lt;a title="http://www.meetup.com/FSharpLondon/" href="http://www.meetup.com/FSharpLondon/"&gt;http://www.meetup.com/FSharpLondon/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Also, if you don’t work with F#, but you’d like too, and you live in London you should join it. Also if you don’t live in London, but you’d like to and you’d like not to work on F# … Ok, you get the point.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9962054" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/F_2300_/">F#</category></item><item><title>LAgent: an agent framework in F# – Part X – ActiveObject</title><link>http://blogs.msdn.com/b/lucabol/archive/2009/12/10/lagent-an-agent-framework-in-f-part-x-activeobject.aspx</link><pubDate>Thu, 10 Dec 2009 18:09:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9935251</guid><dc:creator>lucabol</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucabol/rsscomments.aspx?WeblogPostID=9935251</wfw:commentRss><comments>http://blogs.msdn.com/b/lucabol/archive/2009/12/10/lagent-an-agent-framework-in-f-part-x-activeobject.aspx#comments</comments><description>&lt;p&gt;Download framework &lt;a href="http://code.msdn.microsoft.com/LAgent"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;All posts are here:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/05/29/lagent-an-agent-framework-in-f-part-i-workers-and-parallelworkers.aspx"&gt;Part I - Workers and ParallelWorkers&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/06/05/lagent-an-agent-framework-in-f-part-ii-agents-and-control-messages.aspx"&gt;Part II - Agents and control messages&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/06/12/lagent-an-agent-framework-in-f-part-iii-default-error-management.aspx"&gt;Part III - Default error management&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/06/19/lagent-an-agent-framework-in-f-part-iv-custom-error-management.aspx"&gt;Part IV - Custom error management&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/06/26/lagent-an-agent-framework-in-f-part-v-timeout-management.aspx"&gt;Part V - Timeout management&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/07/03/lagent-an-agent-framework-in-f-part-vi-hot-swapping-of-code-and-something-silly.aspx"&gt;Part VI - Hot swapping of code&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/07/10/lagent-an-agent-framework-in-f-part-vii-an-auction-application.aspx"&gt;Part VII - An auction framework&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/09/04/lagent-an-agent-framework-in-f-part-viii-implementing-mapreduce-user-model.aspx"&gt;Part VIII – Implementing MapReduce (user model)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/09/18/lagent-an-agent-framework-in-f-part-ix-counting-words.aspx"&gt;Part IX – Counting words …&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;If you stare long enough at agents, you start to realize that they are just ‘glorified locks’. They are a convenient programming model to protect a resource from concurrent access. The programming model is convenient because both the client and the server can write their code without worrying about concurrency problems, and yet the program runs in parallel. Protecting a resource sounds a lot like state encapsulation and the concept of state encapsulation is what object orientation is all about.&lt;/p&gt;  &lt;p&gt;So you start thinking if there is a way to enhance vanilla objects to make them agents. You want to reuse all the concepts that you are familiar with (i.e. inheritance, visibility rules, etc…) and you want your clients to call agents as if they were calling normal objects. Obviously, under the cover, the method calls won’t execute immediately, but they would be queued. Let’s look at an example.&lt;/p&gt;  &lt;p&gt;This is our simple counter agent:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;CounterMessage =
| Add &lt;span style="color: blue"&gt;of &lt;/span&gt;int
| Print

&lt;span style="color: blue"&gt;let &lt;/span&gt;counterF = &lt;span style="color: blue"&gt;fun &lt;/span&gt;msg count &lt;span style="color: blue"&gt;-&amp;gt;
    match &lt;/span&gt;msg &lt;span style="color: blue"&gt;with
    &lt;/span&gt;| Add(i)    &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;count + i
    | Print     &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;The value is %i&amp;quot; &lt;/span&gt;count; count
    
&lt;span style="color: blue"&gt;let &lt;/span&gt;c1 = spawnAgent counterF &lt;span style="color: brown"&gt;0
&lt;/span&gt;c1 &amp;lt;-- Add(&lt;span style="color: brown"&gt;3&lt;/span&gt;)
c1 &amp;lt;—Print&lt;/pre&gt;

&lt;p&gt;As nice as this looks, there are unfamiliar things in this model:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The communication is through messages. This requires packing and unpacking which, albeit easy in F#, is unfamiliar and feels like machinery that we’d like to get rid off. &lt;/li&gt;

  &lt;li&gt;The management of state is bizarre, it gets passed into the lambda and returned from it instead of being represented as fields and properties on the agent &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My best attempt at creating an object-like syntax follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;Counter() =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;w = &lt;span style="color: blue"&gt;new &lt;/span&gt;WorkQueue()
    &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;count = &lt;span style="color: brown"&gt;0
    &lt;/span&gt;&lt;span style="color: blue"&gt;member &lt;/span&gt;c.Add x = w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt; 
        &lt;/span&gt;count &amp;lt;- count + x
        )
    &lt;span style="color: blue"&gt;member &lt;/span&gt;c.Print () = w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt; 
        &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;The value is %i&amp;quot; &lt;/span&gt;count
        )&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;c = &lt;span style="color: blue"&gt;new &lt;/span&gt;Counter()
c.Add &lt;span style="color: brown"&gt;3
&lt;/span&gt;c.Print&lt;/pre&gt;

&lt;p&gt;With this syntax, you write your agents like you write your vanilla classes except:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;You need a private field of type &lt;em&gt;WorkQueue&lt;/em&gt; &lt;/li&gt;

  &lt;li&gt;You need to write your methods as lambdas passed to the &lt;em&gt;WorkQueue.Queue&lt;/em&gt; function &lt;/li&gt;

  &lt;li&gt;Your methods cannot return values &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The most worrisome of these constraints is 2. because you can easily forget about it. If you do forget, then everything compiles just fine, but it doesn’t do what you expect. That’s pure badness. I haven’t found a way to enforce it. This is a place where the language could help me. Other than that, the whole model works rather nicely.&lt;/p&gt;

&lt;p&gt;Regarding the third point, you can concoct a programming model that allows you to return values from your methods. Here it is:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;member &lt;/span&gt;c.CountTask = w.QueueWithTask(&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
    &lt;/span&gt;count
    )
&lt;span style="color: blue"&gt;member &lt;/span&gt;c.CountAsync = w.QueueWithAsync(&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
    &lt;/span&gt;count
    )&lt;/pre&gt;

&lt;pre class="code"&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;The count using Task is %i&amp;quot; &lt;/span&gt;(c.CountTask.Result)&lt;/pre&gt;

&lt;p&gt;The first method returns a &lt;em&gt;Task;&lt;/em&gt; the second method returns an &lt;em&gt;AsyncResultCell&lt;/em&gt;. Both are ways to represent a &lt;a href="http://en.wikipedia.org/wiki/Future_(programming)"&gt;promise&lt;/a&gt;. The latter allows a natural integration with the async block in F# as in the following code:&lt;/p&gt;

&lt;pre class="code"&gt;Async.RunSynchronously (
            async {
                &lt;span style="color: blue"&gt;let! &lt;/span&gt;count = c.CountAsync
                printfn &lt;span style="color: maroon"&gt;&amp;quot;The countusing Async is %i&amp;quot; &lt;/span&gt;count
            })&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As for myself, I don’t like methods returning values. Every time I use them, I end up going back and thinking about my problem in a traditional way, aka as method calls that return results, instead of thinking about it in a more actor oriented fashion. I end up waiting for these promises to be materialized and, by doing so, I limit the amount of parallelism that I unleash. As a matter of fact, the whole business of hiding the message passing nature of the programming model is dubious. It makes for a nicer syntax, but you need to make an extra effort in your mind to translate it to what it really is: just message passing with a nice syntactical veneer. I haven’t decided yet which model I like the most.&lt;/p&gt;

&lt;p&gt;You should have a sense of what &lt;em&gt;WorkQueue&lt;/em&gt; is. In essence, it is a &lt;em&gt;Mailbox&lt;/em&gt; of lambdas (look at the red bold code below). &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;WorkQueue() =
    &lt;font color="#ff0000"&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;workQueue = spawnWorker (&lt;span style="color: blue"&gt;fun &lt;/span&gt;f &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;f())&lt;/strong&gt;&lt;/font&gt;
    &lt;span style="color: blue"&gt;member &lt;/span&gt;w.Queue (f) = workQueue &amp;lt;-- f
    &lt;span style="color: blue"&gt;member &lt;/span&gt;w.QueueWithTask f : Task&amp;lt;'T&amp;gt; =
        &lt;span style="color: blue"&gt;let &lt;/span&gt;source = &lt;span style="color: blue"&gt;new &lt;/span&gt;TaskCompletionSource&amp;lt;_&amp;gt;()
        workQueue &amp;lt;-- (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;f() |&amp;gt; source.SetResult)
        source.Task
    &lt;span style="color: blue"&gt;member &lt;/span&gt;w.QueueWithAsync (f:unit &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'T) : Async&amp;lt;'T&amp;gt; =
        &lt;span style="color: blue"&gt;let &lt;/span&gt;result = &lt;span style="color: blue"&gt;new &lt;/span&gt;AsyncResultCell&amp;lt;'T&amp;gt;()
        workQueue &amp;lt;-- (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;f() |&amp;gt; result.RegisterResult )
        result.AsyncWaitResult
    &lt;span style="color: blue"&gt;member &lt;/span&gt;w.Restart () = workQueue &amp;lt;-! Restart
    &lt;span style="color: blue"&gt;member &lt;/span&gt;w.Stop () = workQueue &amp;lt;-! Stop
    &lt;span style="color: blue"&gt;member &lt;/span&gt;w.SetErrorHandler(h) =
        &lt;span style="color: blue"&gt;let &lt;/span&gt;managerF = &lt;span style="color: blue"&gt;fun &lt;/span&gt;(_, name:string, ex:Exception, _, _, _) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;h name ex                             
        &lt;span style="color: blue"&gt;let &lt;/span&gt;manager = spawnWorker managerF
        workQueue &amp;lt;-! SetManager manager
    &lt;span style="color: blue"&gt;member &lt;/span&gt;w.SetName(name) = workQueue &amp;lt;-! SetName(name)
    &lt;span style="color: blue"&gt;member &lt;/span&gt;w.SetQueueHandler(g) = workQueue &amp;lt;-! SetWorkerHandler(g)
    &lt;span style="color: blue"&gt;member &lt;/span&gt;w.SetTimeoutHandler(timeout, f) = workQueue &amp;lt;-! SetTimeoutHandler(timeout, f)&lt;/pre&gt;

&lt;p&gt;I implemented all the services that are in the message passing model. The two are equivalent as expressing power goes. In case you wonder how a real piece of code looks like using this model, here is an &lt;em&gt;ActiveObject&lt;/em&gt; version of the map reduce algorithm. One of these days, I will gather the strength to go trough this code and explain what it does, but not today :-)&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;#load &lt;/span&gt;&lt;span style="color: maroon"&gt;&amp;quot;AgentSystem.fs&amp;quot;
&lt;/span&gt;&lt;span style="color: blue"&gt;open &lt;/span&gt;AgentSystem.LAgent
&lt;span style="color: blue"&gt;open &lt;/span&gt;System
&lt;span style="color: blue"&gt;open &lt;/span&gt;System.Collections
&lt;span style="color: blue"&gt;open &lt;/span&gt;System.Collections.Generic
&lt;span style="color: blue"&gt;open &lt;/span&gt;System.Threading

&lt;span style="color: blue"&gt;type &lt;/span&gt;IOutput&amp;lt;'out_key, 'out_value&amp;gt; =
    &lt;span style="color: blue"&gt;abstract &lt;/span&gt;Reduced: 'out_key &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;seq&amp;lt;'out_value&amp;gt; &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;unit
    &lt;span style="color: blue"&gt;abstract &lt;/span&gt;MapReduceDone: unit &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;unit
    
&lt;span style="color: blue"&gt;type &lt;/span&gt;Mapper&amp;lt;'in_key, 'in_value, 'my_out_key, 'out_value &lt;span style="color: blue"&gt;when &lt;/span&gt;'my_out_key : comparison&amp;gt;&lt;br /&gt;                                                      (map:'in_key &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'in_value &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;seq&amp;lt;'my_out_key * 'out_value&amp;gt;, i, partitionF) =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;w = &lt;span style="color: blue"&gt;new &lt;/span&gt;WorkQueue()
    &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;reducerTracker: BitArray = &lt;span style="color: blue"&gt;null
    let mutable &lt;/span&gt;controller = Unchecked.defaultof&amp;lt;Controller&amp;lt;'in_key, 'in_value, 'my_out_key, 'out_value&amp;gt;&amp;gt;
    &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;reducers = Unchecked.defaultof&amp;lt;Reducer&amp;lt;'in_key, 'in_value, 'my_out_key, 'out_value&amp;gt; array&amp;gt;
    &lt;span style="color: blue"&gt;member &lt;/span&gt;m.Init c reds =
        w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
            &lt;/span&gt;controller &amp;lt;- c
            reducers &amp;lt;- reds
            reducerTracker &amp;lt;- &lt;span style="color: blue"&gt;new &lt;/span&gt;BitArray(reducers.Length, &lt;span style="color: blue"&gt;false&lt;/span&gt;))
    &lt;span style="color: blue"&gt;member &lt;/span&gt;m.Process inKey inValue =
        w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
            let &lt;/span&gt;outKeyValues = map inKey inValue
            outKeyValues |&amp;gt; Seq.iter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;(outKey, outValue) &lt;span style="color: blue"&gt;-&amp;gt;
                                        let &lt;/span&gt;reducerUsed = partitionF outKey (reducers.Length)
                                        reducerTracker.Set(reducerUsed, &lt;span style="color: blue"&gt;true&lt;/span&gt;)
                                        reducers.[reducerUsed].Add(outKey, outValue)))
    &lt;span style="color: blue"&gt;member &lt;/span&gt;m.Done () =
        w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
            &lt;/span&gt;controller.MapDone i reducerTracker)
    &lt;span style="color: blue"&gt;member &lt;/span&gt;m.Stop () = w.Stop ()            
    
&lt;span style="color: blue"&gt;and &lt;/span&gt;Reducer&amp;lt;'in_key, 'in_value, 'out_key, 'out_value &lt;span style="color: blue"&gt;when &lt;/span&gt;'out_key :&lt;br /&gt;                     comparison&amp;gt;(reduce:'out_key &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;seq&amp;lt;'out_value&amp;gt; &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;seq&amp;lt;'out_value&amp;gt;, i, output:IOutput&amp;lt;'out_key, 'out_value&amp;gt;) =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;w = &lt;span style="color: blue"&gt;new &lt;/span&gt;WorkQueue()
    &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;workItems = &lt;span style="color: blue"&gt;new &lt;/span&gt;List&amp;lt;'out_key * 'out_value&amp;gt;()
    &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;controller = Unchecked.defaultof&amp;lt;Controller&amp;lt;'in_key, 'in_value, 'out_key, 'out_value&amp;gt;&amp;gt;
    &lt;span style="color: blue"&gt;member &lt;/span&gt;r.Init c =
        w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
            &lt;/span&gt;controller &amp;lt;- c)
    &lt;span style="color: blue"&gt;member &lt;/span&gt;r.StartReduction () =
        w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
            &lt;/span&gt;workItems
            |&amp;gt; Seq.groupBy fst
            |&amp;gt; Seq.sortBy fst
            |&amp;gt; Seq.map (&lt;span style="color: blue"&gt;fun &lt;/span&gt;(key, values) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;(key, reduce key (values |&amp;gt; Seq.map snd)))
            |&amp;gt; Seq.iter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;(key, value) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;output.Reduced key value)
            controller.ReductionDone i) 
    &lt;span style="color: blue"&gt;member &lt;/span&gt;r.Add (outKey:'out_key, outValue:'out_value) : unit =
        w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
            &lt;/span&gt;workItems.Add((outKey, outValue)))
    &lt;span style="color: blue"&gt;member &lt;/span&gt;m.Stop () = w.Stop ()            
                
&lt;span style="color: blue"&gt;and &lt;/span&gt;Controller&amp;lt;'in_key, 'in_value, 'out_key, 'out_value &lt;span style="color: blue"&gt;when &lt;/span&gt;'out_key : comparison&amp;gt;(output:IOutput&amp;lt;'out_key, 'out_value&amp;gt;) =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;w = &lt;span style="color: blue"&gt;new &lt;/span&gt;WorkQueue()
    &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;mapperTracker: BitArray = &lt;span style="color: blue"&gt;null
    let mutable &lt;/span&gt;reducerUsedByMappers: BitArray = &lt;span style="color: blue"&gt;null
    let mutable &lt;/span&gt;reducerDone: BitArray = &lt;span style="color: blue"&gt;null
    let mutable &lt;/span&gt;mappers = Unchecked.defaultof&amp;lt;Mapper&amp;lt;'in_key, 'in_value, 'out_key, 'out_value&amp;gt; array&amp;gt;
    &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;reducers = Unchecked.defaultof&amp;lt;Reducer&amp;lt;'in_key, 'in_value, 'out_key, 'out_value&amp;gt; array&amp;gt;
    &lt;span style="color: blue"&gt;let &lt;/span&gt;BAtoSeq (b:BitArray) = [&lt;span style="color: blue"&gt;for &lt;/span&gt;x &lt;span style="color: blue"&gt;in &lt;/span&gt;b &lt;span style="color: blue"&gt;do yield &lt;/span&gt;x]
    &lt;span style="color: blue"&gt;member &lt;/span&gt;c.Init maps reds =
        w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
            &lt;/span&gt;mappers &amp;lt;- maps
            reducers &amp;lt;- reds
            mapperTracker &amp;lt;- &lt;span style="color: blue"&gt;new &lt;/span&gt;BitArray(mappers.Length, &lt;span style="color: blue"&gt;false&lt;/span&gt;)
            reducerUsedByMappers &amp;lt;- &lt;span style="color: blue"&gt;new &lt;/span&gt;BitArray(reducers.Length, &lt;span style="color: blue"&gt;false&lt;/span&gt;)
            reducerDone &amp;lt;- &lt;span style="color: blue"&gt;new &lt;/span&gt;BitArray(reducers.Length, &lt;span style="color: blue"&gt;false&lt;/span&gt;))
    &lt;span style="color: blue"&gt;member &lt;/span&gt;c.MapDone (i : int) (reducerTracker : BitArray) : unit =
        w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
            &lt;/span&gt;mapperTracker.Set(i, &lt;span style="color: blue"&gt;true&lt;/span&gt;)
            &lt;span style="color: blue"&gt;let &lt;/span&gt;reducerUsedByMappers = reducerUsedByMappers.Or(reducerTracker)
            &lt;span style="color: blue"&gt;if &lt;/span&gt;not( BAtoSeq mapperTracker |&amp;gt; Seq.exists(&lt;span style="color: blue"&gt;fun &lt;/span&gt;bit &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;bit = &lt;span style="color: blue"&gt;false&lt;/span&gt;)) &lt;span style="color: blue"&gt;then
                &lt;/span&gt;BAtoSeq reducerUsedByMappers |&amp;gt; Seq.iteri (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i r &lt;span style="color: blue"&gt;-&amp;gt; if &lt;/span&gt;r = &lt;span style="color: blue"&gt;true then &lt;/span&gt;reducers.[i].StartReduction ())
                mappers |&amp;gt; Seq.iter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;m &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;m.Stop ())
              )
    &lt;span style="color: blue"&gt;member &lt;/span&gt;c.ReductionDone (i: int) : unit =
        w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
            &lt;/span&gt;reducerDone.Set(i, &lt;span style="color: blue"&gt;true&lt;/span&gt;)
            &lt;span style="color: blue"&gt;if &lt;/span&gt;BAtoSeq reducerDone |&amp;gt; Seq.forall2 (&lt;span style="color: blue"&gt;fun &lt;/span&gt;x y &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;x = y) (BAtoSeq reducerUsedByMappers) &lt;span style="color: blue"&gt;then
                &lt;/span&gt;output.MapReduceDone ()
                reducers |&amp;gt; Seq.iter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;r &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;r.Stop ())
                c.Stop()
             )         
    &lt;span style="color: blue"&gt;member &lt;/span&gt;m.Stop () = w.Stop ()
                
&lt;span style="color: blue"&gt;let &lt;/span&gt;mapReduce   (inputs:seq&amp;lt;'in_key * 'in_value&amp;gt;)
                (map:'in_key &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'in_value &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;seq&amp;lt;'out_key * 'out_value&amp;gt;)
                (reduce:'out_key &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;seq&amp;lt;'out_value&amp;gt; &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;seq&amp;lt;'out_value&amp;gt;)
                (output:IOutput&amp;lt;'out_key, 'out_value&amp;gt;)
                M R partitionF =
                    
    &lt;span style="color: blue"&gt;let &lt;/span&gt;len = inputs |&amp;gt; Seq.length
    &lt;span style="color: blue"&gt;let &lt;/span&gt;M = &lt;span style="color: blue"&gt;if &lt;/span&gt;len &amp;lt; M &lt;span style="color: blue"&gt;then &lt;/span&gt;len &lt;span style="color: blue"&gt;else &lt;/span&gt;M
    
    &lt;span style="color: blue"&gt;let &lt;/span&gt;mappers = Array.init M (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i &lt;span style="color: blue"&gt;-&amp;gt; new &lt;/span&gt;Mapper&amp;lt;'in_key, 'in_value, 'out_key, 'out_value&amp;gt;(map, i, partitionF))
    &lt;span style="color: blue"&gt;let &lt;/span&gt;reducers = Array.init R (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i &lt;span style="color: blue"&gt;-&amp;gt; new &lt;/span&gt;Reducer&amp;lt;'in_key, 'in_value, 'out_key, 'out_value&amp;gt;(reduce, i, output))
    &lt;span style="color: blue"&gt;let &lt;/span&gt;controller = &lt;span style="color: blue"&gt;new &lt;/span&gt;Controller&amp;lt;'in_key, 'in_value, 'out_key, 'out_value&amp;gt;(output)
    
    mappers |&amp;gt; Array.iter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;m &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;m.Init controller reducers)
    reducers |&amp;gt; Array.iter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;r &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;r. Init controller )
    controller.Init mappers reducers
    
    inputs |&amp;gt; Seq.iteri (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i (inKey, inValue) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;mappers.[i % M].Process inKey inValue)
    mappers |&amp;gt; Seq.iter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;m &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;m.Done ())    

&lt;span style="color: blue"&gt;let &lt;/span&gt;partitionF = &lt;span style="color: blue"&gt;fun &lt;/span&gt;key M &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;abs(key.GetHashCode()) % M 

&lt;span style="color: blue"&gt;let &lt;/span&gt;map = &lt;span style="color: blue"&gt;fun &lt;/span&gt;(fileName:string) (fileContent:string) &lt;span style="color: blue"&gt;-&amp;gt;
            let &lt;/span&gt;l = &lt;span style="color: blue"&gt;new &lt;/span&gt;List&amp;lt;string * int&amp;gt;()
            &lt;span style="color: blue"&gt;let &lt;/span&gt;wordDelims = [|&lt;span style="color: maroon"&gt;' '&lt;/span&gt;;&lt;span style="color: maroon"&gt;','&lt;/span&gt;;&lt;span style="color: maroon"&gt;';'&lt;/span&gt;;&lt;span style="color: maroon"&gt;'.'&lt;/span&gt;;&lt;span style="color: maroon"&gt;':'&lt;/span&gt;;&lt;span style="color: maroon"&gt;'?'&lt;/span&gt;;&lt;span style="color: maroon"&gt;'!'&lt;/span&gt;;&lt;span style="color: maroon"&gt;'('&lt;/span&gt;;&lt;span style="color: maroon"&gt;')'&lt;/span&gt;;&lt;span style="color: maroon"&gt;'\n'&lt;/span&gt;;&lt;span style="color: maroon"&gt;'\t'&lt;/span&gt;;&lt;span style="color: maroon"&gt;'\f'&lt;/span&gt;;&lt;span style="color: maroon"&gt;'\r'&lt;/span&gt;;&lt;span style="color: maroon"&gt;'\b'&lt;/span&gt;|]
            fileContent.Split(wordDelims) |&amp;gt; Seq.iter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;word &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;l.Add((word, &lt;span style="color: brown"&gt;1&lt;/span&gt;)))
            l :&amp;gt; seq&amp;lt;string * int&amp;gt;
                                  
&lt;span style="color: blue"&gt;let &lt;/span&gt;reduce = &lt;span style="color: blue"&gt;fun &lt;/span&gt;key (values:seq&amp;lt;int&amp;gt;) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;[values |&amp;gt; Seq.sum] |&amp;gt; seq&amp;lt;int&amp;gt;

&lt;span style="color: blue"&gt;let &lt;/span&gt;printer () =
  { &lt;span style="color: blue"&gt;new &lt;/span&gt;IOutput&amp;lt;string, int&amp;gt; &lt;span style="color: blue"&gt;with
        member &lt;/span&gt;o.Reduced key values = printfn &lt;span style="color: maroon"&gt;&amp;quot;%A %A&amp;quot; &lt;/span&gt;key values
        &lt;span style="color: blue"&gt;member &lt;/span&gt;o.MapReduceDone () = printfn &lt;span style="color: maroon"&gt;&amp;quot;All done!!&amp;quot;&lt;/span&gt;}
    
&lt;span style="color: blue"&gt;let &lt;/span&gt;testInput =&lt;br /&gt;     [&lt;span style="color: maroon"&gt;&amp;quot;File1&amp;quot;&lt;/span&gt;, &lt;span style="color: maroon"&gt;&amp;quot;I was going to the airport when I saw someone crossing&amp;quot;&lt;/span&gt;; &lt;span style="color: maroon"&gt;&amp;quot;File2&amp;quot;&lt;/span&gt;, &lt;span style="color: maroon"&gt;&amp;quot;I was going home when I saw you coming toward me&amp;quot;&lt;/span&gt;]   
mapReduce testInput map reduce (printer ()) &lt;span style="color: brown"&gt;2 2 &lt;/span&gt;partitionF

&lt;span style="color: blue"&gt;open &lt;/span&gt;System.IO
&lt;span style="color: blue"&gt;open &lt;/span&gt;System.Text

&lt;span style="color: blue"&gt;let &lt;/span&gt;gatherer(step) =
  &lt;span style="color: blue"&gt;let &lt;/span&gt;w = &lt;span style="color: blue"&gt;new &lt;/span&gt;WorkQueue()
  &lt;span style="color: blue"&gt;let &lt;/span&gt;data = &lt;span style="color: blue"&gt;new &lt;/span&gt;List&amp;lt;string * int&amp;gt;()
  &lt;span style="color: blue"&gt;let &lt;/span&gt;counter = ref &lt;span style="color: brown"&gt;0 
  &lt;/span&gt;{ &lt;span style="color: blue"&gt;new &lt;/span&gt;IOutput&amp;lt;string, int&amp;gt; &lt;span style="color: blue"&gt;with
        member &lt;/span&gt;o.Reduced key values =
            w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
                if &lt;/span&gt;!counter % step = &lt;span style="color: brown"&gt;0 &lt;/span&gt;&lt;span style="color: blue"&gt;then
                    &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;Processed %i words. Now processing %s&amp;quot; &lt;/span&gt;!counter key 
                data.Add((key, values |&amp;gt; Seq.hd))
                counter := !counter + &lt;span style="color: brown"&gt;1&lt;/span&gt;)
        &lt;span style="color: blue"&gt;member &lt;/span&gt;o.MapReduceDone () =
            w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
                &lt;/span&gt;data
                |&amp;gt; Seq.distinctBy (&lt;span style="color: blue"&gt;fun &lt;/span&gt;(key, _) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;key.ToLower())
                |&amp;gt; Seq.filter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;(key, _) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;not(key = &lt;span style="color: maroon"&gt;&amp;quot;&amp;quot; &lt;/span&gt;|| key = &lt;span style="color: maroon"&gt;&amp;quot;\&amp;quot;&amp;quot; &lt;/span&gt;|| (fst (Double.TryParse(key)))))
                |&amp;gt; Seq.to_array
                |&amp;gt; Array.sortBy snd
                |&amp;gt; Array.rev
                |&amp;gt; Seq.take &lt;span style="color: brown"&gt;20
                &lt;/span&gt;|&amp;gt; Seq.iter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;(key, value) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;%A\t\t%A&amp;quot; &lt;/span&gt;key value)
                printfn &lt;span style="color: maroon"&gt;&amp;quot;All done!!&amp;quot;&lt;/span&gt;)           
        }
                        
&lt;span style="color: blue"&gt;let &lt;/span&gt;splitBook howManyBlocks fileName =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;buffers = Array.init howManyBlocks (&lt;span style="color: blue"&gt;fun &lt;/span&gt;_ &lt;span style="color: blue"&gt;-&amp;gt; new &lt;/span&gt;StringBuilder())
    fileName
    |&amp;gt; File.ReadAllLines
    |&amp;gt; Array.iteri (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i line &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;buffers.[i % (howManyBlocks)].Append(line) |&amp;gt; ignore)
    buffers

&lt;span style="color: blue"&gt;let &lt;/span&gt;blocks1 = &lt;span style="color: blue"&gt;__SOURCE_DIRECTORY__ &lt;/span&gt;+ &lt;span style="color: maroon"&gt;&amp;quot;\kjv10.txt&amp;quot; &lt;/span&gt;|&amp;gt; splitBook &lt;span style="color: brown"&gt;100
&lt;/span&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;blocks2 = &lt;span style="color: blue"&gt;__SOURCE_DIRECTORY__ &lt;/span&gt;+ &lt;span style="color: maroon"&gt;&amp;quot;\warandpeace.txt&amp;quot; &lt;/span&gt;|&amp;gt; splitBook &lt;span style="color: brown"&gt;100
&lt;/span&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;input =
    blocks1
    |&amp;gt; Array.append blocks2
    |&amp;gt; Array.mapi (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i b &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;i.ToString(), b.ToString())
        
&lt;span style="color: green"&gt;//mapReduce input map reduce (gatherer(1000)) 20 20 partitionF

&lt;/span&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;BookSplitter () =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;blocks = &lt;span style="color: blue"&gt;new &lt;/span&gt;List&amp;lt;string * string&amp;gt;()
    &lt;span style="color: blue"&gt;member &lt;/span&gt;b.Split howManyBlocks fileName =
            &lt;span style="color: blue"&gt;let &lt;/span&gt;b =
                fileName
                |&amp;gt; splitBook howManyBlocks
                |&amp;gt; Array.mapi (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i b &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;i.ToString(), b.ToString())
            blocks.AddRange(b)
    &lt;span style="color: blue"&gt;member &lt;/span&gt;b.Blocks () =
            blocks.ToArray() :&amp;gt; seq&amp;lt;string * string&amp;gt;

&lt;span style="color: blue"&gt;type &lt;/span&gt;WordCounter () =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;w = &lt;span style="color: blue"&gt;new &lt;/span&gt;WorkQueue()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;words = &lt;span style="color: blue"&gt;new &lt;/span&gt;Dictionary&amp;lt;string,int&amp;gt;()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;worker(wordCounter:WordCounter, ev:EventWaitHandle) =
          &lt;span style="color: blue"&gt;let &lt;/span&gt;w1 = &lt;span style="color: blue"&gt;new &lt;/span&gt;WorkQueue()
          { &lt;span style="color: blue"&gt;new &lt;/span&gt;IOutput&amp;lt;string, int&amp;gt; &lt;span style="color: blue"&gt;with
                member &lt;/span&gt;o.Reduced key values =
                    w1.Queue (&lt;span style="color: blue"&gt;fun&lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
                        &lt;/span&gt;wordCounter.AddWord key (values |&amp;gt; Seq.hd))
                &lt;span style="color: blue"&gt;member &lt;/span&gt;o.MapReduceDone () =
                    w1.Queue(&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
                        &lt;/span&gt;ev.Set() |&amp;gt; ignore)
           }
    &lt;span style="color: blue"&gt;member &lt;/span&gt;c.AddWord word count =
            &lt;span style="color: blue"&gt;let &lt;/span&gt;exist, value = words.TryGetValue(word)
            &lt;span style="color: blue"&gt;if &lt;/span&gt;exist &lt;span style="color: blue"&gt;then
                &lt;/span&gt;words.[word] &amp;lt;- value + count
            &lt;span style="color: blue"&gt;else
                &lt;/span&gt;words.Add(word, count)
    &lt;span style="color: blue"&gt;member &lt;/span&gt;c.Add fileName =
        w.Queue (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
            let &lt;/span&gt;s = &lt;span style="color: blue"&gt;new &lt;/span&gt;BookSplitter()
            fileName |&amp;gt; s.Split &lt;span style="color: brown"&gt;100
            &lt;/span&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;ev = &lt;span style="color: blue"&gt;new &lt;/span&gt;EventWaitHandle(&lt;span style="color: blue"&gt;false&lt;/span&gt;, EventResetMode.AutoReset)
            &lt;span style="color: blue"&gt;let &lt;/span&gt;blocks = s.Blocks ()
            mapReduce blocks map reduce (worker(c, ev)) &lt;span style="color: brown"&gt;20 20 &lt;/span&gt;partitionF
            ev.WaitOne() |&amp;gt; ignore
            )  
    &lt;span style="color: blue"&gt;member &lt;/span&gt;c.Words =
        w.QueueWithAsync (&lt;span style="color: blue"&gt;fun &lt;/span&gt;() &lt;span style="color: blue"&gt;-&amp;gt;
            &lt;/span&gt;words |&amp;gt; Seq.to_array |&amp;gt; Array.map (&lt;span style="color: blue"&gt;fun &lt;/span&gt;kv &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;kv.Key, kv.Value)
        )    

&lt;span style="color: blue"&gt;let &lt;/span&gt;wc = &lt;span style="color: blue"&gt;new &lt;/span&gt;WordCounter()
wc.Add (&lt;span style="color: blue"&gt;__SOURCE_DIRECTORY__ &lt;/span&gt;+ &lt;span style="color: maroon"&gt;&amp;quot;\kjv10.txt&amp;quot;&lt;/span&gt;)
wc.Add (&lt;span style="color: blue"&gt;__SOURCE_DIRECTORY__ &lt;/span&gt;+ &lt;span style="color: maroon"&gt;&amp;quot;\warandpeace.txt&amp;quot;&lt;/span&gt;)

&lt;span style="color: blue"&gt;let &lt;/span&gt;wordsToPrint = async {
                    &lt;span style="color: blue"&gt;let! &lt;/span&gt;words = wc.Words
                    &lt;span style="color: blue"&gt;return &lt;/span&gt;words
                        |&amp;gt; Seq.distinctBy (&lt;span style="color: blue"&gt;fun &lt;/span&gt;(key, _) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;key.ToLower())
                        |&amp;gt; Seq.filter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;(key, _) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;not(key = &lt;span style="color: maroon"&gt;&amp;quot;&amp;quot; &lt;/span&gt;|| key = &lt;span style="color: maroon"&gt;&amp;quot;\&amp;quot;&amp;quot; &lt;/span&gt;|| (fst (Double.TryParse(key)))))
                        |&amp;gt; Seq.to_array
                        |&amp;gt; Array.sortBy snd
                        |&amp;gt; Array.rev
                        |&amp;gt; Seq.take &lt;span style="color: brown"&gt;20
                        &lt;/span&gt;|&amp;gt; Seq.iter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;(key, value) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;%A\t\t%A&amp;quot; &lt;/span&gt;key value)}


Async.RunSynchronously wordsToPrint
                        
Thread.Sleep(&lt;span style="color: brown"&gt;15000&lt;/span&gt;)
printfn &lt;span style="color: maroon"&gt;&amp;quot;Closed session&amp;quot;               
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9935251" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/F_2300_/">F#</category></item><item><title>My PDC session is online - “Future directions for C# and Visual Basic”</title><link>http://blogs.msdn.com/b/lucabol/archive/2009/11/23/my-pdc-session-is-online-future-directions-for-c-and-visual-basic.aspx</link><pubDate>Mon, 23 Nov 2009 21:17:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9927546</guid><dc:creator>lucabol</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucabol/rsscomments.aspx?WeblogPostID=9927546</wfw:commentRss><comments>http://blogs.msdn.com/b/lucabol/archive/2009/11/23/my-pdc-session-is-online-future-directions-for-c-and-visual-basic.aspx#comments</comments><description>&lt;p&gt;In case you are training to play the part of a first generation Italian immigrant in a Broadway show or if you want to understand why I never short msft stock: &lt;a title="http://microsoftpdc.com/Sessions/FT11" href="http://microsoftpdc.com/Sessions/FT11"&gt;http://microsoftpdc.com/Sessions/FT11&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9927546" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/C_2300_+Programming/">C# Programming</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/-NET+Futures/">.NET Futures</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/VB/">VB</category></item><item><title>Luca at PDC 2009 next week</title><link>http://blogs.msdn.com/b/lucabol/archive/2009/11/13/luca-at-pdc-2009-next-week.aspx</link><pubDate>Fri, 13 Nov 2009 18:49:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9922139</guid><dc:creator>lucabol</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucabol/rsscomments.aspx?WeblogPostID=9922139</wfw:commentRss><comments>http://blogs.msdn.com/b/lucabol/archive/2009/11/13/luca-at-pdc-2009-next-week.aspx#comments</comments><description>&lt;p&gt;I’ll be in Los Angeles next week for PDC 2009. My session is called “&lt;a href="http://microsoftpdc.com/Sessions/FT11"&gt;Future Directions for C# and Visual Basic&lt;/a&gt;” and it comes on Tuesday immediately after the first keynote.&lt;/p&gt;  &lt;p&gt;I’m planning on spending the first half of the session talking about the biggest trends influencing C# and VB. The second part will be about more future looking features we are playing with these days.&lt;/p&gt;  &lt;p&gt;My deck has just two slides, as always. It’s all about watching me typing code on stage, hoping that I make crazy errors …&lt;/p&gt;  &lt;p&gt;There are several other interesting language related sessions at PDC. Here is a list of them (please notice that the times might change). Also feel free to drop by the languages booth and chat. My team and I will be there most of the time.&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="0"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;&lt;b&gt;Day&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;&lt;b&gt;Time&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Title&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;&lt;b&gt;Presenter&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;&lt;b&gt;Room&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Tues 11/17&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;11:00 - 12:00&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Future Directions for C# and Visual Basic&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Luca Bolognese&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;Hall F&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Tues 11/17&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;11:00 - 12:00&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Accelerated Windows Application Development with Microsoft Visual C++ 2010&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Boris Jabes&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;408B&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Tues 11/17&lt;b&gt;&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;12:30 - 13:15&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Dynamic Binding in C# 4&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Mads Torgersen&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;Hall F&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Tues 11/17&lt;b&gt;&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;12:30 - 13:15&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Using Dynamic Languages to Build Scriptable Applications&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Dino Viehland&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;403AB&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Tues 11/17&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;13:30 - 14:30&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;C++ Forever: Interactive Applications in the Age of Manycore&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Rick Molloy&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;515B&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Tues 11/17&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;16:30 - 17:30&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Manycore and the Microsoft .NET Framework 4: A Match Made in Microsoft Visual Studio 2010&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Stephen Toub&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;502A&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Tues 11/17&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;16:30 - 17:30&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Code Contracts and Pex: Power Charge Your Assertions and Unit Tests&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Mike Barnett, Nikolai Tillmann&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;408A&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Wed 11/18&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;11:30 - 12:30&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Microsoft Perspectives on the Future of Programming&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Panel&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;Petree Hall C&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Wed 11/18&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;12:00 - 13:00&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;F# for Architects – Hitting the Sweet Spot&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Chris Smith&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;VS and .NET Pavillion&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Wed 11/18&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;13:00 - 13:45&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Code Like the Wind with Microsoft Visual Basic 2010&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Lucian Wischik&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;Petree Hall D&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Wed 11/18&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;13:00 - 13:45&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Future of Garbage Collection&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Patrick Dussud&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;Petree Hall C&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Wed 11/18&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;13:00 - 13:45&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Microsoft Project Code Name “M”: The Data and Modeling Language&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Don Box, Jeff Pinkston&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;408A&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Thu&lt;/p&gt;          &lt;p&gt;11/19&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;08:30 - 09:30&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;PLINQ: LINQ, but Faster!&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Ed Essey, Igor Ostrovsky&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;515A&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Thu&lt;/p&gt;          &lt;p&gt;11/19&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;10:00 - 11:00&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;A Lap around Microsoft Visual Studio 2010 for the Visual Basic Developer &lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Lisa Feigenbaum&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;VS and .NET Pavillion&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Thu&lt;/p&gt;          &lt;p&gt;11/19&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;10:00 - 11:00&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Axum: A .NET Language for Safe and Scalable Concurrency&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Niklas Gustafsson&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;515A&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Thu&lt;/p&gt;          &lt;p&gt;11/19&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;11:30 - 12:30&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;F# for Parallel and Asynchronous Programming&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Luke Hoban&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;515A&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Thu&lt;/p&gt;          &lt;p&gt;11/19&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;12:45 - 13:30&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Microsoft Visual C# IDE Tips and Tricks&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;DJ Park&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;Petree Hall D&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="62"&gt;         &lt;p&gt;Thu&lt;/p&gt;          &lt;p&gt;11/19&lt;b&gt;&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="96"&gt;         &lt;p&gt;12:45 - 13:30&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="502"&gt;         &lt;p&gt;&lt;b&gt;Microsoft Visual Basic IDE Tips and Tricks&lt;/b&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="147"&gt;         &lt;p&gt;Dustin Campbell&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="117"&gt;         &lt;p&gt;Petree Hall C&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9922139" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/C_2300_+Programming/">C# Programming</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/-NET+Futures/">.NET Futures</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/F_2300_/">F#</category><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/VB/">VB</category></item><item><title>Becoming really rich with C#</title><link>http://blogs.msdn.com/b/lucabol/archive/2009/09/22/becoming-really-rich-with-c.aspx</link><pubDate>Wed, 23 Sep 2009 02:40:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9896982</guid><dc:creator>lucabol</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucabol/rsscomments.aspx?WeblogPostID=9896982</wfw:commentRss><comments>http://blogs.msdn.com/b/lucabol/archive/2009/09/22/becoming-really-rich-with-c.aspx#comments</comments><description>&lt;P&gt;Or maybe not, please do not hold me responsible if you lose money following this system. Having said that, it is my opinion that there are very few concepts that are important in investing. Three big ones are value, diversification and momentum. This post is about the latter two and how to use C# to create a simple trading system that uses both.&lt;/P&gt;
&lt;P&gt;Diversification is ‘not put all your eggs in one basket’ (contrary to ‘put all of them in one basket and watch that basket’). I don’t believe you can ‘watch’ very much in financial markets, so I tend to prefer diversification.&lt;/P&gt;
&lt;P&gt;Momentum is a mysterious tendency of financial prices that have risen the most in the recent past, to continue outperforming in the close future. In essence, buying the top stocks/sectors/asset classes tends to outperform buying the bottom ones over horizons from three months to one year.&lt;/P&gt;
&lt;P&gt;The idea then is to rank some assets (i.e. ETFs) by how fast they have risen in the past, go long the top ones and short the bottom ones. There are hundreds of variations of this basic strategy, we’ll add the rule that we won’t buy assets that are below their 200 days moving average or sell short assets that are above it.&lt;/P&gt;
&lt;P&gt;I’m writing this code with VS 2010 Beta 2 (which hasn’t shipped yet). It should be trivial to modify it to run on B1 (or maybe it does run on it already). I attach the code and data files to this post.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;struct &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Event &lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;internal &lt;/SPAN&gt;Event(&lt;SPAN style="COLOR: #2b91af"&gt;DateTime &lt;/SPAN&gt;date, &lt;SPAN style="COLOR: blue"&gt;double &lt;/SPAN&gt;price) { Date = date; Price = price; }
    &lt;SPAN style="COLOR: blue"&gt;internal readonly &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DateTime &lt;/SPAN&gt;Date;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly double &lt;/SPAN&gt;Price;
}&lt;/PRE&gt;
&lt;P&gt;We’ll use this simple structure to load the closing price for a particular date. My use of internal is kind of bizarre. Actually the whole code might look strange. It is an interesting (maybe un-elegant) mix of object orientation and functional programming.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Summary &lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;internal &lt;/SPAN&gt;Summary(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;ticker, &lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;name, &lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;assetClass,
                    &lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;assetSubClass, &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt;? weekly, &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt;? fourWeeks,
                    &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt;? threeMonths, &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt;? sixMonths, &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt;? oneYear,
                    &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt;? stdDev, &lt;SPAN style="COLOR: blue"&gt;double &lt;/SPAN&gt;price, &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt;? mav200) {
        Ticker = ticker;
        Name = name;
        AssetClass = assetClass;
        AssetSubClass = assetSubClass;
        &lt;SPAN style="COLOR: green"&gt;// Abracadabra ...
        &lt;/SPAN&gt;LRS = (fourWeeks + threeMonths + sixMonths + oneYear) / &lt;SPAN style="COLOR: brown"&gt;4&lt;/SPAN&gt;;
        Weekly = weekly;
        FourWeeks = fourWeeks;
        ThreeMonths = threeMonths;
        SixMonths = sixMonths;
        OneYear = oneYear;
        StdDev = stdDev;
        Mav200 = mav200;
        Price = price;
    }
    &lt;SPAN style="COLOR: blue"&gt;internal readonly string &lt;/SPAN&gt;Ticker;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly string &lt;/SPAN&gt;Name;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly string &lt;/SPAN&gt;AssetClass;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly string &lt;/SPAN&gt;AssetSubClass;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly double&lt;/SPAN&gt;? LRS;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly double&lt;/SPAN&gt;? Weekly;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly double&lt;/SPAN&gt;? FourWeeks;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly double&lt;/SPAN&gt;? ThreeMonths;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly double&lt;/SPAN&gt;? SixMonths;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly double&lt;/SPAN&gt;? OneYear;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly double&lt;/SPAN&gt;? StdDev;
    &lt;SPAN style="COLOR: blue"&gt;internal readonly double&lt;/SPAN&gt;? Mav200;
    &lt;SPAN style="COLOR: blue"&gt;internal double &lt;/SPAN&gt;Price;

    &lt;SPAN style="COLOR: blue"&gt;internal static void &lt;/SPAN&gt;Banner() {
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,-6}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Ticker"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,-50}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Name"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,-12}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Asset Class"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: green"&gt;//Console.Write("{0,-30}\t", "Asset SubClass";
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"RS"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"1Wk"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"4Wk"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"3Ms"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"6Ms"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"1Yr"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,6}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Vol"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"{0,2}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Mv"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: green"&gt;//Console.Write("{0,6}", "Pr");
        //Console.WriteLine("{0,6}", "M200");
    &lt;/SPAN&gt;}

    &lt;SPAN style="COLOR: blue"&gt;internal void &lt;/SPAN&gt;Print() {

        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,-6}"&lt;/SPAN&gt;, Ticker);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,-50}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;(Name.Take(&lt;SPAN style="COLOR: brown"&gt;48&lt;/SPAN&gt;).ToArray()));
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,-12}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;(AssetClass.Take(&lt;SPAN style="COLOR: brown"&gt;10&lt;/SPAN&gt;).ToArray()));
        &lt;SPAN style="COLOR: green"&gt;//Console.Write("{0,-30}\t", new String(AssetSubClass.Take(28).ToArray()));
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4:N0}"&lt;/SPAN&gt;, LRS * &lt;SPAN style="COLOR: brown"&gt;100&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4:N0}"&lt;/SPAN&gt;, Weekly * &lt;SPAN style="COLOR: brown"&gt;100&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4:N0}"&lt;/SPAN&gt;, FourWeeks * &lt;SPAN style="COLOR: brown"&gt;100&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4:N0}"&lt;/SPAN&gt;, ThreeMonths * &lt;SPAN style="COLOR: brown"&gt;100&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4:N0}"&lt;/SPAN&gt;, SixMonths * &lt;SPAN style="COLOR: brown"&gt;100&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,4:N0}"&lt;/SPAN&gt;, OneYear * &lt;SPAN style="COLOR: brown"&gt;100&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0,6:N0}"&lt;/SPAN&gt;, StdDev * &lt;SPAN style="COLOR: brown"&gt;100&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(Price &amp;lt;= Mav200)
            &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"{0,2}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"X"&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: blue"&gt;else
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine();
        &lt;SPAN style="COLOR: green"&gt;//Console.Write("{0,6:N2}", Price);
        //Console.WriteLine("{0,6:N2}", Mav200);
    &lt;/SPAN&gt;}
}&lt;/PRE&gt;
&lt;P&gt;The class Summary above is how I want to present my results. A few comments on the code. I use &lt;EM&gt;Nullable&amp;lt;T&amp;gt;&lt;/EM&gt; because some of this values can be null (i.e. not enough history), but I still don’t want to worry about it. It ends up working rather neatly.&lt;/P&gt;
&lt;P&gt;I also print the results out to &lt;EM&gt;Console, &lt;/EM&gt;which is crazy. I really should be using WPF/Silverlight as the presentation layer. Also the &lt;EM&gt;{0,4:N0}&lt;/EM&gt; notation might be unfamiliar to some of you, but this is how mad &lt;EM&gt;Console&lt;/EM&gt; guys like myself avoid using real UI frameworks. Sometimes we print things in color too.&lt;/P&gt;
&lt;P&gt;The real meat is in the following line:&lt;/P&gt;&lt;PRE class=code&gt;LRS = (fourWeeks + threeMonths + sixMonths + oneYear) / &lt;SPAN style="COLOR: brown"&gt;4&lt;/SPAN&gt;;&lt;/PRE&gt;
&lt;P&gt;That is our highway to richness. It’s a very elaborated quant formula, never before shown, that calculate a magick relative strength (aka momentum) factor as the average of the performance of four weeks, three months, six months and one year.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;TimeSeries &lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;internal readonly string &lt;/SPAN&gt;Ticker;
    &lt;SPAN style="COLOR: blue"&gt;readonly &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DateTime &lt;/SPAN&gt;_start;
    &lt;SPAN style="COLOR: blue"&gt;readonly &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Dictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt;&amp;gt; _adjDictionary;
    &lt;SPAN style="COLOR: blue"&gt;readonly string &lt;/SPAN&gt;_name;
    &lt;SPAN style="COLOR: blue"&gt;readonly string &lt;/SPAN&gt;_assetClass;
    &lt;SPAN style="COLOR: blue"&gt;readonly string &lt;/SPAN&gt;_assetSubClass;

    &lt;SPAN style="COLOR: blue"&gt;internal &lt;/SPAN&gt;TimeSeries(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;ticker, &lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;name, &lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;assetClass, &lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;assetSubClass, &lt;BR&gt;                                                                &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Event&lt;/SPAN&gt;&amp;gt; events) {
        Ticker = ticker;
        _name = name;
        _assetClass = assetClass;
        _assetSubClass = assetSubClass;
        _start = events.Last().Date;
        _adjDictionary = events.ToDictionary(e =&amp;gt; e.Date, e =&amp;gt; e.Price);
    }&lt;/PRE&gt;
&lt;P&gt;I then built myself a little TimeSeries class that represents a series of (date, price). I choose a dictionary to store it because of my assumption that I will be accessing it by date a lot. In retrospect, I was kind of right and kind of wrong. It doesn’t really matter much.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;bool &lt;/SPAN&gt;GetPrice(&lt;SPAN style="COLOR: #2b91af"&gt;DateTime &lt;/SPAN&gt;when, &lt;SPAN style="COLOR: blue"&gt;out double &lt;/SPAN&gt;price, &lt;SPAN style="COLOR: blue"&gt;out double &lt;/SPAN&gt;shift) {
    &lt;SPAN style="COLOR: green"&gt;// To nullify the effect of hours/min/sec/millisec being different from 0
    &lt;/SPAN&gt;when = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;(when.Year, when.Month, when.Day);
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;found = &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;;
    shift = &lt;SPAN style="COLOR: brown"&gt;1&lt;/SPAN&gt;;
    &lt;SPAN style="COLOR: blue"&gt;double &lt;/SPAN&gt;aPrice = &lt;SPAN style="COLOR: brown"&gt;0&lt;/SPAN&gt;;
    &lt;SPAN style="COLOR: blue"&gt;while &lt;/SPAN&gt;(when &amp;gt;= _start &amp;amp;&amp;amp; !found) {
        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(_adjDictionary.TryGetValue(when, &lt;SPAN style="COLOR: blue"&gt;out &lt;/SPAN&gt;aPrice)) {
            found = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;
        }
        when = when.AddDays(-&lt;SPAN style="COLOR: brown"&gt;1&lt;/SPAN&gt;);
        shift -= &lt;SPAN style="COLOR: brown"&gt;1&lt;/SPAN&gt;;
    }
    price = aPrice;
    &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;found;
}&lt;/PRE&gt;
&lt;P&gt;A TimeSeries can give you back the price at a particular date. This looks bizarre and complex, but there is a reason for it. I might ask for a date that doesn’t have a price associated with it (i.e. holidays, week-ends). In such cases I want to return the previous price which could be N days in the past.&lt;/P&gt;
&lt;P&gt;I also want to return how many days in the past I had to go, so that other calculations (i.e. &lt;EM&gt;Return&lt;/EM&gt;) can modify their end date by the same amount. Also I might not find such a price at all, in which case I don’t want to throw an exception, but instead notify the caller. In retrospect, I should have used &lt;EM&gt;double?&lt;/EM&gt; to signify ‘price not found’.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt;? GetReturn(&lt;SPAN style="COLOR: #2b91af"&gt;DateTime &lt;/SPAN&gt;start, &lt;SPAN style="COLOR: #2b91af"&gt;DateTime &lt;/SPAN&gt;end) {
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;startPrice = &lt;SPAN style="COLOR: brown"&gt;0.0&lt;/SPAN&gt;;
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;endPrice = &lt;SPAN style="COLOR: brown"&gt;0.0&lt;/SPAN&gt;;
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;shift = &lt;SPAN style="COLOR: brown"&gt;0.0&lt;/SPAN&gt;;
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;foundEnd = GetPrice(end, &lt;SPAN style="COLOR: blue"&gt;out &lt;/SPAN&gt;endPrice, &lt;SPAN style="COLOR: blue"&gt;out &lt;/SPAN&gt;shift);
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;foundStart = GetPrice(start.AddDays(shift), &lt;SPAN style="COLOR: blue"&gt;out &lt;/SPAN&gt;startPrice, &lt;SPAN style="COLOR: blue"&gt;out &lt;/SPAN&gt;shift);
    &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(!foundStart || !foundEnd)
        &lt;SPAN style="COLOR: blue"&gt;return null&lt;/SPAN&gt;;
    &lt;SPAN style="COLOR: blue"&gt;else
        return &lt;/SPAN&gt;endPrice / startPrice - &lt;SPAN style="COLOR: brown"&gt;1&lt;/SPAN&gt;;
}&lt;/PRE&gt;
&lt;P&gt;We can now go and calculate the return between two dates. Also the &lt;EM&gt;TimeSeries&lt;/EM&gt; object needs to perform a little more calculations.&lt;/P&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: blue"&gt;internal double&lt;/SPAN&gt;? LastWeekReturn() {
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;GetReturn(&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now.AddDays(-&lt;SPAN style="COLOR: brown"&gt;7&lt;/SPAN&gt;), &lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now);
    }
    &lt;SPAN style="COLOR: blue"&gt;internal double&lt;/SPAN&gt;? Last4WeeksReturn() {
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;GetReturn(&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now.AddDays(-&lt;SPAN style="COLOR: brown"&gt;28&lt;/SPAN&gt;), &lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now);
    }
    &lt;SPAN style="COLOR: blue"&gt;internal double&lt;/SPAN&gt;? Last3MonthsReturn() {
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;GetReturn(&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now.AddMonths(-&lt;SPAN style="COLOR: brown"&gt;3&lt;/SPAN&gt;), &lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now);
    }
    &lt;SPAN style="COLOR: blue"&gt;internal double&lt;/SPAN&gt;? Last6MonthsReturn() {
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;GetReturn(&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now.AddMonths(-&lt;SPAN style="COLOR: brown"&gt;6&lt;/SPAN&gt;), &lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now);
    }
    &lt;SPAN style="COLOR: blue"&gt;internal double&lt;/SPAN&gt;? LastYearReturn() {
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;GetReturn(&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now.AddYears(-&lt;SPAN style="COLOR: brown"&gt;1&lt;/SPAN&gt;), &lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now);
    }
    &lt;SPAN style="COLOR: blue"&gt;internal double&lt;/SPAN&gt;? StdDev() {
        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;now = &lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now;
        now = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;(now.Year, now.Month, now.Day);
        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;limit = now.AddYears(-&lt;SPAN style="COLOR: brown"&gt;3&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;rets = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt;&amp;gt;();
        &lt;SPAN style="COLOR: blue"&gt;while &lt;/SPAN&gt;(now &amp;gt;= _start.AddDays(&lt;SPAN style="COLOR: brown"&gt;12&lt;/SPAN&gt;) &amp;amp;&amp;amp; now &amp;gt;= limit) {
            &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;ret = GetReturn(now.AddDays(-&lt;SPAN style="COLOR: brown"&gt;7&lt;/SPAN&gt;), now);
            rets.Add(ret.Value);
            now = now.AddDays(-&lt;SPAN style="COLOR: brown"&gt;7&lt;/SPAN&gt;);
        }
        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;mean = rets.Average();
        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;variance = rets.Select(r =&amp;gt; &lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.Pow(r - mean, &lt;SPAN style="COLOR: brown"&gt;2&lt;/SPAN&gt;)).Sum();
        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;weeklyStdDev = &lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.Sqrt(variance / rets.Count);
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;weeklyStdDev * &lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.Sqrt(&lt;SPAN style="COLOR: brown"&gt;40&lt;/SPAN&gt;);
    }
    &lt;SPAN style="COLOR: blue"&gt;internal double&lt;/SPAN&gt;? MAV200() {
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;_adjDictionary&lt;BR&gt;               .ToList()&lt;BR&gt;               .OrderByDescending(k =&amp;gt; k.Key)&lt;BR&gt;               .Take(&lt;SPAN style="COLOR: brown"&gt;200)&lt;BR&gt;               &lt;/SPAN&gt;.Average(k =&amp;gt; k.Value);
    }
    &lt;SPAN style="COLOR: blue"&gt;internal double &lt;/SPAN&gt;TodayPrice() {
        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;price = &lt;SPAN style="COLOR: brown"&gt;0.0&lt;/SPAN&gt;;
        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;shift = &lt;SPAN style="COLOR: brown"&gt;0.0&lt;/SPAN&gt;;
        GetPrice(&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now, &lt;SPAN style="COLOR: blue"&gt;out &lt;/SPAN&gt;price, &lt;SPAN style="COLOR: blue"&gt;out &lt;/SPAN&gt;shift);
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;price;
    }
    &lt;SPAN style="COLOR: blue"&gt;internal &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Summary &lt;/SPAN&gt;GetSummary() {
        &lt;SPAN style="COLOR: blue"&gt;return new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Summary&lt;/SPAN&gt;(Ticker, _name, _assetClass, _assetSubClass, &lt;BR&gt;                           LastWeekReturn(), Last4WeeksReturn(), Last3MonthsReturn(),&lt;BR&gt;                           Last6MonthsReturn(), LastYearReturn(), StdDev(), TodayPrice(),&lt;BR&gt;                           MAV200());
    }
}&lt;/PRE&gt;
&lt;P&gt;Nothing particularly interesting in this code. Just a bunch of calculations. The &lt;EM&gt;MAV200&lt;/EM&gt; is the 200 days moving average of closing prices. It shows a more functional way of doing things. The &lt;EM&gt;StdDev&lt;/EM&gt; function is instead very imperative.&lt;/P&gt;
&lt;P&gt;We now can work on downloading the prices. This is how you construct the right URL:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;static string &lt;/SPAN&gt;CreateUrl(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;ticker, &lt;SPAN style="COLOR: #2b91af"&gt;DateTime &lt;/SPAN&gt;start, &lt;SPAN style="COLOR: #2b91af"&gt;DateTime &lt;/SPAN&gt;end) {
    &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;@"http://ichart.finance.yahoo.com/table.csv?s=" &lt;/SPAN&gt;+ ticker + &lt;SPAN style="COLOR: #a31515"&gt;"&amp;amp;a="&lt;BR&gt;            &lt;/SPAN&gt;+ (start.Month - &lt;SPAN style="COLOR: brown"&gt;1&lt;/SPAN&gt;).ToString() + &lt;SPAN style="COLOR: #a31515"&gt;"&amp;amp;b=" &lt;/SPAN&gt;+ start.Day.ToString() + &lt;SPAN style="COLOR: #a31515"&gt;"&amp;amp;c="&lt;BR&gt;            &lt;/SPAN&gt;+ start.Year.ToString() + &lt;SPAN style="COLOR: #a31515"&gt;"&amp;amp;d=" &lt;/SPAN&gt;+ (end.Month - &lt;SPAN style="COLOR: brown"&gt;1&lt;/SPAN&gt;).ToString() + &lt;SPAN style="COLOR: #a31515"&gt;"&amp;amp;e="&lt;BR&gt;            &lt;/SPAN&gt;+ end.Day.ToString() + &lt;SPAN style="COLOR: #a31515"&gt;"&amp;amp;f=" &lt;/SPAN&gt;+ end.Year.ToString() + &lt;SPAN style="COLOR: #a31515"&gt;"&amp;amp;g=d&amp;amp;ignore=.csv"&lt;/SPAN&gt;;
}&lt;/PRE&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And let’s set how many concurrent connections we are going to use …&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ServicePointManager&lt;/SPAN&gt;.DefaultConnectionLimit = &lt;SPAN style="COLOR: brown"&gt;10&lt;/SPAN&gt;;&lt;/PRE&gt;
&lt;P&gt;On my machine, setting this number too high causes errors to be returned. I’m not sure on which side of the connection the problem lies.&lt;/P&gt;
&lt;P&gt;We can then load all the tickers we want to load from a file. One of the files has Leveraged ETFs, which I want to filter out because they tend to pop up always at the top.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;tickers =
    &lt;SPAN style="COLOR: green"&gt;//File.ReadAllLines("ETFs.csv")
    //File.ReadAllLines("ETFTest.csv")
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;File&lt;/SPAN&gt;.ReadAllLines(&lt;SPAN style="COLOR: #a31515"&gt;"AssetClasses.csv"&lt;/SPAN&gt;)
    .Skip(&lt;SPAN style="COLOR: brown"&gt;1&lt;/SPAN&gt;)
    .Select(l =&amp;gt; l.Split(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;[] { &lt;SPAN style="COLOR: #a31515"&gt;',' &lt;/SPAN&gt;}))
    .Where(v =&amp;gt; v[&lt;SPAN style="COLOR: brown"&gt;2&lt;/SPAN&gt;] != &lt;SPAN style="COLOR: #a31515"&gt;"Leveraged"&lt;/SPAN&gt;)
    .Select(values =&amp;gt; &lt;SPAN style="COLOR: #2b91af"&gt;Tuple&lt;/SPAN&gt;.Create(values[&lt;SPAN style="COLOR: brown"&gt;0&lt;/SPAN&gt;], values[&lt;SPAN style="COLOR: brown"&gt;1&lt;/SPAN&gt;], values[&lt;SPAN style="COLOR: brown"&gt;2&lt;/SPAN&gt;], values[&lt;SPAN style="COLOR: brown"&gt;3&lt;/SPAN&gt;]))
    .ToArray();
&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;len = tickers.Length;

&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;start = &lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now.AddYears(-&lt;SPAN style="COLOR: brown"&gt;2&lt;/SPAN&gt;);
&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;end = &lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now;
&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;cevent = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CountdownEvent&lt;/SPAN&gt;(len);
&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;summaries = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Summary&lt;/SPAN&gt;[len];&lt;/PRE&gt;
&lt;P&gt;And then load all of them, making sure to make an asynchronous call so not to keep the thread busy.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;for(var i = &lt;/SPAN&gt;&lt;SPAN style="COLOR: brown"&gt;0;&lt;/SPAN&gt; i &amp;lt; len; i++)  {
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;t = tickers[i];
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;url = CreateUrl(t.Item1, start, end);
    &lt;SPAN style="COLOR: blue"&gt;using &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;webClient = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;WebClient&lt;/SPAN&gt;()) {
        webClient.DownloadStringCompleted +=&lt;BR&gt;                          &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DownloadStringCompletedEventHandler&lt;/SPAN&gt;(downloadStringCompleted);
        webClient.DownloadStringAsync(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt;(url), &lt;SPAN style="COLOR: #2b91af"&gt;Tuple&lt;/SPAN&gt;.Create(t, cevent, summaries, i));
    }
}

cevent.Wait();&lt;/PRE&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice the use of a Countdown event to wait for all the thread to complete before printing out the results. Also notice the new &lt;EM&gt;Tuple&amp;lt;T&amp;gt;&lt;/EM&gt; class used to package things to send around.&lt;/P&gt;
&lt;P&gt;We can then print out the top and bottom 15%:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;top15perc =
        summaries
        .Where(s =&amp;gt; s.LRS.HasValue)
        .OrderByDescending(s =&amp;gt; s.LRS)
        .Take((&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;)(len * &lt;SPAN style="COLOR: brown"&gt;0.15&lt;/SPAN&gt;));
&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;bottom15perc =
        summaries
        .Where(s =&amp;gt; s.LRS.HasValue)
        .OrderBy(s =&amp;gt; s.LRS)
        .Take((&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;)(len * &lt;SPAN style="COLOR: brown"&gt;0.15&lt;/SPAN&gt;));

&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine();
&lt;SPAN style="COLOR: #2b91af"&gt;Summary&lt;/SPAN&gt;.Banner();
&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"TOP 15%"&lt;/SPAN&gt;);
&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;s &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;top15perc)
    s.Print();

&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine();
&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Bottom 15%"&lt;/SPAN&gt;);
&lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;s &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;bottom15perc)
    s.Print();&lt;/PRE&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is what we do when a request comes back with data:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;static void &lt;/SPAN&gt;downloadStringCompleted(&lt;SPAN style="COLOR: blue"&gt;object &lt;/SPAN&gt;sender, &lt;SPAN style="COLOR: #2b91af"&gt;DownloadStringCompletedEventArgs &lt;/SPAN&gt;e) {
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;bigTuple =&lt;BR&gt;             (&lt;SPAN style="COLOR: #2b91af"&gt;Tuple&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Tuple&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;, &lt;SPAN style="COLOR: #2b91af"&gt;CountdownEvent&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;Summary&lt;/SPAN&gt;[], &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;)&lt;BR&gt;              e.UserState;
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;tuple = bigTuple.Item1;
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;cevent = bigTuple.Item2;
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;summaries = bigTuple.Item3;
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;i = bigTuple.Item4;
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;ticker = tuple.Item1;
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;name = tuple.Item2;
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;asset = tuple.Item3;
    &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;subAsset = tuple.Item4;

    &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(e.Error == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) {
        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;adjustedPrices =
                e.Result
                .Split(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;[] { &lt;SPAN style="COLOR: #a31515"&gt;'\n' &lt;/SPAN&gt;})
                .Skip(&lt;SPAN style="COLOR: brown"&gt;1&lt;/SPAN&gt;)
                .Select(l =&amp;gt; l.Split(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;[] { &lt;SPAN style="COLOR: #a31515"&gt;',' &lt;/SPAN&gt;}))
                .Where(l =&amp;gt; l.Length == &lt;SPAN style="COLOR: brown"&gt;7&lt;/SPAN&gt;)
                .Select(v =&amp;gt; &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Event&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Parse(v[&lt;SPAN style="COLOR: brown"&gt;0&lt;/SPAN&gt;]), &lt;SPAN style="COLOR: #2b91af"&gt;Double&lt;/SPAN&gt;.Parse(v[&lt;SPAN style="COLOR: brown"&gt;6&lt;/SPAN&gt;])));

        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;timeSeries = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;TimeSeries&lt;/SPAN&gt;(ticker, name, asset, subAsset, adjustedPrices);
        summaries[i] = timeSeries.GetSummary();
        cevent.Signal();
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Write(&lt;SPAN style="COLOR: #a31515"&gt;"{0} "&lt;/SPAN&gt;, ticker);
    }
    &lt;SPAN style="COLOR: blue"&gt;else &lt;/SPAN&gt;{
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"[{0} ERROR] "&lt;/SPAN&gt;, ticker);
        &lt;SPAN style="COLOR: green"&gt;//Console.WriteLine(e.Error);
        &lt;/SPAN&gt;summaries[i] = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Summary&lt;/SPAN&gt;(ticker, name, &lt;SPAN style="COLOR: #a31515"&gt;"ERROR"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"ERROR"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: brown"&gt;0&lt;/SPAN&gt;, &lt;SPAN style="COLOR: brown"&gt;0&lt;/SPAN&gt;, &lt;SPAN style="COLOR: brown"&gt;0&lt;/SPAN&gt;, &lt;SPAN style="COLOR: brown"&gt;0&lt;/SPAN&gt;, &lt;SPAN style="COLOR: brown"&gt;0&lt;/SPAN&gt;, &lt;SPAN style="COLOR: brown"&gt;0&lt;/SPAN&gt;,&lt;SPAN style="COLOR: brown"&gt;0&lt;/SPAN&gt;,&lt;SPAN style="COLOR: brown"&gt;0&lt;/SPAN&gt;); 
        cevent.Signal();
    }
}&lt;/PRE&gt;
&lt;P&gt;We first unpack the &lt;EM&gt;Tuple&lt;/EM&gt; we sent out originally, we then extract the Date and Price, create a &lt;EM&gt;Summary &lt;/EM&gt;object and store it in the &lt;EM&gt;summaries&lt;/EM&gt; array. It’s important to remember to &lt;EM&gt;Signal&lt;/EM&gt; to the &lt;EM&gt;cevent&lt;/EM&gt; in the error case as well because we want to print out the results even if some downloading failed.&lt;/P&gt;
&lt;P&gt;And here is what you get for your effort:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/BecomingreallyrichwithC_C128/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/BecomingreallyrichwithC_C128/image_2.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/BecomingreallyrichwithC_C128/image_thumb.png" width=743 height=506 mce_src="http://blogs.msdn.com/blogfiles/lucabol/WindowsLiveWriter/BecomingreallyrichwithC_C128/image_thumb.png"&gt;&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9896982" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-09-89-69-82/SystemCodeAndData.zip" length="15441" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/b/lucabol/archive/tags/C_2300_+Programming/">C# Programming</category></item></channel></rss>