<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Matthew Podwysocki's Blog</title><link>http://weblogs.asp.net/podwysocki/default.aspx</link><description>Architect, Develop, Inspire...</description><language>en</language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><geo:lat>38.977109</geo:lat><geo:long>-77.385278</geo:long><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/MatthewPodwysockisBlog" type="application/rss+xml" /><item><title>DC ALT.NET 11/25 - Web Testing Frameworks</title><link>http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~3/456803381/dc-alt-net-11-25-web-testing-frameworks.aspx</link><category>ALT.NET</category><category>User Groups</category><category>ASP.NET</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Mon, 17 Nov 2008 23:22:00 -0600</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6744910</guid><description>&lt;p&gt;The November meeting for DC ALT.NET will be on November 25th, 2008 from 7PM-9PM.&amp;nbsp; Check out &lt;a href="http://altnet.ent0.com/"&gt;our site&lt;/a&gt; and &lt;a href="http://tech.groups.yahoo.com/group/dcaltnet/"&gt;our mailing list&lt;/a&gt; for more information as it becomes available.&amp;nbsp; This month, John Morales will be facilitating a discussion on web testing frameworks which includes Selenium, Watir and WatiN among others.&amp;nbsp; Once again, I'd like to thank &lt;a href="http://cynergysystems.com/"&gt;Cynergy Systems&lt;/a&gt;, Inc for sponsoring this month's event. &lt;/p&gt;

&lt;p&gt;The information is as follows: &lt;/p&gt;

&lt;p&gt;DateTime: 
  &lt;br&gt;11/25/2008 - 7PM-9PM &lt;/p&gt;

&lt;p&gt;Location: 
  &lt;br&gt;Cynergy Systems Inc. 
  &lt;br&gt;1600 K St NW 
  &lt;br&gt;Suite 300 
  &lt;br&gt;Washington, DC 20006 
  &lt;br&gt;&lt;a href="http://maps.google.com/maps?q=1600+K+Street+NW,+Washington,+DC++20006&amp;amp;iwloc=A&amp;amp;hl=en"&gt;Show Map&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.eventbrite.com/event/210949957"&gt;&lt;img src="http://www.eventbrite.com/static/images/button_ext/rsvp.gif" border="0"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;br&gt;&lt;br&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://weblogs.asp.net/podwysocki/archive/2008/11/18/dc-alt-net-11-25-web-testing-frameworks.aspx" mce_href="http://www.dotnetkicks.com/kick/?url=http://weblogs.asp.net/podwysocki/archive/2008/11/18/dc-alt-net-11-25-web-testing-frameworks.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://weblogs.asp.net/podwysocki/archive/2008/11/18/dc-alt-net-11-25-web-testing-frameworks.aspx" alt="kick it on DotNetKicks.com" mce_src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://weblogs.asp.net/podwysocki/archive/2008/11/18/dc-alt-net-11-25-web-testing-frameworks.aspx" border="0"&gt;&lt;/a&gt;
&lt;script src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/18/dc-alt-net-11-25-web-testing-frameworks.aspx" type="text/javascript" charset="utf-8" mce_src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/18/dc-alt-net-11-25-web-testing-frameworks.aspx"&gt;&lt;/script&gt;
&lt;script src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/18/dc-alt-net-11-25-web-testing-frameworks.aspx" type="text/javascript" charset="utf-8" mce_src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/18/dc-alt-net-11-25-web-testing-frameworks.aspx"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_title = 'Functional .NET 4.0 - Tuples and Zip;&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_blurb = 'Functional .NET 4.0 - Tuples and Zip';&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_url = 'http://weblogs.asp.net/podwysocki/archive/2008/11/18/dc-alt-net-11-25-web-testing-frameworks.aspx';&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_style = '1';&lt;/script&gt;
&lt;script src="http://widgets.dzone.com/widgets/zoneit.js" language="javascript"&gt;&lt;/script&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6744910" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=9WTzN"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=9WTzN" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=rdWDn"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=rdWDn" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=UtLnN"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=UtLnN" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=pKrJn"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=pKrJn" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/456803381" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=6744910</wfw:commentRss><feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=MatthewPodwysockisBlog&amp;itemurl=http%3A%2F%2Fweblogs.asp.net%2Fpodwysocki%2Farchive%2F2008%2F11%2F18%2Fdc-alt-net-11-25-web-testing-frameworks.aspx</feedburner:awareness><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2008/11/18/dc-alt-net-11-25-web-testing-frameworks.aspx</feedburner:origLink></item><item><title>Functional .NET 4.0 - Tuples and Zip</title><link>http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~3/454623098/functional-net-4-0-tuples-and-zip.aspx</link><category>C#</category><category>F#</category><category>Functional Programming</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Sun, 16 Nov 2008 00:05:00 -0600</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6740734</guid><description>&lt;p&gt;Previously, when covering some of the additions to the .NET 4.0 Framework such as &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/10/29/c-4-0-named-and-optional-parameters-behind-the-scenes.aspx"&gt;optional and named parameters&lt;/a&gt;, some of the other additions have caught my eye from the perspective of functional programming.&amp;nbsp; Unlike .NET 3.5, this release is not as targeted towards functional programming as it is more towards dynamic programming and COM interoperability.&amp;nbsp; But, there are a few items to note that we can soon take advantage of, including the Tuple type and the Zip operator function among other items.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Looking at Tuples&lt;/h2&gt;

&lt;p&gt;To define a tuple, it comes from the mathematics field, and is simply an ordered list of values, which are components of that tuple.&amp;nbsp; These components can be of any type, whether it be string, integer, or otherwise.&amp;nbsp; In order to refer to these components, we retrieve references to them by absolute position in that sequence.&lt;/p&gt;

&lt;p&gt;In F#, the tuple is a fully supported data type and perhaps one of the most useful.&amp;nbsp; To define a tuple is to define a number of expressions grouped together with comma separation to form a new expression such as the following:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;#light&lt;/span&gt;&amp;nbsp; &lt;br&gt;
  &lt;br&gt;&lt;span style="color: green;"&gt;// val blogger : string * string&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; blogger1 = &lt;span style="color: maroon;"&gt;"Matthew"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"Podwysocki"&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; blogger2 = &lt;span style="color: maroon;"&gt;"Jeremy"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"Miller"&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; blogger3 = &lt;span style="color: maroon;"&gt;"David"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"Laribee"&lt;/span&gt; 
  &lt;br&gt;
  &lt;br&gt;&lt;span style="color: green;"&gt;// val bloggers : &lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: green;"&gt;// (string * string) * (string * string) * (string * string) &lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; bloggers = blogger1, blogger2, blogger3&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;From there, tuples can be decomposed into their components in either of two ways.&amp;nbsp; For pair tuples, a tuple with exactly two components, can be deconstructed using the fst and snd functions such as the following:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;#light&lt;/span&gt; 
  &lt;br&gt;
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; pageHitCount = &lt;span style="color: maroon;"&gt;"http://www.codebetter.com/"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;25500&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; page = fst pageHitCount 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; hitCount = snd pageHitCount&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;However, it is more common to use a pattern expression to retrieve values from a tuple, such as the following code:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;#light&lt;/span&gt; 
  &lt;br&gt;
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; request =&amp;nbsp; &lt;br&gt;&amp;nbsp; &lt;span style="color: maroon;"&gt;"http://codebetter"&lt;/span&gt;,&amp;nbsp; &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;new&lt;/span&gt; DateTime&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: maroon;"&gt;2008&lt;/span&gt;, &lt;span style="color: maroon;"&gt;11&lt;/span&gt;, &lt;span style="color: maroon;"&gt;15&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;, 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: maroon;"&gt;"Firefox/3.0.4 (.NET CLR 3.5.30729)"&lt;/span&gt; &lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; host, date, userAgent = request&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;What we're able to do is break down the given request into three pieces, the host, date and user-agent.&amp;nbsp; This makes pattern matching against tuples really powerful such as the following:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;#light&lt;/span&gt; 
  &lt;br&gt;
  &lt;br&gt;&lt;span style="color: green;"&gt;// val permit_request : string * string * int -&amp;gt; bool&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; permit_request = &lt;span style="color: blue;"&gt;function&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;|&lt;/span&gt;&amp;nbsp;&lt;span style="color: maroon;"&gt;"http"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"google.com"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;80&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;true&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;|&lt;/span&gt; _, &lt;span style="color: maroon;"&gt;"microsoft.com"&lt;/span&gt;, _ &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;true&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;|&lt;/span&gt;&amp;nbsp;&lt;span style="color: maroon;"&gt;"ftp"&lt;/span&gt;, _, &lt;span style="color: maroon;"&gt;21&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;true&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;|&lt;/span&gt; _ &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;Just as well, we could even use them in active patterns so that we could pattern match against parts of a FileVersionInfo in order to determine which action to take such as the following.&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;#light&lt;/span&gt; 
  &lt;br&gt;
  &lt;br&gt;&lt;span style="color: blue;"&gt;open&lt;/span&gt; System&lt;span style="color: blue;"&gt;.&lt;/span&gt;Diagnostics 
  &lt;br&gt;
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;|&lt;/span&gt;FileVersionSections&lt;span style="color: blue;"&gt;|&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;(&lt;/span&gt;f&lt;span style="color: blue;"&gt;:&lt;/span&gt;FileVersionInfo&lt;span style="color: blue;"&gt;)&lt;/span&gt; =&amp;nbsp; &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;(&lt;/span&gt;f&lt;span style="color: blue;"&gt;.&lt;/span&gt;FileName,f&lt;span style="color: blue;"&gt;.&lt;/span&gt;ProductName,f&lt;span style="color: blue;"&gt;.&lt;/span&gt;ProductVersion&lt;span style="color: blue;"&gt;)&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; parse_files = &lt;span style="color: blue;"&gt;function&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;|&lt;/span&gt; FileVersionSections&lt;span style="color: blue;"&gt;(&lt;/span&gt;fn, &lt;span style="color: maroon;"&gt;"Parallel Extensions for the .NET Framework"&lt;/span&gt;, _&lt;span style="color: blue;"&gt;)&lt;/span&gt;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; printfn &lt;span style="color: maroon;"&gt;"Parallel Extensions file %s"&lt;/span&gt; fn 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;|&lt;/span&gt; FileVersionSections&lt;span style="color: blue;"&gt;(&lt;/span&gt;fn, &lt;span style="color: maroon;"&gt;"Microsoft Office Communicator 2007"&lt;/span&gt;, _&lt;span style="color: blue;"&gt;)&lt;/span&gt;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; printfn &lt;span style="color: maroon;"&gt;"Communicator file %s"&lt;/span&gt; fn 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;|&lt;/span&gt; _ &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; printfn &lt;span style="color: maroon;"&gt;"Unknown file"&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&lt;span style="color: maroon;"&gt;&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;You're saying, great, but what has this to do with .NET 4.0?&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Tuples in .NET 4.0&lt;/h2&gt;

&lt;p&gt;Earlier this month, &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx"&gt;Justin Van Patten, on the BCL Team Blog&lt;/a&gt; announced some the base class library changes coming to .NET 4.0.&amp;nbsp; Among them was Tuples in which was stated:&lt;/p&gt;

&lt;blockquote&gt;
  
&lt;p&gt;We are providing common tuple types in the BCL to facilitate language interoperability and to reduce duplication in the framework.&amp;nbsp; A tuple is a simple generic data structure that holds an ordered set of items of heterogeneous types.&amp;nbsp; Tuples are supported natively in languages such as F# and IronPython, but are also easy to use from any .NET language such as C# and VB.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What does that mean exactly?&amp;nbsp; Does this mean we'll get some form of syntactic sugar around them as well?&amp;nbsp; If we could decompose them in such a fashion as we do in F#, and to initialize them in an easy fashion without a lot of pomp and circumstance.&amp;nbsp; Gazing from the intent in the .NET libraries, something like this might be an option:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt; t1 = Tuple.Create(&lt;span style="color: maroon;"&gt;1&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'a'&lt;/span&gt;);&amp;nbsp; &lt;br&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt; i1 = t1.Item1;&amp;nbsp; &lt;br&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt; i2 = t1.Item2;&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;But, unless there were a nicer way to tease apart the data, it just becomes a simple holder of data, instead of something that we could decompose easily into patterns.&amp;nbsp; I would love to some sort of syntactic sugar in C# to allow for this to happen.&amp;nbsp; I am, however, encouraged that they are working with the F# team and other teams to ensure compatibility between the libraries.&lt;/p&gt;

&lt;p&gt;If you open Reflector, you will find the type definitions in mscorlib.dll version 4.0 under the System namespace.&amp;nbsp; You may also be surprised to find them as internal classes only at this point, which is unfortunate and we find ourselves not able to take advantage of these things such as the CodeContracts and BigInteger/BigNumber in the .NET 3.5 libraries.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/controlpanel/blogs/$net40_tuple%5B3%5D.png"&gt;&lt;img src="http://weblogs.asp.net/controlpanel/blogs/$net40_tuple_thumb%5B1%5D.png" style="border: 0px none ;" alt="net40_tuple" border="0" height="262" width="508"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you can see from the above screen catpure, we have up to 7 arguments for a given tuple and the rest as defined by another tuple.&amp;nbsp; I, however, haven't seen tuples quite that large before, but it's always possible...&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;The Zip Operator Function&lt;/h2&gt;

&lt;p&gt;Another functional programming item that has been added to the System.Linq.Enumerable class in System.Core.dll.&amp;nbsp; This method allows us to combine two collections together using a function to calculate the new element given the element from each collection.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;In F#, we have two ways of doing this in F# in the Seq module.&amp;nbsp; They are defined as:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;val&lt;/span&gt; map2 &lt;span style="color: blue;"&gt;:&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;(&lt;/span&gt;'a &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; 'b &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; 'c&lt;span style="color: blue;"&gt;)&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; seq&amp;lt;'a&amp;gt; &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; seq&amp;lt;'b&amp;gt; &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; seq&amp;lt;'c&amp;gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;val&lt;/span&gt; zip &lt;span style="color: blue;"&gt;:&lt;/span&gt; seq&amp;lt;'a&amp;gt; &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; seq&amp;lt;'b&amp;gt; &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; seq&amp;lt;'a * 'b&amp;gt;&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;The map2 function takes a function which takes the two items from the list to produce the calculated item, and two collections and returns a new collection of the computed items.&amp;nbsp; If one collection is shorter than the other, the rest of the computations are not completed.&amp;nbsp; The zip function is a simple function which takes the items from the first and second collection and combines them in a tuple. &lt;/p&gt;

&lt;p&gt;An example of each follows:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: green;"&gt;// Map2&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; l1 = seq&lt;span style="color: blue;"&gt;[&lt;/span&gt;&lt;span style="color: maroon;"&gt;1&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;.&lt;/span&gt;&lt;span style="color: blue;"&gt;.&lt;/span&gt;&amp;nbsp;&lt;span style="color: maroon;"&gt;26&lt;/span&gt;&lt;span style="color: blue;"&gt;]&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; l2 = seq&lt;span style="color: blue;"&gt;[&lt;/span&gt;&lt;span style="color: maroon;"&gt;'a'&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;.&lt;/span&gt;&lt;span style="color: blue;"&gt;.&lt;/span&gt;&amp;nbsp;&lt;span style="color: maroon;"&gt;'z'&lt;/span&gt;&lt;span style="color: blue;"&gt;]&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; mapped = Seq&lt;span style="color: blue;"&gt;.&lt;/span&gt;map2&amp;nbsp; &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;fun&lt;/span&gt; a b &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; sprintf &lt;span style="color: maroon;"&gt;"%d%c"&lt;/span&gt; a b&lt;span style="color: blue;"&gt;)&lt;/span&gt; l1 l2 
  &lt;br&gt;
  &lt;br&gt;&lt;span style="color: green;"&gt;// Zip&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; z1 = seq&lt;span style="color: blue;"&gt;[&lt;/span&gt;&lt;span style="color: maroon;"&gt;'a'&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;.&lt;/span&gt;&lt;span style="color: blue;"&gt;.&lt;/span&gt;&amp;nbsp;&lt;span style="color: maroon;"&gt;'z'&lt;/span&gt;&lt;span style="color: blue;"&gt;]&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; z2 = seq&lt;span style="color: blue;"&gt;[&lt;/span&gt;&lt;span style="color: maroon;"&gt;'A'&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;.&lt;/span&gt;&lt;span style="color: blue;"&gt;.&lt;/span&gt;&amp;nbsp;&lt;span style="color: maroon;"&gt;'Z'&lt;/span&gt;&lt;span style="color: blue;"&gt;]&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; zipped = Seq&lt;span style="color: blue;"&gt;.&lt;/span&gt;zip z1 z2&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;Now, to use the C# version is rather simple.&amp;nbsp; The signature of this method is simply:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;static&lt;/span&gt; IEnumerable&amp;lt;TResult&amp;gt; Zip&amp;lt;TFirst, TSecond, TResult&amp;gt;( 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt; IEnumerable&amp;lt;TFirst&amp;gt; first,&amp;nbsp; &lt;br&gt;&amp;nbsp; IEnumerable&amp;lt;TSecond&amp;gt; second,&amp;nbsp; &lt;br&gt;&amp;nbsp; Func&amp;lt;TFirst, TSecond, TResult&amp;gt; func)&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;As you can see, this follows the same exact pattern as the map2 function from F#, which allows us to compose the two items together via a function to compute the hew item.&amp;nbsp; Let's define a few tests that will pass given our knowledge of the Zip function.&amp;nbsp; We can also include the Tuple class, well, at least the F# version to show that behavior as well.&amp;nbsp; These are basically tests that I wrote for my Functional C# library, but I hadn't published my tests, and I probably should.&amp;nbsp; &lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;[Fact] 
  &lt;br&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; ZipWithAdding_ShouldAddRanges() 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Arrange&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range1 = Enumerable.Range(&lt;span style="color: maroon;"&gt;1&lt;/span&gt;, &lt;span style="color: maroon;"&gt;10&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range2 = Enumerable.Range(&lt;span style="color: maroon;"&gt;11&lt;/span&gt;, &lt;span style="color: maroon;"&gt;10&lt;/span&gt;); 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Act&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range3 = range1.Zip(range2, (i, j) =&amp;gt; i + j); 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Assert&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.Count() == &lt;span style="color: maroon;"&gt;10&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.ElementAt(&lt;span style="color: maroon;"&gt;0&lt;/span&gt;) == &lt;span style="color: maroon;"&gt;12&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.ElementAt(&lt;span style="color: maroon;"&gt;9&lt;/span&gt;) == &lt;span style="color: maroon;"&gt;30&lt;/span&gt;); 
  &lt;br&gt;} 
  &lt;br&gt;
  &lt;br&gt;[Fact] 
  &lt;br&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; ZipWithIntAndChar_ShouldCombine() 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Arrange&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range1 = Enumerable.Range(&lt;span style="color: maroon;"&gt;1&lt;/span&gt;, &lt;span style="color: maroon;"&gt;5&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range2 = &lt;span style="color: blue;"&gt;new&lt;/span&gt;[] { &lt;span style="color: maroon;"&gt;'a'&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'b'&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'c'&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'d'&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'e'&lt;/span&gt; }; 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Act&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range3 = range1.Zip(range2, (i, j) =&amp;gt; i + j.ToString()); 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Assert&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.Count() == &lt;span style="color: maroon;"&gt;5&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.ElementAt(&lt;span style="color: maroon;"&gt;0&lt;/span&gt;) == &lt;span style="color: maroon;"&gt;"1a"&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.ElementAt(&lt;span style="color: maroon;"&gt;4&lt;/span&gt;) == &lt;span style="color: maroon;"&gt;"5e"&lt;/span&gt;); 
  &lt;br&gt;} 
  &lt;br&gt;
  &lt;br&gt;[Fact] 
  &lt;br&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; ZipWithTuples_ShouldCombineLists() 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Arrange&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range1 = Enumerable.Range(&lt;span style="color: maroon;"&gt;1&lt;/span&gt;, &lt;span style="color: maroon;"&gt;5&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range2 = &lt;span style="color: blue;"&gt;new&lt;/span&gt;[] { &lt;span style="color: maroon;"&gt;'a'&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'b'&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'c'&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'d'&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'e'&lt;/span&gt; }; 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Act&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range3 = range1.Zip(range2, (i, j) =&amp;gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; Tuple&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;, &lt;span style="color: blue;"&gt;char&lt;/span&gt;&amp;gt;(i, j)); 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Assert&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.Count() == &lt;span style="color: maroon;"&gt;5&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.ElementAt(&lt;span style="color: maroon;"&gt;0&lt;/span&gt;).CompareTo( 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;new&lt;/span&gt; Tuple&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;, &lt;span style="color: blue;"&gt;char&lt;/span&gt;&amp;gt;(&lt;span style="color: maroon;"&gt;1&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'a'&lt;/span&gt;)) == &lt;span style="color: maroon;"&gt;0&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.ElementAt(&lt;span style="color: maroon;"&gt;4&lt;/span&gt;).CompareTo( 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;new&lt;/span&gt; Tuple&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;, &lt;span style="color: blue;"&gt;char&lt;/span&gt;&amp;gt;(&lt;span style="color: maroon;"&gt;5&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'e'&lt;/span&gt;)) == &lt;span style="color: maroon;"&gt;0&lt;/span&gt;); 
  &lt;br&gt;}&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;Because the Parallel Extensions for .NET are becoming part of the BCL in .NET 4.0, the ParallelEnumerable also has the Zip method as well, which allows us to take advantage of data parallelism, should our machine allow such as this:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;[Fact] 
  &lt;br&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; ParallelZipWithAdding_ShouldAddRanges() 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Arrange&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range1 = ParallelEnumerable.Range(&lt;span style="color: maroon;"&gt;1&lt;/span&gt;, &lt;span style="color: maroon;"&gt;10&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range2 = ParallelEnumerable.Range(&lt;span style="color: maroon;"&gt;11&lt;/span&gt;, &lt;span style="color: maroon;"&gt;10&lt;/span&gt;); 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Act&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; range3 = range1.Zip(range2, (i, j) =&amp;gt; i + j); 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Assert&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.Count() == &lt;span style="color: maroon;"&gt;10&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.ElementAt(&lt;span style="color: maroon;"&gt;0&lt;/span&gt;) == &lt;span style="color: maroon;"&gt;12&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(range3.ElementAt(&lt;span style="color: maroon;"&gt;9&lt;/span&gt;) == &lt;span style="color: maroon;"&gt;30&lt;/span&gt;); 
  &lt;br&gt;}&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;With the including of the Parallel Extensions for .NET, it's going to be a lot of more functional fun built-into the BCL.&amp;nbsp; I can only hope for more of this coming down the road.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Wrapping It Up&lt;/h2&gt;

&lt;p&gt;Although the .NET Framework 4.0 release didn't give too many items for functional programming as they had in the .NET 3.5 release, the .NET 4.0 Framework has a few interesting items with the inclusion of the Tuple and Zip method.&amp;nbsp; Even more intrigue of course comes from adding the Parallel Extensions for the .NET Framework to the base class library as a first-class citizen.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;I'd wish they would have made the Tuple class public already so that we could at least play around with some of those features, but I as many understand why some of the decisions were made, and I also realize it's early in the cycle still.&amp;nbsp; Hopefully in the next release or so, it will become available to us to use, including some syntactic sugar about their creation and decomposition, but ultimately, that's a language decision and not necessarily a framework decision.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;It's great to see a language convergence in terms of libraries now being available to all.&amp;nbsp; F# will continue to be my language of choice, but with C# gaining some of these libraries, it makes the transition switch much easier without having to reinvent the wheel with either re-implementing the feature or converting Func delegates to F# FastFunc types and back again.&lt;/p&gt;
&lt;br&gt;&lt;br&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://weblogs.asp.net/podwysocki/archive/2008/11/16/functional-net-4-0-tuples-and-zip.aspx" mce_href="http://www.dotnetkicks.com/kick/?url=http://weblogs.asp.net/podwysocki/archive/2008/11/16/functional-net-4-0-tuples-and-zip.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://weblogs.asp.net/podwysocki/archive/2008/11/16/functional-net-4-0-tuples-and-zip.aspx" alt="kick it on DotNetKicks.com" mce_src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://weblogs.asp.net/podwysocki/archive/2008/11/16/functional-net-4-0-tuples-and-zip.aspx" border="0"&gt;&lt;/a&gt;
&lt;script src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/16/functional-net-4-0-tuples-and-zip.aspx" type="text/javascript" charset="utf-8" mce_src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/16/functional-net-4-0-tuples-and-zip.aspx"&gt;&lt;/script&gt;
&lt;script src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/16/functional-net-4-0-tuples-and-zip.aspx" type="text/javascript" charset="utf-8" mce_src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/16/functional-net-4-0-tuples-and-zip.aspx"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_title = 'Functional .NET 4.0 - Tuples and Zip;&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_blurb = 'Functional .NET 4.0 - Tuples and Zip';&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_url = 'http://weblogs.asp.net/podwysocki/archive/2008/11/16/functional-net-4-0-tuples-and-zip.aspx';&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_style = '1';&lt;/script&gt;
&lt;script src="http://widgets.dzone.com/widgets/zoneit.js" language="javascript"&gt;&lt;/script&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6740734" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=Vt2jN"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=Vt2jN" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=eVqvn"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=eVqvn" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=cxPZN"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=cxPZN" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=WG6tn"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=WG6tn" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/454623098" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=6740734</wfw:commentRss><feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=MatthewPodwysockisBlog&amp;itemurl=http%3A%2F%2Fweblogs.asp.net%2Fpodwysocki%2Farchive%2F2008%2F11%2F16%2Ffunctional-net-4-0-tuples-and-zip.aspx</feedburner:awareness><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2008/11/16/functional-net-4-0-tuples-and-zip.aspx</feedburner:origLink></item><item><title>.NET Code Contracts and TDD Are Complementary</title><link>http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~3/453480499/net-code-contracts-and-tdd-are-complementary.aspx</link><category>Spec#</category><category>TDD/BDD</category><category>C#</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Fri, 14 Nov 2008 17:11:00 -0600</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6736945</guid><description>&lt;p&gt;After my &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/11/08/code-contracts-and-net-4-0-spec-comes-alive.aspx"&gt;recent post&lt;/a&gt; on the introduction of &lt;a href="http://research.microsoft.com/contracts/"&gt;Code Contracts in .NET 4.0&lt;/a&gt;, I got some strong reaction that they would rather rely on TDD, or the better term, Example Driven Development specifications to ensure correctness.&amp;nbsp; Instead, it's my intent to talk about how they can complement each other in some rather powerful ways, such as having contracts bound to interfaces instead of class instances to ensure uniform interaction.&amp;nbsp; When we combine these two things together, the edge cases of our behaviors quickly melt away.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Design by Contract Basics&lt;/h2&gt;

&lt;p&gt; In order to understand how it can help, it's best to understand more about what Design by Contract can do for us.&amp;nbsp; The basic idea revolves around three questions:&lt;/p&gt;

&lt;ul&gt;
  
&lt;li&gt;What does it expect? (Pre-conditions)&lt;/li&gt;

  
&lt;li&gt;What does it guarantee? (Post-conditions)&lt;/li&gt;

  
&lt;li&gt;What does it maintain? (Invariants)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, to map that to actual software terms, these constraints are enforced at the method level.&amp;nbsp; Listed below are the basic rules for how contracts work:&lt;/p&gt;

&lt;ul&gt;
  
&lt;li&gt;Pre-conditions may be weakened and not strengthened by subclasses&lt;/li&gt;

  
&lt;li&gt;Post-conditions may be strengthened and not weakened by subclasses&lt;/li&gt;

  
&lt;li&gt;Invariants may be strengthened and not weakened by classes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The intent of DbC and using contracts, your program must never try to verify the contract conditions and instead fail hard, with the contract verification being your safety net.&amp;nbsp; This fail hard technique makes reading and understanding this code easier because the intended behavior is clearly specified.&amp;nbsp; When using the .NET Code Contracts, these hard failures are enforced either through the Debug.Assert or the Environment.FailFast implementations, depending on the build configuration.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;TDD and Contracts Strategies&lt;/h2&gt;

&lt;p&gt;We have a couple of options when we talk about Code Contracts and TDD.&amp;nbsp; Either way, the tests always come first.&amp;nbsp; Then what comes next is up to interpretation.&lt;/p&gt;

&lt;p&gt;We have the option of:&lt;/p&gt;

&lt;ol&gt;
  
&lt;li&gt;Defining the contract implementation after the test, to narrow the behavior of the code under test, then write the code required to pass.&amp;nbsp; &lt;/li&gt;

  
&lt;li&gt;Defining the code to achieve a passing test, then refactor the code to a contract implementation. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It really depends on the application as to which style might work better.&amp;nbsp; For most applications, I would recommend that you go with the second option where you define the code first to pass, and then refactor to an evolving contract design.&amp;nbsp; For those creating frameworks with predefined behaviors, the first option would probably work best.&lt;/p&gt;

&lt;p&gt;One thing that Code Contracts gives us is the ability apply intrinsic behavior to interfaces.&amp;nbsp; At that point, I no longer really call them interfaces, but abstract contracts is a better term for them.&amp;nbsp; I favor their use as much as possible to achieve uniform behavior across implementations and really becomes a powerful tool to enforce Don't Repeat Yourself (DRY).&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Red - Yellow - Green Refactor&lt;/h2&gt;

&lt;p&gt;One analogy that can be adapted here is the original idea of Red, Green, Refactor.&amp;nbsp; Let's take this a step further to use a phrase that Greg Young uses, which is Red, Yellow, Green, Refactor.&amp;nbsp; This is to say that we first implement the test, fail with a red light because no code actually exists for it to compile or work.&amp;nbsp; Then we implement the contract and our code to a point where it can at least compile, which would be yellow.&amp;nbsp; After that, we then pass our test with green after implementing the right amount of code to satisfy our contract.&amp;nbsp; We can refactor our contracts and code to clean it up a bit more, and move onto the next test.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;BDD for the Business and DbC for the Minutia&lt;/h2&gt;

&lt;p&gt;Another argument that can be made is that we could make is that we treat BDD for the business and Design by Contract for the minutia.&amp;nbsp; This idea is that we can continue to use the BDD syntax that we normally do, and then use contracts to enforce the edge constraints, to narrow our definitions.&amp;nbsp; This would allow us to focus on the business at the high level using BDD, and then the low level enforcement using Code Contracts.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Domain Driven Design and Design by Contract&lt;/h2&gt;

&lt;p&gt;One of the topics mentioned in the hallmark book Domain Driven Design, Eric Evans covers Design by Contract in Chapter 10 on Supple Design.&amp;nbsp; In this chapter, he states:&lt;/p&gt;

&lt;blockquote&gt;
  
&lt;p&gt;State post-conditions of operations and invariants of classes and aggregates.&amp;nbsp; If assertions cannot be coded directly into your programming language, write automated unit tests fort them.&amp;nbsp; Write them into documentation or diagrams where it its the style of the project's development status.&lt;/p&gt;

  
&lt;p&gt;Seek models with coherent sets of concepts, which lead a developer to infer the intended assertions, accelerating the learning curve and reducing the risk of contradictory code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What is interesting is that he talks about putting assertions into our code.&amp;nbsp; Given that Code Contracts are available to us to use, we can take advantage of exactly what he states.&amp;nbsp; These contracts then become part of the documentation of our code, with an intention revealing interface of what the behavior of this code is.&lt;/p&gt;

&lt;p&gt;Further, he states:&lt;/p&gt;

&lt;blockquote&gt;
  
&lt;p&gt;Even though many object oriented languages don't currently support assertions directly, assertions are still a powerful way of thinking about a design.&amp;nbsp; Automated unit tests can partially compensate for lack of language support.&amp;nbsp; Because assertions are all in the term of states, rather than procedures, they make tests easy to write.&amp;nbsp; The test setup puts the preconditions in place; then, after execution, the tests check whether the post-conditions hold.&lt;/p&gt;

  
&lt;p&gt;Clearly stated invariants and pre- and post-conditions allow for a developer to understand the consequences of using an operation or object....&amp;nbsp; so it's important to find models that make sense to people as well as satisfying the needs of the application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This states the case rather succinctly for using code contracts when the language supports them.&amp;nbsp; Indeed, to be able to statically enforce these constraints and prove their success or failure is key.&amp;nbsp; Enough though of the talk about DbC and Domain Driven Design, let's actually walk through an example of how to work these in conjunction together.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Walking Through an Example&lt;/h2&gt;

&lt;p&gt;Let's walk through a simple example to demonstrate how these two may work together.&amp;nbsp; In this example, we will define an immutable generic list that is optimized for recursion.&amp;nbsp; This implementation would be fairly similar to an implementation done by the F# team as part of the immutable collections.&amp;nbsp; I'll be going into that a bit later in a different post.&lt;/p&gt;

&lt;p&gt;The first example we want to give is for an empty list.&amp;nbsp; For the success criteria for this example, we will say that for a given empty list, an is empty flag should be true, the length is 0, and there should be exceptional behavior for checking the list head and tail.&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;[Fact] 
  &lt;br&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; EmptyList_ShouldBeEmpty() 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Act &lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IImmutableList&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt; list = ImmutableList&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;.Empty; 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Assert &lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(list.IsEmpty); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(list.Length == &lt;span style="color: maroon;"&gt;0&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.Throws&amp;lt;ArgumentException&amp;gt;(&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; () =&amp;gt; { &lt;font color="#0000ff"&gt;var&lt;/font&gt; i = list[&lt;span style="color: maroon;"&gt;0&lt;/span&gt;]; }); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.Throws&amp;lt;InvalidOperationException&amp;gt;( 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; () =&amp;gt; { &lt;font color="#0000ff"&gt;var&lt;/font&gt; head = list.Head; }); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.Throws&amp;lt;InvalidOperationException&amp;gt;( 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; () =&amp;gt; { &lt;font color="#0000ff"&gt;var&lt;/font&gt; tail = list.Tail; }); 
  &lt;br&gt;}&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;Now that the empty list example has been defined, let's actually work to implement the behavior through the use of a contract.&amp;nbsp; We're going to follow the first option enumerated above on defining a contract first, and then create the code to implement the feature, because this is more of a framework piece than it is for any standard line of business application code.&amp;nbsp; In order to create code contracts on interfaces, we need to create a ContractClass that contains our contracts and decorate our interface for it.&amp;nbsp; I will have to define later a class that implements the code contract class.&lt;/p&gt;

&lt;p&gt;In my test above, I need to define a few items on my interface for success, which is an empty flag, a length, the head and the tail of the list.&amp;nbsp; Below is the definition of this interface.&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;[ContractClass(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(IImmutableList_Contract&amp;lt;&amp;gt;))]&amp;nbsp; &lt;br&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;interface&lt;/span&gt; IImmutableList&amp;lt;T&amp;gt; : IEnumerable&amp;lt;T&amp;gt;&amp;nbsp; &lt;br&gt;{&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; IsEmpty { [Pure] &lt;span style="color: blue;"&gt;get&lt;/span&gt;; }&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; Length { [Pure] &lt;span style="color: blue;"&gt;get&lt;/span&gt;; }&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; T Head { [Pure] &lt;span style="color: blue;"&gt;get&lt;/span&gt;; }&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IImmutableList&amp;lt;T&amp;gt; Tail { [Pure] &lt;span style="color: blue;"&gt;get&lt;/span&gt;; }&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; T &lt;span style="color: blue;"&gt;this&lt;/span&gt;[&lt;span style="color: blue;"&gt;int&lt;/span&gt; index] { [Pure] &lt;span style="color: blue;"&gt;get&lt;/span&gt;; }&amp;nbsp; &lt;br&gt;}&amp;nbsp; &lt;br&gt;&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;Once this is defined, we can now create our contract class.&amp;nbsp; This must implement the IImmutableList&amp;lt;T&amp;gt; interface as well as mark it with an attribute stating that it is the contract class for the given interface.&amp;nbsp; Each method in turn must be implemented with their given pre-conditions, post-conditions and invariants.&amp;nbsp; Each method must also return the result from the comutation by using the CodeContract.Result&amp;lt;T&amp;gt;() method.&amp;nbsp; &lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;
  &lt;br&gt;[ContractClassFor(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(IImmutableList&amp;lt;&amp;gt;))] 
  &lt;br&gt;&lt;span style="color: blue;"&gt;internal&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;sealed&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt; IImmutableList_Contract&amp;lt;T&amp;gt; : IImmutableList&amp;lt;T&amp;gt; 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; IEnumerator&amp;lt;T&amp;gt; GetEnumerator() 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodeContract.Ensures(IsEmpty ? 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;null&lt;/span&gt; == CodeContract.Result&amp;lt;IEnumerator&amp;lt;T&amp;gt;&amp;gt;() : 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;null&lt;/span&gt; != CodeContract.Result&amp;lt;IEnumerator&amp;lt;T&amp;gt;&amp;gt;()); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; CodeContract.Result&amp;lt;IEnumerator&amp;lt;T&amp;gt;&amp;gt;(); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; GetEnumerator(); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;bool&lt;/span&gt; IsEmpty 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Pure] 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; CodeContract.Result&amp;lt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;(); } 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt; Length 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Pure] 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;get&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodeContract.Ensures(&lt;span style="color: maroon;"&gt;0&lt;/span&gt; &amp;lt;= CodeContract.Result&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;()); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; CodeContract.Result&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;(); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; T Head 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Pure] 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;get&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodeContract.Requires(!IsEmpty); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; CodeContract.Result&amp;lt;T&amp;gt;(); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; IImmutableList&amp;lt;T&amp;gt; Tail 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Pure] 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;get&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodeContract.Requires(!IsEmpty); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; CodeContract.Result&amp;lt;IImmutableList&amp;lt;T&amp;gt;&amp;gt;(); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; T &lt;span style="color: blue;"&gt;this&lt;/span&gt;[&lt;span style="color: blue;"&gt;int&lt;/span&gt; index] 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Pure] 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;get&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodeContract.Requires(!IsEmpty); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodeContract.Requires(&lt;span style="color: maroon;"&gt;0&lt;/span&gt; &amp;lt;= index &amp;amp;&amp;amp; Length &amp;lt; index); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; CodeContract.Result&amp;lt;T&amp;gt;(); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;}&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;Now there is enough code to satisfy my preconditions.&amp;nbsp; Moving onto the actual implementation, there is a bit of code required to get this code to pass.&amp;nbsp;&amp;nbsp; Let's implement the bare bones code that would be required to get the test to pass.&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;abstract&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt; ImmutableList&amp;lt;T&amp;gt; : IImmutableList&amp;lt;T&amp;gt; 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt; _Empty : ImmutableList&amp;lt;T&amp;gt; { } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt; _Cons : ImmutableList&amp;lt;T&amp;gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; T _Head { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;internal&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;; } 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; ImmutableList&amp;lt;T&amp;gt; _Tail { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;internal&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;; } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;internal&lt;/span&gt; _Cons(T head, ImmutableList&amp;lt;T&amp;gt; tail) 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _Head = head; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _Tail = tail; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;static&lt;/span&gt; IImmutableList&amp;lt;T&amp;gt; empty = 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;new&lt;/span&gt; _Empty(); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;static&lt;/span&gt; IImmutableList&amp;lt;T&amp;gt; Empty&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; empty; } } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;bool&lt;/span&gt; IsEmpty 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { [Pure] &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;is&lt;/span&gt; _Empty; } } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt; Length 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { [Pure] &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: maroon;"&gt;0&lt;/span&gt;; } } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; T Head 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { [Pure] &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; ((_Cons)&lt;span style="color: blue;"&gt;this&lt;/span&gt;)._Head; } } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; IImmutableList&amp;lt;T&amp;gt; Tail 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { [Pure] &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; ((_Cons)&lt;span style="color: blue;"&gt;this&lt;/span&gt;)._Tail; } } 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;[&lt;span style="color: blue;"&gt;int&lt;/span&gt; index] 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { [Pure] &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;default&lt;/span&gt;(T) } } 
  &lt;br&gt;}&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;Now that we got enough code to compile, we have another problem when we run the tests?&amp;nbsp; What is it?&amp;nbsp; Well, our exceptional cases go away, because the system will fail hard should you violate them by either calling Debug.Assert or Environment.FailFast.&amp;nbsp; Therefore, the test needs to be rewritten slightly to leave off those exceptional cases.&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;[Fact]&amp;nbsp; &lt;br&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; EmptyList_ShouldBeEmpty()&amp;nbsp; &lt;br&gt;{&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Act&amp;nbsp; &lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IImmutableList&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt; list = ImmutableList&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;.Empty;&amp;nbsp; &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Assert&amp;nbsp; &lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(list.IsEmpty);&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(list.Length == &lt;span style="color: maroon;"&gt;0&lt;/span&gt;);&amp;nbsp; &lt;br&gt;}&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;Now the test, as written should compile and succeed.&amp;nbsp; What we have gained here is quite a bit.&amp;nbsp; With static proving, and the ability to encapsulate behavior in our interfaces, we have saved ourselves from DRY for subsequent implementations of this interface.&amp;nbsp; Given the success of this, we are now able to move on to our next test which is when we have items in the list, we should have a head, a tail, not empty and a length of 10.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;[Fact] 
  &lt;br&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; NonEmptyList_ShouldShowProperCount() 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Arrange&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var range = Enumerable.Range(&lt;span style="color: maroon;"&gt;1&lt;/span&gt;, &lt;span style="color: maroon;"&gt;10&lt;/span&gt;); 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Act&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IImmutableList&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt; list =&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ImmutableList&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;.FromIEnumerator(range.GetEnumerator()); 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Assert&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(list.Length == &lt;span style="color: maroon;"&gt;10&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.False(list.IsEmpty); &lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(list[0] == &lt;span style="color: maroon;"&gt;1&lt;/span&gt;); &lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(list.Head == &lt;span style="color: maroon;"&gt;1&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.True(list.Tail != &lt;span style="color: blue;"&gt;null&lt;/span&gt;); 
  &lt;br&gt;}&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;As we define more tests or examples as we call them, we further refine the contract to include the proper bounds checks and add more functionality to the interface and to the list implementation.&amp;nbsp; What's nice about this implementation is that our contracts don't litter our code, but instead are enforced at the interface level.&amp;nbsp; That's not to say that we can't later on add more invariants, pre-conditions and post-conditions to our class implementations itself instead of the interface level.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Checked Exceptions All Over Again?&lt;/h2&gt;

&lt;p&gt;Some people may look at this and start to wonder if this is checked exceptions all over again.&amp;nbsp; It's not for some simple reasons.&amp;nbsp; First off, the code contract implementation does not force you to handle the exceptions in any way.&amp;nbsp; Instead, when this becomes part of the documentation, it states what the pre-conditions, post-conditions, and invariants are, and during compilation, they will be displayed back as warnings when you have violated the contracts, as well at runtime as hard failures.&amp;nbsp; In the Java world, you are forced to handle the exceptions, even if you met the pre-conditions, which isn't very helpful.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Wrapping It Up&lt;/h2&gt;

&lt;p&gt;We have covered quite a bit here today.&amp;nbsp; I hope this overview can give you a brief understanding of how Design by Contract can work in conjunction with TDD to produce verifiable code.&amp;nbsp; Given the ability to statically verify our contracts and behavior is important as our contracts then become part of the documentation for our system.&amp;nbsp; In the next post in this series, we're going to talk more about guidance on where you should compromise and where you should never compromise when it comes to Code Contracts.&lt;/p&gt;

&lt;p&gt;In the mean time, as always, download the latest version of the &lt;a href="http://research.microsoft.com/contracts/"&gt;.NET Code Contracts&lt;/a&gt; and give the team feedback.&lt;/p&gt;


&lt;br&gt;&lt;br&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://weblogs.asp.net/podwysocki/archive/2008/11/14/net-code-contracts-and-tdd-are-complementary.aspx" mce_href="http://www.dotnetkicks.com/kick/?url=http://weblogs.asp.net/podwysocki/archive/2008/11/14/net-code-contracts-and-tdd-are-complementary.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://weblogs.asp.net/podwysocki/archive/2008/11/14/net-code-contracts-and-tdd-are-complementary.aspx" alt="kick it on DotNetKicks.com" mce_src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://weblogs.asp.net/podwysocki/archive/2008/11/14/net-code-contracts-and-tdd-are-complementary.aspx" border="0"&gt;&lt;/a&gt;
&lt;script src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/14/net-code-contracts-and-tdd-are-complementary.aspx" type="text/javascript" charset="utf-8" mce_src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/14/net-code-contracts-and-tdd-are-complementary.aspx"&gt;&lt;/script&gt;
&lt;script src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/14/net-code-contracts-and-tdd-are-complementary.aspx" type="text/javascript" charset="utf-8" mce_src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/14/net-code-contracts-and-tdd-are-complementary.aspx"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_title = '.NET Code Contracts and TDD Are Complementary;&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_blurb = '.NET Code Contracts and TDD Are Complementary';&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_url = 'http://weblogs.asp.net/podwysocki/archive/2008/11/14/net-code-contracts-and-tdd-are-complementary.aspx';&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_style = '1';&lt;/script&gt;
&lt;script src="http://widgets.dzone.com/widgets/zoneit.js" language="javascript"&gt;&lt;/script&gt;

&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6736945" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=yGvxN"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=yGvxN" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=uJEan"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=uJEan" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=ZfvuN"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=ZfvuN" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=jxFXn"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=jxFXn" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/453480499" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=6736945</wfw:commentRss><feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=MatthewPodwysockisBlog&amp;itemurl=http%3A%2F%2Fweblogs.asp.net%2Fpodwysocki%2Farchive%2F2008%2F11%2F14%2Fnet-code-contracts-and-tdd-are-complementary.aspx</feedburner:awareness><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2008/11/14/net-code-contracts-and-tdd-are-complementary.aspx</feedburner:origLink></item><item><title>KaizenConf'08 Functional Programming Presentation Video</title><link>http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~3/451302657/kaizenconf-08-functional-programming-presentation-video.aspx</link><category>C#</category><category>F#</category><category>Functional Programming</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Wed, 12 Nov 2008 19:19:00 -0600</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6731946</guid><description>&lt;p&gt;Last week, I provided a basic wrap up of my functional programming talk at the &lt;a href="http://kaizenconf.com/"&gt;Continuous Improvement in Software Development conference (KaizenConf)&lt;/a&gt; in Austin.&amp;nbsp; Little did I realize that I was indeed being filmed while I talked by &lt;a href="http://www.markwatson.net/"&gt;Mark Leon Watson&lt;/a&gt;.&amp;nbsp; So, without further ado, enjoy the first 20 minutes of my talk.&amp;nbsp; I should emphasize though that it's not the official position that designers will never be there, but instead, it's not the highest priority.&amp;nbsp; In time, they may come as needs arise.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;In this section of the talk, I cover the following areas:&lt;/p&gt;

&lt;ul&gt;
  
&lt;li&gt;What is Functional Programming?&lt;/li&gt;

  
&lt;li&gt;Why does it matter?&lt;/li&gt;

  
&lt;li&gt;How do we do it?&lt;/li&gt;

  
&lt;li&gt;Functional C#, what parts are functional and what parts aren't&lt;/li&gt;

  
&lt;li&gt;What is F# and why is it useful?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;a href="http://weblogs.asp.net/controlpanel/blogs/" style="left: 0px ! important; top: 13px ! important;" title="Click here to block this object with Adblock Plus" class="abp-objtab-08260330634433517 visible ontop"&gt;&lt;/a&gt;&lt;a href="http://vimeo.com/moogaloop.swf?clip_id=2200193&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=&amp;amp;fullscreen=1" style="left: 0px ! important; top: 13px ! important;" title="Click here to block this object with Adblock Plus" class="abp-objtab-08260330634433517 visible ontop"&gt;&lt;/a&gt;&lt;a href="http://weblogs.asp.net/controlpanel/blogs/" style="left: 400px ! important; top: 0px ! important;" title="Click here to block this object with Adblock Plus" class="abp-objtab-08260330634433517 visible ontop"&gt;&lt;/a&gt;&lt;a href="http://vimeo.com/moogaloop.swf?clip_id=2200193&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=&amp;amp;fullscreen=1" style="left: 400px ! important; top: 0px ! important;" title="Click here to block this object with Adblock Plus" class="abp-objtab-08260330634433517 visible ontop"&gt;&lt;/a&gt;
&lt;a href="http://weblogs.asp.net/controlpanel/blogs/" style="left: 0px ! important; top: 13px ! important;" title="Click here to block this object with Adblock Plus" class="abp-objtab-08260330634433517 visible ontop"&gt;&lt;/a&gt;&lt;a href="http://vimeo.com/moogaloop.swf?clip_id=2200193&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=&amp;amp;fullscreen=1" style="left: 0px ! important; top: 13px ! important;" title="Click here to block this object with Adblock Plus" class="abp-objtab-08260330634433517 visible ontop"&gt;&lt;/a&gt;&lt;a href="http://weblogs.asp.net/controlpanel/blogs/" style="left: 400px ! important; top: 0px ! important;" title="Click here to block this object with Adblock Plus" class="abp-objtab-09297100708137077 visible ontop"&gt;&lt;/a&gt;&lt;a href="http://vimeo.com/moogaloop.swf?clip_id=2200193&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=&amp;amp;fullscreen=1" style="left: 400px ! important; top: 0px ! important;" title="Click here to block this object with Adblock Plus" class="abp-objtab-09297100708137077 visible ontop"&gt;&lt;/a&gt;&lt;embed src="http://vimeo.com/moogaloop.swf?clip_id=2200193&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=&amp;amp;fullscreen=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="267"&gt;
&lt;br&gt;&lt;a href="http://vimeo.com/2200193"&gt;Functional Programming - Is it a Game Changer? (Part 1 - KaizenConf '08 Workshop)&lt;/a&gt; from &lt;a href="http://vimeo.com/markleon"&gt;Mark Leon Watson&lt;/a&gt; on &lt;a href="http://vimeo.com/"&gt;Vimeo&lt;/a&gt; 

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Thanks to Mark and I hope you all enjoy and feedback would be appreciated.&amp;nbsp; I haven't yet made my F# samples available, but as always, my C# ones are available at the &lt;a href="http://code.msdn.microsoft.com/FunctionalCSharp"&gt;Functional C# library&lt;/a&gt; on MSDN Code Gallery.&amp;nbsp; More will be added there shortly to reflect some of the things I talked about here in the slides.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
TODO:

Email Scott Lock

&lt;br&gt;&lt;br&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://weblogs.asp.net/podwysocki/archive/2008/11/12/kaizenconf-08-functional-programming-presentation-video.aspx" mce_href="http://www.dotnetkicks.com/kick/?url=http://weblogs.asp.net/podwysocki/archive/2008/11/12/kaizenconf-08-functional-programming-presentation-video.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://weblogs.asp.net/podwysocki/archive/2008/11/12/kaizenconf-08-functional-programming-presentation-video.aspx" alt="kick it on DotNetKicks.com" mce_src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://weblogs.asp.net/podwysocki/archive/2008/11/12/kaizenconf-08-functional-programming-presentation-video.aspx" border="0"&gt;&lt;/a&gt;
&lt;script src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/12/kaizenconf-08-functional-programming-presentation-video.aspx" type="text/javascript" charset="utf-8" mce_src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/12/kaizenconf-08-functional-programming-presentation-video.aspx"&gt;&lt;/script&gt;
&lt;script src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/12/kaizenconf-08-functional-programming-presentation-video.aspx" type="text/javascript" charset="utf-8" mce_src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/12/kaizenconf-08-functional-programming-presentation-video.aspx"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_title = 'KaizenConf'08 Functional Programming Presentation Video;&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_blurb = 'KaizenConf'08 Functional Programming Presentation Video';&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_url = 'http://weblogs.asp.net/podwysocki/archive/2008/11/12/kaizenconf-08-functional-programming-presentation-video.aspx';&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_style = '1';&lt;/script&gt;
&lt;script src="http://widgets.dzone.com/widgets/zoneit.js" language="javascript"&gt;&lt;/script&gt;

&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6731946" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=FsHpN"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=FsHpN" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=icchn"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=icchn" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=ObWXN"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=ObWXN" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=lv7Nn"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=lv7Nn" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/451302657" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=6731946</wfw:commentRss><feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=MatthewPodwysockisBlog&amp;itemurl=http%3A%2F%2Fweblogs.asp.net%2Fpodwysocki%2Farchive%2F2008%2F11%2F12%2Fkaizenconf-08-functional-programming-presentation-video.aspx</feedburner:awareness><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2008/11/12/kaizenconf-08-functional-programming-presentation-video.aspx</feedburner:origLink></item><item><title>Code Contracts for .NET 4.0 - Spec# Comes Alive</title><link>http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~3/446225207/code-contracts-for-net-4-0-spec-comes-alive.aspx</link><category>Spec#</category><category>C#</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Fri, 07 Nov 2008 23:40:00 -0600</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6726228</guid><description>&lt;p&gt;As I've said in many posts before, I'm a big fan of stating your preconditions, postconditions, invariants and so on explicitly in your code through the use of contracts in the Design by Contract Parlance.&amp;nbsp; &lt;a href="http://research.microsoft.com/specsharp/"&gt;Spec#&lt;/a&gt; is a project that came from Microsoft Research as a language based upon C# to add Design by Contract features to the language.&amp;nbsp; I've talked extensively on the subject in the past on this blog and previous blogs of mine especially around the time of the ALT.NET Open Spaces, Seattle event back in April of this year.&amp;nbsp; The importance of making side effects well known is something that I mentioned briefly during my Approaching Functional Programming talk at KaizenConf.&amp;nbsp; To be able to express and statically verify behavior is important in regards to side effects and method purity.&lt;/p&gt;

&lt;p&gt;One of the less talked about items from this year's Professional Developer Conference was &lt;a href="http://research.microsoft.com/contracts/"&gt;Code Contracts for .NET&lt;/a&gt;.&amp;nbsp; This talk was bundled in with automated test generation with Pex for a combined talk called &lt;a href="http://channel9.msdn.com/pdc2008/TL51/"&gt;"Research: Contract Checking and Automated Test Generation with Pex"&lt;/a&gt;.&amp;nbsp; In this talk, &lt;a href="http://research.microsoft.com/%7Embarnett/"&gt;Mike Barnett&lt;/a&gt;, of the Spec# team was joined by &lt;a href="http://research.microsoft.com/users/nikolait/"&gt;Nikolai Tillmann&lt;/a&gt;, who is currently leading the &lt;a href="http://research.microsoft.com/pex/"&gt;Pex Project&lt;/a&gt;, and they talk about the project, Code Contracts for .NET.&amp;nbsp; I encourage you to watch the video to see how it works and the benefits of using the library.&amp;nbsp; It's been rewarding to watch how Spec# has grown in presence in recent months, especially the publicity that both &lt;a href="http://codebetter.com/blogs/gregyoung/"&gt;Greg Young&lt;/a&gt; and I have given them.&amp;nbsp; Mike has done a great job with this project and I'm really excited for the future of it.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Code Contracts for .NET&lt;/h2&gt;

&lt;p&gt;It was announced that a subset of Spec#, the contracts library, would be included as part of the base class library in .NET 4.0.&amp;nbsp; When .NET 3.5 came out, if you opened System.Core.dll through .NET Reflector, you may have noticed that there was a namespace in there called Microsoft.Contracts.&amp;nbsp; Since that time, the Spec# team has worked with the Base Class Library team to help integrate this more thoroughly into the libraries as a language agnostic approach.&amp;nbsp; There are many decisions to be made as languages and frameworks evolve as to what goes where.&amp;nbsp; To make these features as library features allows for more rapid change and flexibility than if they were exclusive language constructs such as they were with Spec#.&amp;nbsp; After all, I want to be able to switch to languages such as F# and still have the ability to statically verify my code.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;First things first, what is Design by Contract?&amp;nbsp; It's an approach to software development that was coined by Dr. Betrand Meyer through the creation of the Eiffel language.&amp;nbsp; The main tenets of this paradigm include:&lt;/p&gt;

&lt;ul&gt;
  
&lt;li&gt;What does it expect? (Preconditions) &lt;/li&gt;

  
&lt;li&gt;What does it guarantee? (Postconditions) &lt;/li&gt;

  
&lt;li&gt;What does it maintain? (Invariants) &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The idea is to have these tenets enforced programmatically through the language itself.&amp;nbsp; To this end, in the .NET framework, outside of Spec#, haven't had this available to them.&amp;nbsp; So, the Spec# team realized, that instead of the language approach, maybe they should focus on a language agnostic approach through the use of libraries, and static verification.&amp;nbsp; Now, I can feel free to use my language of choice inside the .NET framework to utilize these contracts, such as F#, C#, VB, etc.&amp;nbsp; As part of the release at PDC, we have the libraries available to us for use in Visual Studio 2008, so we'll be going through what exactly that means.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;First, you need to install the Code Contracts for .NET, which you can find &lt;a href="http://research.microsoft.com/research/downloads/details/4ed7dd5f-490b-489e-8ca8-109324279968/details.aspx"&gt;here&lt;/a&gt;.&amp;nbsp; This integrates with Visual Studio 2008 and adds an additional property window to your project files.&amp;nbsp; This gives us the ability to enforce contracts using the Microsoft.Contracts.dll.&amp;nbsp; If you look at the property page for your given project, it should look like this.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;&lt;img src="http://codebetter.com/blogs/matthew.podwysocki/WindowsLiveWriter/CodeContractsand.NET4.0SpecComesAlive_98A7/contracts_thumb.png" style="border-width: 0px;" alt="contracts" width="520" border="0" height="391"&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;As you can see, we have the ability to perform runtime checking, static contract checking with non-null obligations and array bounds automatically.&amp;nbsp; The install comes with a PDF with some documentation on how to use the library when covering preconditions, postconditions, invariants, purity and contract classes.&amp;nbsp; Let's go through a quick example of using this library.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Using the Library&lt;/h2&gt;

&lt;p&gt;In order to utilize contracts, you must use the CodeContract class from the System.Diagnostics.Contracts namespace.&amp;nbsp; The CodeContract has a few interesting methods worth exploring.&amp;nbsp; &lt;/p&gt;

&lt;table width="662" border="0" cellpadding="2" cellspacing="0"&gt;&lt;tbody&gt;
    
&lt;tr&gt;
      
&lt;td valign="top" width="175"&gt;&lt;b&gt;Method&lt;/b&gt;&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;&lt;b&gt;Meaning&lt;/b&gt;&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="177"&gt;Assert &lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Express contract assertions &lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="178"&gt;Assume&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Express contract assumptions that should hold. Assume to be true or false&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="179"&gt;EndContractBlock&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Used to mark the end of a code contract block of if statements&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="180"&gt;Ensures&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Express postconditions&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="181"&gt;EnsuresOnThrow&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Express postconditions that abnormally terminate a method&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="181"&gt;Exists&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Ensure a value exists in a collection&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="181"&gt;ForAll&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Express constraint for each item in a collection&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="181"&gt;Invariant&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Express invariant constraints&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="181"&gt;OldValue&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Retrieve the old value from the pre-state&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="181"&gt;Requires&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Express preconditions&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="181"&gt;RequiresAlways&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Express preconditions for all build configurations&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="181"&gt;Result&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Get the method's return value&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="181"&gt;Throws&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Express exceptional postcondition&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="181"&gt;ValueAtReturn&lt;/td&gt;

      
&lt;td valign="top" width="485"&gt;Specify out parameter value for postcondition checking&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;There are several attributes worth noting as well in this library as well:&lt;/p&gt;

&lt;table width="749" border="0" cellpadding="2" cellspacing="0"&gt;&lt;tbody&gt;
    
&lt;tr&gt;
      
&lt;td valign="top" width="276"&gt;Attribute&lt;/td&gt;

      
&lt;td valign="top" width="471"&gt;Meaning&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="278"&gt;ContractClassAttribute&lt;/td&gt;

      
&lt;td valign="top" width="471"&gt;Define a code contract class used to enforce contracts on an interface&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="280"&gt;ContractClassForAttribute&lt;/td&gt;

      
&lt;td valign="top" width="471"&gt;Define a code contract class for a given interface&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="281"&gt;ContractInvariantMethodAttribute&lt;/td&gt;

      
&lt;td valign="top" width="471"&gt;Used to mark a method that represents the object invariant&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="282"&gt;ContractPublicPropertyNameAttribute&lt;/td&gt;

      
&lt;td valign="top" width="471"&gt;Used in a method contract where the method is more visible than the field&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="283"&gt;ContractVerificationAttribute&lt;/td&gt;

      
&lt;td valign="top" width="471"&gt;Used to assume correctness of the given assembnly&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="284"&gt;PureAttribute&lt;/td&gt;

      
&lt;td valign="top" width="471"&gt;Express method purity&lt;/td&gt;
    &lt;/tr&gt;

    
&lt;tr&gt;
      
&lt;td valign="top" width="285"&gt;RuntimeContractAttribute&lt;/td&gt;

      
&lt;td valign="top" width="471"&gt;Added to the assembly via the rewriter to express that is has already been rewritten&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;As you can see here, we have a lot of goodies in here.&amp;nbsp; Let's step just through preconditions for right now.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Preconditions&lt;/h2&gt;

&lt;p&gt;The first item we noted in our definition of Design by Contract is what do we expect.&amp;nbsp; In this example, let's create a simple Geocoordinate value object used to hold latitude and longitude values.&amp;nbsp; There are some constraints we need to use to ensure correctness.&amp;nbsp; We need to enforce preconditions on the constructor as well as a function used to calculate the distance between two Geocoordinate classes.&amp;nbsp; Since I want this available in all build configuration, I will use the CodeContract.RequiresAlways method to define my precondition contracts.&lt;/p&gt;

&lt;p&gt;Let's take a look at what that might look like:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt; Geocoordinate 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;readonly&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;double&lt;/span&gt; latitude; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;readonly&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;double&lt;/span&gt; longitude; 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; Geocoordinate(&lt;span style="color: blue;"&gt;double&lt;/span&gt; latitude, &lt;span style="color: blue;"&gt;double&lt;/span&gt; longitude) 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodeContract.RequiresAlways(latitude &amp;lt;= &lt;span style="color: maroon;"&gt;90&lt;/span&gt;&lt;span style="color: maroon;"&gt;.0&lt;/span&gt; &amp;amp;&amp;amp; latitude &amp;gt;= -&lt;span style="color: maroon;"&gt;90&lt;/span&gt;&lt;span style="color: maroon;"&gt;.0&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodeContract.RequiresAlways(longitude &amp;lt;= &lt;span style="color: maroon;"&gt;180&lt;/span&gt;&lt;span style="color: maroon;"&gt;.0&lt;/span&gt; &amp;amp;&amp;amp; longitude &amp;gt;= -&lt;span style="color: maroon;"&gt;180&lt;/span&gt;&lt;span style="color: maroon;"&gt;.0&lt;/span&gt;); 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.latitude = latitude; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.longitude = longitude; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Pure] 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;double&lt;/span&gt; Latitude { &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; latitude; } } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Pure] 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;double&lt;/span&gt; Longitude { &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; longitude; } } 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Pure] 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;double&lt;/span&gt; GetDistance(Geocoordinate g1, Geocoordinate g2) 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodeContract.RequiresAlways(g1 != &lt;span style="color: blue;"&gt;null&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodeContract.RequiresAlways(g2 != &lt;span style="color: blue;"&gt;null&lt;/span&gt;); 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;const&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;double&lt;/span&gt; R = &lt;span style="color: maroon;"&gt;3956&lt;/span&gt;; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;const&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;double&lt;/span&gt; degreesToRadians = Math.PI / &lt;span style="color: maroon;"&gt;180&lt;/span&gt;; 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; lon1 = g1.Longitude * degreesToRadians; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; lat1 = g1.Latitude * degreesToRadians; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; lon2 = g2.Longitude * degreesToRadians; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; lat2 = g2.Latitude * degreesToRadians; 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; dlon = lon2 - lon1; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; dlat = lat2 - lat1; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; a = Math.Pow(Math.Sin(dlat / &lt;span style="color: maroon;"&gt;2&lt;/span&gt;), &lt;span style="color: maroon;"&gt;2&lt;/span&gt;) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(dlon / &lt;span style="color: maroon;"&gt;2&lt;/span&gt;), &lt;span style="color: maroon;"&gt;2&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; c = &lt;span style="color: maroon;"&gt;2&lt;/span&gt; * Math.Atan2(Math.Sqrt(a), Math.Sqrt(&lt;span style="color: maroon;"&gt;1&lt;/span&gt; - a)); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; d = R * c; 
  &lt;br&gt;
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; d;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;}&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;I also added method purity just for clarity sake.&amp;nbsp; This allows me to specify that I am not changing object state, and is quite useful for enforcement.&amp;nbsp; This makes it obvious to the caller that this method does no more than compute the result and return it.&amp;nbsp; That's important and reassuring when using third party libraries.&amp;nbsp; Let's say I call this class with the following code:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[] args) 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; g = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Geocoordinate(&lt;span style="color: maroon;"&gt;91&lt;/span&gt;&lt;span style="color: maroon;"&gt;.0&lt;/span&gt;, &lt;span style="color: maroon;"&gt;181&lt;/span&gt;&lt;span style="color: maroon;"&gt;.0&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; g1 = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Geocoordinate(&lt;span style="color: maroon;"&gt;40&lt;/span&gt;&lt;span style="color: maroon;"&gt;.774&lt;/span&gt;, &lt;span style="color: maroon;"&gt;73&lt;/span&gt;&lt;span style="color: maroon;"&gt;.972&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; g2 = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Geocoordinate(&lt;span style="color: maroon;"&gt;40&lt;/span&gt;&lt;span style="color: maroon;"&gt;.811&lt;/span&gt;, &lt;span style="color: maroon;"&gt;73&lt;/span&gt;&lt;span style="color: maroon;"&gt;.910&lt;/span&gt;); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color="#0000ff"&gt;var&lt;/font&gt; distance = Geocoordinate.GetDistance(g1, g2); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(distance); 
  &lt;br&gt;}&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;You may notice that the g is out of bounds from my precondition.&amp;nbsp; When I run the application, I get a nasty reminder that I broke the contract.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://codebetter.com/blogs/matthew.podwysocki/WindowsLiveWriter/CodeContractsand.NET4.0SpecComesAlive_98A7/assert_fail_thumb.png" style="border: 0px none ;" alt="assert_fail" width="390" border="0" height="506"&gt; &lt;/p&gt;

&lt;p&gt;That's exactly the behavior we wanted to express through our precondition.&amp;nbsp; There is a lot more to cover in subsequent posts on the subject, but I hope this gives you a sense of some of the power of the library.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Wrapping It Up&lt;/h2&gt;

&lt;p&gt;This is just a first look at the new Code Contracts library.&amp;nbsp; In my talks at the Continuous Improvement in Software Development conference, I stressed the need to for our intentions to be more clear in our code.&amp;nbsp; This is a great first step to creating a language agnostic approach to static code verification.&amp;nbsp; To be able to ensure to the caller what the API will expect, return and maintain is especially powerful and I think important in the future of software development, especially in regards to state management.&amp;nbsp; The language agnostic approach given with this library is an important step forward, so that I could statically verify my code no matter the language on the .NET platform.&amp;nbsp; There is a lot more to this library to explore.&amp;nbsp; I urge you to &lt;a href="http://research.microsoft.com/research/downloads/details/4ed7dd5f-490b-489e-8ca8-109324279968/details.aspx"&gt;download it&lt;/a&gt;, and give them feedback.&amp;nbsp; You can post feedback on the &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/pex/thread/14115b4d-52c1-4e93-89cd-19db3fd86756/"&gt;Pex forums&lt;/a&gt; or send them an email at codconfb _at_ microsoft _dot_ com.&lt;/p&gt;
&lt;br&gt;&lt;br&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx" mce_href="http://www.dotnetkicks.com/kick/?url=http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx" alt="kick it on DotNetKicks.com" mce_src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx" border="0"&gt;&lt;/a&gt;
&lt;script src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx" type="text/javascript" charset="utf-8" mce_src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx"&gt;&lt;/script&gt;
&lt;script src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx" type="text/javascript" charset="utf-8" mce_src="http://feeds.feedburner.com/%7Es/MatthewPodwysocki?i=http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_title = 'Code Contracts for .NET 4.0 - Spec# Comes Alive;&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_blurb = 'Code Contracts for .NET 4.0 - Spec# Comes Alive';&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_url = 'http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx';&lt;/script&gt;
&lt;script type="text/javascript"&gt;var dzone_style = '1';&lt;/script&gt;
&lt;script src="http://widgets.dzone.com/widgets/zoneit.js" language="javascript"&gt;&lt;/script&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6726228" width="1" height="1"&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=4LUaN"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=4LUaN" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=0icSn"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=0icSn" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=FKlzN"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=FKlzN" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?a=jW38n"&gt;&lt;img src="http://feeds.feedburner.com/~f/MatthewPodwysockisBlog?i=jW38n" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~4/446225207" height="1" width="1"/&gt;</description><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/podwysocki/rsscomments.aspx?PostID=6726228</wfw:commentRss><feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=MatthewPodwysockisBlog&amp;itemurl=http%3A%2F%2Fweblogs.asp.net%2Fpodwysocki%2Farchive%2F2008%2F11%2F08%2Fcode-contracts-for-net-4-0-spec-comes-alive.aspx</feedburner:awareness><feedburner:origLink>http://weblogs.asp.net/podwysocki/archive/2008/11/08/code-contracts-for-net-4-0-spec-comes-alive.aspx</feedburner:origLink></item><item><title>KaizenConf Wrapup - Approaching Functional Programming</title><link>http://feeds.feedburner.com/~r/MatthewPodwysockisBlog/~3/443859813/kaizenconf-wrapup-approaching-functional-programming.aspx</link><category>C#</category><category>F#</category><category>Functional Programming</category><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">podwysocki</dc:creator><pubDate>Wed, 05 Nov 2008 19:33:00 -0600</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6722457</guid><description>&lt;p&gt;This past weekend, I had the opportunity to attend and give a workshop at the Continuous Improvement in Software Development, or KaizenConf.&amp;nbsp; I'm grateful to Scott Bellware and Dave Laribee for organizing this event.&amp;nbsp; The tone and subject matter at this conference was both refreshing and interesting.&amp;nbsp; This conference had a focus on continuous improvement through methodology, communication, as well as technology, which was a bit different than the ALT.NET Conferences, which had much more of a technology tilt.&amp;nbsp; The conversations had not only in the sessions, but also the hallways in between were enlightening, and it was enjoyable to get so many perspectives on software development issues.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Approaching Functional Programming&lt;/h2&gt;

&lt;p&gt;I want to thank everyone who attended my workshop on Thursday on functional programming.&amp;nbsp; It's a topic that I'm passionate about in regards to how it can improve your code significantly.&amp;nbsp; The title of my workshop was "Functional Programming - Is It A Game Changer", and instead, I think the talk is better titled "Approaching Functional Programming".&amp;nbsp; In this talk, I not only talked about the basics of functional programming, but how to mix and match the object oriented and imperative styles as well.&amp;nbsp; I had a pretty good attendance, even though I was up against a Domain Driven Design talk by Mr. Laribee and Textual DSLs in Boo with Ayende.&amp;nbsp; This was one of the sessions that was not recorded, but instead, I hope to give you some basic ideas of what I covered.&amp;nbsp; This talk was also given as part of an open space discussion as well.&lt;/p&gt;

&lt;p&gt;Some of the topics I covered were:&lt;/p&gt;

&lt;ul&gt;
  
&lt;li&gt;What is Functional Programming?&lt;/li&gt;

  
&lt;li&gt;Why is it important?&lt;/li&gt;

  
&lt;li&gt;How can I do it in .NET?&amp;nbsp; With C#?&amp;nbsp; With F#?&lt;/li&gt;

  
&lt;li&gt;How does it affect your code?&lt;/li&gt;

  
&lt;li&gt;Moving from imperative to functional&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many times when we're faced with functional programming, many people look with a blank stare on several topics, including immutability, functional composition, currying and most of all, monads.&amp;nbsp; Let's just go through some simple examples of each and learn how they might help us.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;From Mutable to Immutable&lt;/h2&gt;

&lt;p&gt;One of the easiest examples to understand is moving from mutable data structures to mutable ones.&amp;nbsp; The advantage of doing so is especially apparent when moving to data parallel applications.&amp;nbsp; If you don't have to worry about data ownership and which thread is writing to the structure, the less opportunity for unintended consequences.&amp;nbsp; Let's take a simple point class as our first example:&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt; Point2D 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt; x; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt; y; 
  &lt;br&gt;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; Point2D(&lt;span style="color: blue;"&gt;int&lt;/span&gt; x, &lt;span style="color: blue;"&gt;int&lt;/span&gt; y)&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.x = x; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.y = y; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt; MoveBy(&lt;span style="color: blue;"&gt;int&lt;/span&gt; dx, &lt;span style="color: blue;"&gt;int&lt;/span&gt; dy)&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x += dx; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y += dy; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;} &lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;Classes such as the above scream to me to be a value object, in the Domain Driven Design parlance.&amp;nbsp; As identity is not important to us, if we need to change it, we through it away and get a new one.&amp;nbsp; This stresses that these value objects are immutable.&amp;nbsp; Let's move this class to be an immutable data structure instead.&amp;nbsp; &lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt; Point2D 
  &lt;br&gt;{ 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;readonly&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt; x; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;readonly&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt; y; 
  &lt;br&gt;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; Point2D(&lt;span style="color: blue;"&gt;int&lt;/span&gt; x, &lt;span style="color: blue;"&gt;int&lt;/span&gt; y) &lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.x = x; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.y = y; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; Point2D MoveBy(&lt;span style="color: blue;"&gt;int&lt;/span&gt; dx, &lt;span style="color: blue;"&gt;int&lt;/span&gt; dy) &lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt; Point2D(x + dx, y + dy); 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
  &lt;br&gt;} &lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;As you can see, there is no unnecessary mutation here, instead, when we want to move the point, we are given a new point class.&amp;nbsp; This isn't to say that given a standard object oriented approach, that all values must be immutable, but instead, the state must be explicit.&amp;nbsp; I like the approach that F# takes in which you have to go out of your way to make your mutability explicit through either the mutable keyword or a reference cell.&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;type&lt;/span&gt; Point2D = 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;val&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;mutable&lt;/span&gt; x&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: blue;"&gt;float&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;val&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;mutable&lt;/span&gt; y&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: blue;"&gt;float&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: blue;"&gt;(&lt;/span&gt;dx, dy&lt;span style="color: blue;"&gt;)&lt;/span&gt; = &lt;span style="color: blue;"&gt;{&lt;/span&gt; x = dx; y = dy; &lt;span style="color: blue;"&gt;}&lt;/span&gt;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;member&lt;/span&gt; this&lt;span style="color: blue;"&gt;.&lt;/span&gt;MoveBy&lt;span style="color: blue;"&gt;(&lt;/span&gt;dx, dy&lt;span style="color: blue;"&gt;)&lt;/span&gt; = 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; this&lt;span style="color: blue;"&gt;.&lt;/span&gt;x &lt;span style="color: blue;"&gt;&amp;lt;-&lt;/span&gt; this&lt;span style="color: blue;"&gt;.&lt;/span&gt;x + dx 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; this&lt;span style="color: blue;"&gt;.&lt;/span&gt;y &lt;span style="color: blue;"&gt;&amp;lt;-&lt;/span&gt; this&lt;span style="color: blue;"&gt;.&lt;/span&gt;y + dy&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&amp;nbsp;&lt;/div&gt;

&lt;p&gt;Projects such as Spec#, which is being productized in .NET 4.0 through &lt;a href="http://research.microsoft.com/contracts/"&gt;Code Contracts&lt;/a&gt;, could also help.&amp;nbsp; More on this in a subsequent post.&amp;nbsp; Moving on to functional composition over inheritance.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Functional Composition over Inheritance&lt;/h2&gt;

&lt;p&gt;Many times in our .NET code, we find ourselves with large amounts of pomp and circumstance code which does a simple operation such as sorting.&amp;nbsp; Instead, we could apply functional composition through delegates to solve this issue.&amp;nbsp; Take the example of the following code used to sort some employee data.&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;type&lt;/span&gt; Employee = &lt;span style="color: blue;"&gt;{&lt;/span&gt; Id&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;; Name&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;; Level&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;; DateOfBirth&lt;span style="color: blue;"&gt;:&lt;/span&gt;DateTime &lt;span style="color: blue;"&gt;}&lt;/span&gt; 
  &lt;br&gt;
  &lt;br&gt;&lt;span style="color: blue;"&gt;type&lt;/span&gt; LevelComparer&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; = 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IComparer&amp;lt;Employee&amp;gt; &lt;span style="color: blue;"&gt;with&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;member&lt;/span&gt; x&lt;span style="color: blue;"&gt;.&lt;/span&gt;Compare&lt;span style="color: blue;"&gt;(&lt;/span&gt;e1, e2&lt;span style="color: blue;"&gt;)&lt;/span&gt; = 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e1&lt;span style="color: blue;"&gt;.&lt;/span&gt;Level&lt;span style="color: blue;"&gt;.&lt;/span&gt;CompareTo&lt;span style="color: blue;"&gt;(&lt;/span&gt;e2&lt;span style="color: blue;"&gt;.&lt;/span&gt;Level&lt;span style="color: blue;"&gt;)&lt;/span&gt; 
  &lt;br&gt;
  &lt;br&gt;&lt;span style="color: blue;"&gt;type&lt;/span&gt; NameComparer&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; = 
  &lt;br&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IComparer&amp;lt;Employee&amp;gt; &lt;span style="color: blue;"&gt;with&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;member&lt;/span&gt; x&lt;span style="color: blue;"&gt;.&lt;/span&gt;Compare&lt;span style="color: blue;"&gt;(&lt;/span&gt;e1, e2&lt;span style="color: blue;"&gt;)&lt;/span&gt; = 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e1&lt;span style="color: blue;"&gt;.&lt;/span&gt;Name&lt;span style="color: blue;"&gt;.&lt;/span&gt;CompareTo&lt;span style="color: blue;"&gt;(&lt;/span&gt;e2&lt;span style="color: blue;"&gt;.&lt;/span&gt;Name&lt;span style="color: blue;"&gt;)&lt;/span&gt; 
  &lt;br&gt;
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; employeeData = &lt;span style="color: blue;"&gt;new&lt;/span&gt; ResizeArray&amp;lt;Employee&amp;gt;&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; 
  &lt;br&gt;employeeData&lt;span style="color: blue;"&gt;.&lt;/span&gt;Sort&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt; LevelComparer&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; 
  &lt;br&gt;employeeData&lt;span style="color: blue;"&gt;.&lt;/span&gt;Sort&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt; NameComparer&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;As you may notice, we have two classes that do one operation, sorting.&amp;nbsp; You may also note that the sort method also takes an overload with a Comparer&amp;lt;T&amp;gt;.&amp;nbsp; This allows us to use lambda expressions instead and get rid of the unnecessary class noise.&amp;nbsp; Let's replace that code to use functional composition to remove some of the code.&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; employeeData = &lt;span style="color: blue;"&gt;new&lt;/span&gt; ResizeArray&amp;lt;Employee&amp;gt;&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; 
  &lt;br&gt;employeeData&lt;span style="color: blue;"&gt;.&lt;/span&gt;Sort&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;fun&lt;/span&gt; e1 e2 &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; e1&lt;span style="color: blue;"&gt;.&lt;/span&gt;Level&lt;span style="color: blue;"&gt;.&lt;/span&gt;CompareTo&lt;span style="color: blue;"&gt;(&lt;/span&gt;e2&lt;span style="color: blue;"&gt;.&lt;/span&gt;Level&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; 
  &lt;br&gt;employeeData&lt;span style="color: blue;"&gt;.&lt;/span&gt;Sort&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;fun&lt;/span&gt; e1 e2 &lt;span style="color: blue;"&gt;-&amp;gt;&lt;/span&gt; e1&lt;span style="color: blue;"&gt;.&lt;/span&gt;Name&lt;span style="color: blue;"&gt;.&lt;/span&gt;CompareTo&lt;span style="color: blue;"&gt;(&lt;/span&gt;e2&lt;span style="color: blue;"&gt;.&lt;/span&gt;Name&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;By using the application of functions, we are able to make our code more concise.&amp;nbsp; Let's turn to some more examples.&amp;nbsp; Let's talk about functional composition in terms of applying curried functions.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Currying&lt;/h2&gt;

&lt;p&gt;The topic of currying is another hot topic when understanding functional programming.&amp;nbsp; To be able to reduce the number of arguments from a given function from two down to one, we're able to make very reusable functions.&amp;nbsp; Some of these examples were transcribed from Practical Groovy and applied to F#, so they may look familiar.&amp;nbsp; Let's take a simple example of a little rules engine to calculate a book price.&amp;nbsp; Not only will we be covering currying, but also being able to compose functions together in a meaningful way.&lt;/p&gt;

&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;type&lt;/span&gt; Book = &lt;span style="color: blue;"&gt;{&lt;/span&gt; name&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; author&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; price&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: blue;"&gt;float&lt;/span&gt;; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; category&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;}&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; bk = &lt;span style="color: blue;"&gt;{&lt;/span&gt; name = &lt;span style="color: maroon;"&gt;"Expert F#"&lt;/span&gt;; author = &lt;span style="color: maroon;"&gt;"Don Syme"&lt;/span&gt;; price = &lt;span style="color: maroon;"&gt;55&lt;/span&gt;&lt;span style="color: maroon;"&gt;.99&lt;/span&gt;; category = &lt;span style="color: maroon;"&gt;"CompSci"&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;}&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;br&gt;&lt;span style="color: green;"&gt;// Constants&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; discountRate = &lt;span style="color: maroon;"&gt;0&lt;/span&gt;&lt;span style="color: maroon;"&gt;.1&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; taxRate = &lt;span style="color: maroon;"&gt;0&lt;/span&gt;&lt;span style="color: maroon;"&gt;.55&lt;/span&gt; 
  &lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;br&gt;&lt;span style="color: green;"&gt;// Calculation functions&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; calcDiscountedPrice = &lt;span style="color: green;"&gt;(*) (1. - discountRate) 
    &lt;br&gt;let calcTax = (*)&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: maroon;"&gt;1&lt;/span&gt;&lt;span style="color: blue;"&gt;.&lt;/span&gt; + taxRate&lt;span style="color: blue;"&gt;)&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; calcNetPrice = calcTax &amp;lt;&amp;lt; calcDiscountedPrice 
  &lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;br&gt;&lt;span style="color: green;"&gt;// Calculate net prices&lt;/span&gt; 
  &lt;br&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; netPrice =&amp;nbsp; calcNetPrice&lt;span style="color: blue;"&gt;(&lt;/span&gt;bk&lt;span style="color: blue;"&gt;.&lt;/span&gt;price&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: courier new;"&gt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;This above code allows me to specify my functions for calcuating discounted prices, tax and net price as simple, reusable functions.&amp;nbsp; As you can see, the calcDiscountedPrice and calcTax functions are missing their final argument, which is a float.&amp;nbsp; Each of these functions follow the signature float =&amp;gt; float.&amp;nbsp; The calcNetPrice function may look odd to you, but I'm using what is called a combinator, which allows me to compose functions f and g such as this: f(g x).&amp;nbsp; After I apply the two functions together, I can then supply the final argument, the book price, to calculate the net price.&amp;nbsp; I emphasized over and over, small reusable functions and then compose them together is the way to make functional programming powerful.&lt;/p&gt;

&lt;p&gt;Let's look at one last one example here.&amp;nbsp; In this example, we're creating a simple library application to loan books.&amp;nbsp; We want the ability to display loaned books and books available for loan.&amp;nbsp; First, you want to create