<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Mark Needham</title>
	
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<pubDate>Wed, 15 Jul 2009 21:41:19 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/MarkNeedham" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>F#: Passing command line arguments to a script</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/OHBvUU2aLlI/</link>
		<comments>http://www.markhneedham.com/blog/2009/07/16/f-passing-command-line-arguments-to-a-script/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 21:40:18 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
		
		<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1461</guid>
		<description><![CDATA[I've been doing a bit of refactoring of my FeedBurner application so that I can call it from the command line with the appropriate arguments and one of the problems I came across is working out how to pass arguments from the command line into an F# script.
With a compiled application we are able to [...]]]></description>
			<content:encoded><![CDATA[<p>I've been doing a bit of refactoring of <a href="http://www.markhneedham.com/blog/2009/07/12/f-a-day-writing-a-feedburner-graph-creator/">my FeedBurner application</a> so that I can call it from the command line with the appropriate arguments and one of the problems I came across is working out how to pass arguments from the command line into an <a href="http://www.markhneedham.com/blog/2009/06/09/f-useful-for-scripting/">F# script</a>.</p>
<p>With a compiled application we are able to make use of the '<a href="http://www.markhneedham.com/blog/2009/05/02/f-entry-point-of-an-application/">EntryPointAttribute</a>' to get access to the arguments passed in:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #6c6;">&#91;</span><span style="color: #a52a2a;">&lt;</span>EntryPointAttribute<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#93;</span>
<span style="color: #06c; font-weight: bold;">let</span> main args <span style="color: #a52a2a;">=</span>
    ShowFeedBurnerStats args
    <span style="color: #c6c;">0</span></pre></div></div>

<p>Sadly this doesn't work with a script but <a href="http://cs.hubfs.net/forums/thread/2911.aspx">it was pointed out on Hub FS</a> that we can get access to all the command line arguments by using 'Sys.argv' or 'System.Environment.GetCommandLineArgs()' which seems to be the preferred choice of the compiler.</p>
<p>The problem is that with that method you get every single argument passed to the command line and there are some that we don't care about given the way you would typically call an F# script:</p>

<div class="wp_syntax"><div class="code"><pre class="text">fsi --exec --nologo CreateFeedBurnerGraph.fsx -- &quot;scotthanselman&quot; &quot;2009-03-01&quot; &quot;2009-07-14&quot;</pre></div></div>

<p>Results in the following arguments:</p>

<div class="wp_syntax"><div class="code"><pre class="text">fsi
--exec
--nologo
CreateFeedBurnerGraph.fsx
--
scotthanselman
2009-03-01
2009-07-14</pre></div></div>

<p>We care about everything after the '&#8211;' so I wrote a little function to just gather those values:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml">    <span style="color: #06c; font-weight: bold;">let</span> GetArgs initialArgs  <span style="color: #a52a2a;">=</span>
        <span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> find args matches <span style="color: #a52a2a;">=</span>
            <span style="color: #06c; font-weight: bold;">match</span> args <span style="color: #06c; font-weight: bold;">with</span>
            <span style="color: #a52a2a;">|</span> hd<span style="color: #a52a2a;">::</span>_ <span style="color: #06c; font-weight: bold;">when</span> hd <span style="color: #a52a2a;">=</span> <span style="color: #3cb371;">&quot;--&quot;</span> <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">to_array</span> <span style="color: #6c6;">&#40;</span>matches<span style="color: #6c6;">&#41;</span>
            <span style="color: #a52a2a;">|</span> hd<span style="color: #a52a2a;">::</span>tl <span style="color: #a52a2a;">-&gt;</span> find tl <span style="color: #6c6;">&#40;</span>hd<span style="color: #a52a2a;">::</span>matches<span style="color: #6c6;">&#41;</span> 
            <span style="color: #a52a2a;">|</span> <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span> <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">empty</span>
        find <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">rev</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">to_list</span> initialArgs<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span></pre></div></div>

<p>I'm not sure this works for every possible case (if you put '&#8211;' in as an argument it wouldn't work as expected!) but it's doing the job so far.</p>
<p>An even better way of doing this which I came across while writing this is to use 'fsi.CommandLineArgs' which allows you to just get the arguments passed to the script. Even with this approach though the '&#8211;' is still counted as one of the arguments so the function above still makes sense.</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml">GetArgs <span style="color: #6c6;">&#91;</span><span style="color: #a52a2a;">|</span><span style="color: #3cb371;">&quot;--&quot;</span><span style="color: #a52a2a;">;</span> <span style="color: #3cb371;">&quot;scotthanselman&quot;</span><span style="color: #a52a2a;">;</span> <span style="color: #3cb371;">&quot;2009-03-01&quot;</span><span style="color: #a52a2a;">;</span> <span style="color: #3cb371;">&quot;2009-07-14&quot;</span><span style="color: #a52a2a;">|</span><span style="color: #6c6;">&#93;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">val</span> it <span style="color: #a52a2a;">:</span> string array <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#91;</span><span style="color: #a52a2a;">|</span><span style="color: #3cb371;">&quot;scotthanselman&quot;</span><span style="color: #a52a2a;">;</span> <span style="color: #3cb371;">&quot;2009-03-01&quot;</span><span style="color: #a52a2a;">;</span> <span style="color: #3cb371;">&quot;2009-07-14&quot;</span><span style="color: #a52a2a;">|</span><span style="color: #6c6;">&#93;</span></pre></div></div>

<p>And from the script I have the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> programArgs <span style="color: #a52a2a;">=</span> fsi<span style="color: #a52a2a;">.</span><span style="color: #060;">CommandLineArgs</span> <span style="color: #a52a2a;">|&gt;</span> GetArgs
ShowFeedBurnerStats programArgs</pre></div></div>

<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/OHBvUU2aLlI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/07/16/f-passing-command-line-arguments-to-a-script/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/07/16/f-passing-command-line-arguments-to-a-script/</feedburner:origLink></item>
		<item>
		<title>Book Club: An agile approach to a legacy system (Chris Stevenson and Andy Pols)</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/U4WdD3UYthg/</link>
		<comments>http://www.markhneedham.com/blog/2009/07/15/book-club-an-agile-approach-to-a-legacy-system-chris-stevenson-and-andy-pols/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 14:53:45 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
		
		<category><![CDATA[Book Club]]></category>

		<category><![CDATA[legacy-code]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1458</guid>
		<description><![CDATA[Our latest book club session was a discussion on a paper written by my colleague Chris Stevenson and Andy Pols titled 'An Agile Approach to a Legacy System' which I think was written in 2004. This paper was suggested by Dave Cameron.
These are some of my thoughts and our discussion of the paper:

The first thing [...]]]></description>
			<content:encoded><![CDATA[<p>Our latest book club session was a discussion on a paper written by my colleague Chris Stevenson and Andy Pols titled '<a href="http://www.skizz.biz/whitepapers/an-agile-approach-to-a-legacy-system.pdf">An Agile Approach to a Legacy System</a>' which I think was written in 2004. This paper was suggested by <a href="http://intwoplacesatonce.com/">Dave Cameron</a>.</p>
<p>These are some of my thoughts and our discussion of the paper:</p>
<ul>
<li>The first thing that was quite interesting was that the authors pointed out that <strong>if you just try and rewrite a part of a legacy system you are actually just writing legacy code yourself</strong> - we weren't sure exactly what was meant by this since for me at least the definition of legacy code is 'code which we are scared to change [because it has no tests]' but presumably the new code did have tests so it wasn't legacy in this sense. Rewriting the code doesn't really add any value to the business though as they point out since all that code might not even being used in which case it is just wasted effort. The idea of not rewriting is something that <a href="http://blog.objectmentor.com/articles/2009/01/09/the-big-redesign-in-the-sky">Uncle Bob advocates</a> and <a href="http://gojko.net/2009/06/19/eric-evans-why-do-efforts-to-replace-legacy-systems-fail/">Eric Evans also mentions the dangers of trying to replace legacy systems in his latest presentations</a>.</li>
<li>I thought it was interesting that the team d<strong>idn't make use of automatic integration since they were frequently integrating on their own machines</strong> - I'm not sure how well this would work on a big team but certainly if you have a team of people fairly experienced with CI then I can imagine that it would work really well. Dean has written <a href="http://deancornish.blogspot.com/2009/07/continuous-integration-without-ci.html">a post about serverless CI</a> which covers the idea in more details.</li>
<li>I liked the idea of <strong>putting up politics as user requirements on the story wall</strong> and then prioritising them just like any other requirement. More often the approach tends to be to try and address these problems as soon as they arise and then end up not really solving them and then getting burnt later on. This approach sounds much better. </li>
<li>Another idea that I like is that <strong>the team didn't get hung up on process</strong> - the teams I've been on which worked the best weren't slaves to process and I've often heard it suggested that having a process is just a way of dealing with the fact that there is a lack of trust in the team. Jay Fields recently wrote about the idea of having <a href="http://blog.jayfields.com/2009/07/more-trust-less-cardwall.html">more trust and less card wall</a> and Ron Jeffries has a recent post where he talks about  <a href="http://xprogramming.com/blog/needles/how-should-user-stories-be-written.htm">the light weight way that we should be making use of stories</a>.</li>
<li>Another really cool idea which I don't remember seeing before is <strong>having the whole team involved in major refactorings</strong> until the whole refactoring has been completed. Quite often with refactorings like this one pair might go off and do it and then when they checkin later there are a lot of changes and the other pairs have trouble merging and now have a lot of code which they are unfamiliar with. </li>
<li>The idea of having a <strong>self selected team</strong> sounds like an interesting idea as you then only have people on the team who actually want to be on it and want to make things happen. I'm not sure how often this would actually happen but it is a nice idea.</li>
<li>The importance of <strong>testing the system against a live database</strong> before putting it into production is emphasised and this goes beyond just using production data in a test environment. The team also made use of data verification testing to ensure that the new system and the current ones were working identically.  </li>
</ul>
<p>Although this paper is relatively short it's probably been the most interesting one that we've looked at so far. I think a lot of the ideas outlined can be used in any project and not just when dealing with legacy systems - definitely worth reading.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/U4WdD3UYthg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/07/15/book-club-an-agile-approach-to-a-legacy-system-chris-stevenson-and-andy-pols/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/07/15/book-club-an-agile-approach-to-a-legacy-system-chris-stevenson-and-andy-pols/</feedburner:origLink></item>
		<item>
		<title>Test Doubles: My current approach</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/_wS2n3gzmDM/</link>
		<comments>http://www.markhneedham.com/blog/2009/07/14/test-doubles-my-current-approach/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 03:23:52 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
		
		<category><![CDATA[Testing]]></category>

		<category><![CDATA[test-doubles]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1453</guid>
		<description><![CDATA[My colleague Sarah Taraporewalla recently wrote about her thoughts on  test doubles (to use Gerard Meszaros' language) and it got me thinking about the approach I generally take in this area. 
Stub objects
I use stubs mostly to control the output of depended on components of the system under test where we don't want to [...]]]></description>
			<content:encoded><![CDATA[<p>My colleague Sarah Taraporewalla recently wrote about <a href="http://sarahtaraporewalla.com/thoughts/testing/canned-stubbed-and-mocked-fake-objects/">her thoughts on  test doubles</a> (to use <a href="http://xunitpatterns.com/">Gerard Meszaros</a>' language) and it got me thinking about the approach I generally take in this area. </p>
<h3>Stub objects</h3>
<p>I use <a href="http://xunitpatterns.com/Test%20Stub.html">stubs</a> mostly to control the output of depended on components of the system under test where we don't want to verify those outputs.</p>
<p>Most of the time I make use of the mocking library's ability to stub out method calls on these dependencies.</p>
<p>I find that it generally seems to require less effort to do this than to create hand written stubs although chatting to <a href="http://intwoplacesatonce.com/">Dave</a> about this he pointed out that one situation where it would make more sense to use a hand written stub is when stubbing out a <a href="http://www.markhneedham.com/blog/2008/09/24/testing-with-joda-time/">clock/time provider</a>. This is because there are likely to be multiple calls to it all over the place and most of the time you probably want it to return the same value anyway.</p>
<p>I actually quite like the fact that you need to specify all the stub calls that you want to make in each test - it helps you to see when you have too many dependencies and then hopefully you can do something about that. </p>
<p>On previous projects I worked on we decided the way to get around that problem was to <a href="http://www.markhneedham.com/blog/2008/12/19/tdd-mock-expectations-in-setup/">define all the stub method calls in a setup method</a> but that seems to lead to a world of pain later on when you forget that you've stubbed a method in the setup and now want to assert an expectation on it or (to a lesser extent) write a test which doesn't actually use the stub.</p>
<h3>Fake objects</h3>
<p>I often confuse <a href="http://xunitpatterns.com/Fake%20Object.html">fakes</a> with stubs as seem to be quite similar to each other in their intent - the difference as I understand it is that with a stub we are controlling the output of a dependency whereas a fake just sits there and lets interactions happen with it. The values passed in earlier calls to the fake may be returned in later calls to it.</p>
<p>The most common use of this pattern is to replace a real database with a fake one for testing although on a recent project we were making use of a hand written fake session store to avoid having to refer to the real session in our test code. </p>
<p>We might have one call to the 'SessionFake' to store a value and then if a retrieve call is made later on we would return the value that we previously stored. </p>
<p>The approach Sarah describes for stubbing repositories seems quite similar to this as well.</p>
<h3>Mock objects</h3>
<p>I use <a href="http://xunitpatterns.com/Mock%20Object.html">mocks</a>  to replace depended on components of the system under test when I do care about the way that is is used i.e. we want to verify the behaviour of the dependencies.</p>
<p>If we see a mock object being created in a test then we should see a call to a 'verify' method later on to ensure that the expected methods are called on it.</p>
<p>I used to use these all over the place for just about every test where I wanted to control the way that a dependency acted until I realised how fragile and confusing that made the tests.</p>
<p>Now, after recently watching <a href="http://blog.jayfields.com/2009/06/developer-testing-welcome-to-beta-test.html">a presentation by Jay Fields</a>, I try to ensure that I'm only setting up one expectation per test and use of the other test double approaches for any other dependencies that needs to be taken care of in that test.</p>
<h3>Dummy objects</h3>
<p>Most of the time when I pass <a href="http://xunitpatterns.com/Dummy%20Object.html">dummy</a> values into tests they tend to be strings and I prefer to pass in a value of 'irrelevantValue' rather than just passing in a null which may lead to difficult to locate Null Pointer Exceptions further down the line if the value which we thought was just a dummy starts being used.</p>
<p>We are generally only passing in these dummy values to satisfy the requirements of the system under test which may require values to be entered even if the particular piece of functionality that we are testing doesn't make use of them.</p>
<h3>Overall</h3>
<p>I think my current approach to testing leans more towards <a href="http://martinfowler.com/articles/mocksArentStubs.html">mockist rather than classicist</a> although I think I am probably moving more towards the middle as I see the problems we can run into with over mocking.</p>
<p>With test doubles my current approach has minimising the effort required to create them as the most important aspect but I'm sure that will change given a different context. With all the test doubles I generally try and use <a href="http://www.markhneedham.com/blog/2009/01/21/c-builder-pattern-still-useful-for-test-data/">test data builders</a> where it's not overkill.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/_wS2n3gzmDM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/07/14/test-doubles-my-current-approach/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/07/14/test-doubles-my-current-approach/</feedburner:origLink></item>
		<item>
		<title>F#: A day writing a Feedburner graph creator</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/WMU5Fhdidtw/</link>
		<comments>http://www.markhneedham.com/blog/2009/07/12/f-a-day-writing-a-feedburner-graph-creator/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 07:14:13 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
		
		<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1450</guid>
		<description><![CDATA[I've spent a bit of the day writing a little application to take the xml from my Feedburner RSS feed and create a graph showing the daily &#038; weekly average subscribers.
What did I learn?

I decided that I wanted to parameterise the feedburner url so that I would be able to run the code for different [...]]]></description>
			<content:encoded><![CDATA[<p>I've spent a bit of the day writing a little application to take the xml from my <a href="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=markneedham&#038;dates=2009-03-01,2009-03-27">Feedburner RSS feed</a> and create a graph showing the daily &#038; weekly average subscribers.</p>
<h4>What did I learn?</h4>
<ul>
<li>I decided that I wanted to parameterise the feedburner url so that I would be able to run the code for different time periods and against different feeds. In C# we'd probably make use of 'string.Format()' which has an equivalent in F# called 'sprintf'
<p>My initial thought was that I would be able to do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> ShowFeedBurnerStats feed <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> statsUrl <span style="color: #a52a2a;">=</span> <span style="color: #3cb371;">&quot;https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=%s&amp;dates=2009-01-01,2009-07-11&quot;</span>
    sprintf statsUrl feed <span style="color: #a52a2a;">|&gt;</span> GetXml
    <span style="color: #a52a2a;">//</span> more code</pre></div></div>

<p>Which actually results in the following compilation error:</p>

<div class="wp_syntax"><div class="code"><pre class="text">The type 'string' is not compatible with the type 'Printf.StringFormat</pre></div></div>

<p>After a bit of searching I found <a href="http://cs.hubfs.net/forums/thread/6341.aspx">a post by Robert Pickering where he explains that the format string needs to be next to the sprintf function to work as expected</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> ShowFeedBurnerStats feed <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> statsUrl <span style="color: #a52a2a;">=</span> sprintf <span style="color: #3cb371;">&quot;https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=%s&amp;dates=2009-01-01,2009-07-11&quot;</span>
    statsUrl feed <span style="color: #a52a2a;">|&gt;</span> GetXml
    <span style="color: #a52a2a;">//</span> more code</pre></div></div>

<p>'statsUrl' therefore becomes a function taking in a 'string' and returning a 'string'.
</li>
<li>I'm still trying to work out the best way to decompose the code I write into functions which make sense in terms of the domain I'm working in.
<p>I often found myself <strong>splitting up a function along the boundary of where any I/O interaction was happening</strong> so that I could execute the I/O function and save the data before using it in another function which I would execute a lot more frequently (using F# interactive) while I was tweaking it. </li>
<li>I still haven't come up with a completely satisfactory approach to coding these little applications - right now I'm finding that the feedback cycle is significantly quicker if I just write functions and then run them in F# interactive and then tweak anything which isn't working as expected.
<p>I didn't write any unit tests while coding this although I did find myself writing shorter functions than I originally did when writing my little twitter application. The problem of not writing the tests is that I lose the protection against regression that I would otherwise get.</li>
<li>I still have a bit of a <a href="http://www.markhneedham.com/blog/2009/06/02/f-tuples-dont-seem-to-express-intent-well/">love hate relationship with tuples</a> - I found myself making use of them early on when I was focused on getting the code to work and I could still understand the code easily.
<p>Originally I was only storing 'date' and 'circulation' in the tuple but once I added a third value to the tuple ('weeklyAverage') it became too confusing for me to understand so I decided to introduce the 'FeedBurnerStats' type to simplify things for myself. </li>
<li>I ended up writing a function called 'Join' which is quite similar to 'Seq.zip' because I wanted to join two sequences together but only join items which had the same date (the 'string' value in the tuple).
<p>Therefore, if I had some data like this:</p>
<p>'dailyStats'</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #3cb371;">&quot;2009-01,07&quot;</span>, <span style="color: #c6c;">200</span>
<span style="color: #3cb371;">&quot;2009-01,08&quot;</span>, <span style="color: #c6c;">222</span></pre></div></div>

<p>'weeklyAverages'</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #3cb371;">&quot;2009-01,07&quot;</span>, <span style="color: #c6c;">300</span>
<span style="color: #3cb371;">&quot;2009-01,08&quot;</span>, <span style="color: #c6c;">322</span></pre></div></div>

<p>I wanted the join of the two sequences to look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #3cb371;">&quot;2009-01,07&quot;</span>, <span style="color: #c6c;">200</span>, <span style="color: #c6c;">300</span>
<span style="color: #3cb371;">&quot;2009-01,08&quot;</span>, <span style="color: #c6c;">222</span>, <span style="color: #c6c;">322</span></pre></div></div>

<p>Which wasn't working as expected when I used 'Seq.zip' - the items that were getting matched together seemed to be quite random to me.</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> Join <span style="color: #6c6;">&#40;</span>dailyStats<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>decimal<span style="color: #a52a2a;">*</span>string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>weeklyAverages<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>decimal<span style="color: #a52a2a;">*</span>string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    dailyStats <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> d <span style="color: #a52a2a;">-&gt;</span> <span style="color: #6c6;">&#123;</span> Date <span style="color: #a52a2a;">=</span> d <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">snd</span><span style="color: #a52a2a;">;</span> 
                                      Circulation <span style="color: #a52a2a;">=</span> d <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">fst</span><span style="color: #a52a2a;">;</span>
                                      WeeklyAverage <span style="color: #a52a2a;">=</span> weeklyAverages <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">find</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> w <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">snd</span> d <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">snd</span> w<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">fst</span><span style="color: #6c6;">&#125;</span><span style="color: #6c6;">&#41;</span></pre></div></div>

</ul>
<p>I've included the code is at the end of the post - there are some areas where I don't really like the way I've solved a problem but I'm not sure of a better way at the moment.</p>
<p>In particular:</p>
<ul>
<li>I wanted to make use of 'Seq.windowed' to find the rolling weekly average but I needed it to go back 7 days rather than forward 7 days which meant I needed to reverse the sequence. Right now I've done this by converting it to a list and using 'List.rev' to do so but this seems like a fairly inefficient way of doing this.
<p>The alternative seemed to be to write a function to change the order of the items in the sequence but again this doesn't seem like a great approach.</li>
<li><strong>What do you do with functions which are only used by one other areas of the code?</strong> For example 'ConvertToCommaSeparatedString' is only used by 'CreateGoogleGraphUri' so I defined it inside that function - I could then pull it to a function in its own right if other areas of the code need it. I did this to reduce the clutter of functions hanging around but it then makes 'CreateGoogleGraphUri' more difficult to read. </li>
</ul>
<p>I decided to run it against some blogs I follow to see what the graphs, created using <a href="http://chart.apis.google.com/">Google's Charts API</a>, would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml">ShowFeedBurnerStats <span style="color: #3cb371;">&quot;scotthanselman&quot;</span> <span style="color: #3cb371;">&quot;2009-03-01&quot;</span> <span style="color: #3cb371;">&quot;2009-07-11&quot;</span><span style="color: #a52a2a;">;;</span>
ShowFeedBurnerStats <span style="color: #3cb371;">&quot;youdthinkwithallmy&quot;</span> <span style="color: #3cb371;">&quot;2009-03-01&quot;</span> <span style="color: #3cb371;">&quot;2009-07-11&quot;</span><span style="color: #a52a2a;">;;</span>
ShowFeedBurnerStats <span style="color: #3cb371;">&quot;codinghorror&quot;</span> <span style="color: #3cb371;">&quot;2009-03-01&quot;</span> <span style="color: #3cb371;">&quot;2009-07-11&quot;</span><span style="color: #a52a2a;">;;</span></pre></div></div>

<p><img src="http://www.markhneedham.com/blog/wp-content/uploads/2009/07/hanselman.png" alt="hanselman.png" border="0" width="600" height="240" /></p>
<p><img src="http://www.markhneedham.com/blog/wp-content/uploads/2009/07/jasonyip.png" alt="jasonyip.png" border="0" width="600" height="240" /></p>
<p><img src="http://www.markhneedham.com/blog/wp-content/uploads/2009/07/codinghorror.png" alt="codinghorror.png" border="0" width="600" height="240" /></p>
<p>Interestingly you can actually see the points where feedburner for some reason counted a particular days circulation as being 0. </p>
<p>And here's the code:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">open</span> System<span style="color: #a52a2a;">.</span><span style="color: #060;">IO</span>
<span style="color: #06c; font-weight: bold;">open</span> System<span style="color: #a52a2a;">.</span><span style="color: #060;">Net</span>
<span style="color: #06c; font-weight: bold;">open</span> Microsoft<span style="color: #a52a2a;">.</span><span style="color: #060;">FSharp</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Control</span>
<span style="color: #06c; font-weight: bold;">open</span> System<span style="color: #a52a2a;">.</span><span style="color: #060;">Xml</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Linq</span>
<span style="color: #06c; font-weight: bold;">open</span> System
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> downloadUrl <span style="color: #6c6;">&#40;</span>url<span style="color: #a52a2a;">:</span>string<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span> async<span style="color: #6c6;">&#123;</span>
    <span style="color: #06c; font-weight: bold;">let</span> request <span style="color: #a52a2a;">=</span>  HttpWebRequest<span style="color: #a52a2a;">.</span><span style="color: #060;">Create</span><span style="color: #6c6;">&#40;</span>url<span style="color: #6c6;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">let</span><span style="color: #a52a2a;">!</span> response <span style="color: #a52a2a;">=</span> request<span style="color: #a52a2a;">.</span><span style="color: #060;">AsyncGetResponse</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">let</span> stream <span style="color: #a52a2a;">=</span> response<span style="color: #a52a2a;">.</span><span style="color: #060;">GetResponseStream</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
    use reader <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">new</span> StreamReader<span style="color: #6c6;">&#40;</span>stream<span style="color: #6c6;">&#41;</span>
    return<span style="color: #a52a2a;">!</span> reader<span style="color: #a52a2a;">.</span><span style="color: #060;">AsyncReadToEnd</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#125;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> xName value <span style="color: #a52a2a;">=</span> XName<span style="color: #a52a2a;">.</span><span style="color: #060;">Get</span> value
<span style="color: #06c; font-weight: bold;">let</span> GetDescendants element <span style="color: #6c6;">&#40;</span>xDocument<span style="color: #a52a2a;">:</span>XDocument<span style="color: #6c6;">&#41;</span>  <span style="color: #a52a2a;">=</span> xDocument<span style="color: #a52a2a;">.</span><span style="color: #060;">Descendants</span><span style="color: #6c6;">&#40;</span>xName element<span style="color: #6c6;">&#41;</span>
<span style="color: #06c; font-weight: bold;">let</span> GetAttribute element <span style="color: #6c6;">&#40;</span>xElement<span style="color: #a52a2a;">:</span>XElement<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span> xElement<span style="color: #a52a2a;">.</span><span style="color: #060;">Attribute</span><span style="color: #6c6;">&#40;</span>xName element<span style="color: #6c6;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> GetXml <span style="color: #a52a2a;">=</span> downloadUrl <span style="color: #a52a2a;">&gt;&gt;</span> Async<span style="color: #a52a2a;">.</span><span style="color: #060;">Run</span> <span style="color: #a52a2a;">&gt;&gt;</span> XDocument<span style="color: #a52a2a;">.</span><span style="color: #060;">Parse</span> 
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> GetDateAndCirculation <span style="color: #6c6;">&#40;</span>document<span style="color: #a52a2a;">:</span>XDocument<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span> 
    document <span style="color: #a52a2a;">|&gt;</span> 
    GetDescendants <span style="color: #3cb371;">&quot;entry&quot;</span>  <span style="color: #a52a2a;">|&gt;</span> 
    Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> element <span style="color: #a52a2a;">-&gt;</span> GetAttribute <span style="color: #3cb371;">&quot;circulation&quot;</span> element, GetAttribute <span style="color: #3cb371;">&quot;date&quot;</span> element<span style="color: #6c6;">&#41;</span>  <span style="color: #a52a2a;">|&gt;</span> 
    Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> attribute <span style="color: #a52a2a;">-&gt;</span> Decimal<span style="color: #a52a2a;">.</span><span style="color: #060;">Parse</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fst</span> attribute<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span><span style="color: #6c6;">&#41;</span>, <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">snd</span> attribute<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span><span style="color: #6c6;">&#41;</span> 
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> CalculateAverage days <span style="color: #6c6;">&#40;</span>feedStats<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>decimal <span style="color: #a52a2a;">*</span> string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> ReverseSequence <span style="color: #6c6;">&#40;</span>sequence<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>_<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span> sequence <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">to_list</span> <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">rev</span> <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">to_seq</span>
    feedStats <span style="color: #a52a2a;">|&gt;</span> 
    ReverseSequence <span style="color: #a52a2a;">|&gt;</span>
    Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">windowed</span> days <span style="color: #a52a2a;">|&gt;</span>
    Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> x <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> y <span style="color: #a52a2a;">-&gt;</span> y <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">fst</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">average</span>, x<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span><span style="color: #c6c;">0</span><span style="color: #6c6;">&#93;</span> <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">snd</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span>
    ReverseSequence    
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> CalculateWeeklyAverage <span style="color: #6c6;">&#40;</span>feedStats<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>decimal <span style="color: #a52a2a;">*</span> string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span> CalculateAverage <span style="color: #c6c;">7</span> feedStats
&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> FeedBurnerStats <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span> Date <span style="color: #a52a2a;">:</span> string<span style="color: #a52a2a;">;</span> Circulation<span style="color: #a52a2a;">:</span> decimal<span style="color: #a52a2a;">;</span> WeeklyAverage<span style="color: #a52a2a;">:</span> decimal <span style="color: #6c6;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> Join <span style="color: #6c6;">&#40;</span>dailyStats<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>decimal<span style="color: #a52a2a;">*</span>string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>weeklyAverages<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>decimal<span style="color: #a52a2a;">*</span>string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    dailyStats <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> d <span style="color: #a52a2a;">-&gt;</span> <span style="color: #6c6;">&#123;</span> Date <span style="color: #a52a2a;">=</span> d <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">snd</span><span style="color: #a52a2a;">;</span> 
                                      Circulation <span style="color: #a52a2a;">=</span> d <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">fst</span><span style="color: #a52a2a;">;</span>
                                      WeeklyAverage <span style="color: #a52a2a;">=</span> weeklyAverages <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">find</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> w <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">snd</span> d <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">snd</span> w<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> <span style="color: #06c; font-weight: bold;">fst</span><span style="color: #6c6;">&#125;</span><span style="color: #6c6;">&#41;</span>        
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> GetFeedBurnerStats feed startDate endDate <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> statsUrl <span style="color: #a52a2a;">=</span> sprintf <span style="color: #3cb371;">&quot;https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=%s&amp;dates=%s,%s&quot;</span>
    <span style="color: #06c; font-weight: bold;">let</span> allStats <span style="color: #a52a2a;">=</span> GetDateAndCirculation <span style="color: #6c6;">&#40;</span>statsUrl feed startDate endDate <span style="color: #a52a2a;">|&gt;</span> GetXml<span style="color: #6c6;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">let</span> weeklyAverages <span style="color: #a52a2a;">=</span> allStats <span style="color: #a52a2a;">|&gt;</span> CalculateWeeklyAverage
    <span style="color: #06c; font-weight: bold;">let</span> dailyStats <span style="color: #a52a2a;">=</span> allStats <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">filter</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> weeklyAverages <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">exists</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> y <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">snd</span> y <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">snd</span> x<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> 
    Join dailyStats weeklyAverages   
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> CreateGoogleGraphUri feed <span style="color: #6c6;">&#40;</span>stats<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>FeedBurnerStats<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> ConvertToCommaSeparatedString <span style="color: #6c6;">&#40;</span>value<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
        <span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> convert <span style="color: #6c6;">&#40;</span>innerVal<span style="color: #a52a2a;">:</span>List<span style="color: #a52a2a;">&lt;</span>string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> acc <span style="color: #a52a2a;">=</span>
            <span style="color: #06c; font-weight: bold;">match</span> innerVal <span style="color: #06c; font-weight: bold;">with</span>
                <span style="color: #a52a2a;">|</span> <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span> <span style="color: #a52a2a;">-&gt;</span> acc
                <span style="color: #a52a2a;">|</span> hd<span style="color: #a52a2a;">::</span><span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span> <span style="color: #a52a2a;">-&gt;</span> convert <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span> <span style="color: #6c6;">&#40;</span>acc <span style="color: #a52a2a;">+</span> hd<span style="color: #6c6;">&#41;</span>
                <span style="color: #a52a2a;">|</span> hd<span style="color: #a52a2a;">::</span>tl <span style="color: #a52a2a;">-&gt;</span> convert tl <span style="color: #6c6;">&#40;</span>acc <span style="color: #a52a2a;">+</span> hd <span style="color: #a52a2a;">+</span> <span style="color: #3cb371;">&quot;,&quot;</span><span style="color: #6c6;">&#41;</span>          
        convert <span style="color: #6c6;">&#40;</span>Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">to_list</span> value<span style="color: #6c6;">&#41;</span> <span style="color: #3cb371;">&quot;&quot;</span>  
&nbsp;
    <span style="color: #06c; font-weight: bold;">let</span> graphUrl <span style="color: #a52a2a;">=</span> sprintf <span style="color: #3cb371;">&quot;http://chart.apis.google.com/chart?cht=lc&amp;chtt=%s&amp;&amp;chco=000000,FF0000&amp;chdl=WeeklyAverage|Daily&amp;chs=600x240&amp;chds=%s,%s&amp;chd=t:%s|%s&quot;</span>
    <span style="color: #06c; font-weight: bold;">let</span> weeklyAverages <span style="color: #a52a2a;">=</span> stats <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> f <span style="color: #a52a2a;">-&gt;</span> f<span style="color: #a52a2a;">.</span><span style="color: #060;">WeeklyAverage</span><span style="color: #a52a2a;">.</span><span style="color: #060;">ToString</span><span style="color: #6c6;">&#40;</span><span style="color: #3cb371;">&quot;f0&quot;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> ConvertToCommaSeparatedString 
    <span style="color: #06c; font-weight: bold;">let</span> circulation <span style="color: #a52a2a;">=</span> stats <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> f <span style="color: #a52a2a;">-&gt;</span> f<span style="color: #a52a2a;">.</span><span style="color: #060;">Circulation</span><span style="color: #a52a2a;">.</span><span style="color: #060;">ToString</span><span style="color: #6c6;">&#40;</span><span style="color: #3cb371;">&quot;f0&quot;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> ConvertToCommaSeparatedString 
&nbsp;
    <span style="color: #06c; font-weight: bold;">let</span> maximum <span style="color: #a52a2a;">=</span> stats <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> f <span style="color: #a52a2a;">-&gt;</span> f<span style="color: #a52a2a;">.</span><span style="color: #060;">Circulation</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #06c; font-weight: bold;">max</span>
    <span style="color: #06c; font-weight: bold;">let</span> minimum <span style="color: #a52a2a;">=</span> stats <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> f <span style="color: #a52a2a;">-&gt;</span> f<span style="color: #a52a2a;">.</span><span style="color: #060;">Circulation</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #06c; font-weight: bold;">min</span>
&nbsp;
    <span style="color: #06c; font-weight: bold;">new</span> System<span style="color: #a52a2a;">.</span><span style="color: #060;">Uri</span><span style="color: #6c6;">&#40;</span>graphUrl feed <span style="color: #6c6;">&#40;</span>minimum<span style="color: #a52a2a;">.</span><span style="color: #060;">ToString</span><span style="color: #6c6;">&#40;</span><span style="color: #3cb371;">&quot;f0&quot;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>maximum<span style="color: #a52a2a;">.</span><span style="color: #060;">ToString</span><span style="color: #6c6;">&#40;</span><span style="color: #3cb371;">&quot;f0&quot;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> weeklyAverages circulation<span style="color: #6c6;">&#41;</span>      
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> ShowFeedBurnerStats feed startDate endDate <span style="color: #a52a2a;">=</span> CreateGoogleGraphUri feed <span style="color: #6c6;">&#40;</span>GetFeedBurnerStats feed startDate endDate<span style="color: #6c6;">&#41;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/WMU5Fhdidtw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/07/12/f-a-day-writing-a-feedburner-graph-creator/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/07/12/f-a-day-writing-a-feedburner-graph-creator/</feedburner:origLink></item>
		<item>
		<title>F#: Wrapping .NET library calls</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/gN0m0hqQ65E/</link>
		<comments>http://www.markhneedham.com/blog/2009/07/12/f-wrapping-net-library-calls/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 02:11:46 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
		
		<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1444</guid>
		<description><![CDATA[I've been spending a bit of time writing some code to parse the xml of my Feedburner RSS feed and create a graph to show both the daily and weekly average subscribers which you can't currently get from the Feedburner dashboard.
One thing which I found while doing this is that calls to the .NET base [...]]]></description>
			<content:encoded><![CDATA[<p>I've been spending a bit of time writing some code to parse the xml of my Feedburner RSS feed and create a graph to show both the daily and weekly average subscribers which you can't currently get from the Feedburner dashboard.</p>
<p>One thing which I found while doing this is that calls to the .NET base class library don't seem to fit in that well with the way that you would typically compose functions together in F#.</p>
<p>For example one of the first things I wanted to do was print the date and the circulation count to the console which I originally did like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">open</span> System<span style="color: #a52a2a;">.</span><span style="color: #060;">IO</span>
<span style="color: #06c; font-weight: bold;">open</span> System<span style="color: #a52a2a;">.</span><span style="color: #060;">Net</span>
<span style="color: #06c; font-weight: bold;">open</span> Microsoft<span style="color: #a52a2a;">.</span><span style="color: #060;">FSharp</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Control</span>
<span style="color: #06c; font-weight: bold;">open</span> System<span style="color: #a52a2a;">.</span><span style="color: #060;">Xml</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Linq</span>
<span style="color: #06c; font-weight: bold;">open</span> System
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> xName value <span style="color: #a52a2a;">=</span> XName<span style="color: #a52a2a;">.</span><span style="color: #060;">Get</span> value
&nbsp;
<span style="color: #a52a2a;">//</span> GetXml is a <span style="color: #06c; font-weight: bold;">function</span> <span style="color: #06c; font-weight: bold;">of</span> <span style="color: #06c; font-weight: bold;">type</span> string <span style="color: #a52a2a;">-&gt;</span> string
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> GetFeedBurnerStats url <span style="color: #a52a2a;">=</span> 
    <span style="color: #06c; font-weight: bold;">let</span> feedBurnerXml <span style="color: #a52a2a;">=</span> GetXml url <span style="color: #a52a2a;">|&gt;</span> XDocument<span style="color: #a52a2a;">.</span><span style="color: #060;">Parse</span>
    feedBurnerXml<span style="color: #a52a2a;">.</span><span style="color: #060;">Descendants</span><span style="color: #6c6;">&#40;</span>xName <span style="color: #3cb371;">&quot;entry&quot;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> 
    Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> x<span style="color: #a52a2a;">.</span><span style="color: #060;">Attribute</span><span style="color: #6c6;">&#40;</span>xName <span style="color: #3cb371;">&quot;circulation&quot;</span><span style="color: #6c6;">&#41;</span>, x<span style="color: #a52a2a;">.</span><span style="color: #060;">Attribute</span><span style="color: #6c6;">&#40;</span>xName <span style="color: #3cb371;">&quot;date&quot;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span>
    Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">iter</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> printfn <span style="color: #3cb371;">&quot;%s %s&quot;</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fst</span> x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">snd</span> x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span><span style="color: #6c6;">&#41;</span></pre></div></div>

<p>It's quite annoying that we need to store the XDocument as a value before being able to call one of the methods on it to get the data that we want.</p>
<p>I realised that if I created a function which took in the element whose descendants I wanted to find and the XDocument I could then call the 'XDocument.Descendants()' method inside that function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> xName value <span style="color: #a52a2a;">=</span> XName<span style="color: #a52a2a;">.</span><span style="color: #060;">Get</span> value
<span style="color: #06c; font-weight: bold;">let</span> GetDescendants element <span style="color: #6c6;">&#40;</span>xDocument<span style="color: #a52a2a;">:</span>XDocument<span style="color: #6c6;">&#41;</span>  <span style="color: #a52a2a;">=</span> xDocument<span style="color: #a52a2a;">.</span><span style="color: #060;">Descendants</span><span style="color: #6c6;">&#40;</span>xName element<span style="color: #6c6;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> GetFeedBurnerStats <span style="color: #a52a2a;">=</span> 
    GetXml <span style="color: #a52a2a;">&gt;&gt;</span> 
    XDocument<span style="color: #a52a2a;">.</span><span style="color: #060;">Parse</span> <span style="color: #a52a2a;">&gt;&gt;</span> 
    GetDescendants <span style="color: #3cb371;">&quot;entry&quot;</span> <span style="color: #a52a2a;">&gt;&gt;</span>
    Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> x<span style="color: #a52a2a;">.</span><span style="color: #060;">Attribute</span><span style="color: #6c6;">&#40;</span>xName <span style="color: #3cb371;">&quot;circulation&quot;</span><span style="color: #6c6;">&#41;</span>, x<span style="color: #a52a2a;">.</span><span style="color: #060;">Attribute</span><span style="color: #6c6;">&#40;</span>xName <span style="color: #3cb371;">&quot;date&quot;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">&gt;&gt;</span>
    Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">iter</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> printfn <span style="color: #3cb371;">&quot;%s %s&quot;</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fst</span> x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">snd</span> x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span><span style="color: #6c6;">&#41;</span></pre></td></tr></table></div>

<p>Since we no longer need to store the intermediate step of creating the XDocument we can now just chain together the functions using the <a href="http://www.markhneedham.com/blog/2009/01/12/f-partial-function-application-with-the-function-composition-operator/">functional composition operator</a> instead of the <a href="http://www.markhneedham.com/blog/2009/01/06/f-forward-operator/">forward operator</a>.</p>
<p>We can also do this with the calls to 'Attribute' in the 'Seq.map' function on line 9 which helps simplify the code around there.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> xName value <span style="color: #a52a2a;">=</span> XName<span style="color: #a52a2a;">.</span><span style="color: #060;">Get</span> value
<span style="color: #06c; font-weight: bold;">let</span> GetDescendants element <span style="color: #6c6;">&#40;</span>xDocument<span style="color: #a52a2a;">:</span>XDocument<span style="color: #6c6;">&#41;</span>  <span style="color: #a52a2a;">=</span> xDocument<span style="color: #a52a2a;">.</span><span style="color: #060;">Descendants</span><span style="color: #6c6;">&#40;</span>xName element<span style="color: #6c6;">&#41;</span>
<span style="color: #06c; font-weight: bold;">let</span> GetAttribute element <span style="color: #6c6;">&#40;</span>xElement<span style="color: #a52a2a;">:</span>XElement<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span> xElement<span style="color: #a52a2a;">.</span><span style="color: #060;">Attribute</span><span style="color: #6c6;">&#40;</span>xName element<span style="color: #6c6;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> GetFeedBurnerStats <span style="color: #a52a2a;">=</span> 
    GetXml <span style="color: #a52a2a;">&gt;&gt;</span> 
    XDocument<span style="color: #a52a2a;">.</span><span style="color: #060;">Parse</span> <span style="color: #a52a2a;">&gt;&gt;</span> 
    GetDescendants <span style="color: #3cb371;">&quot;entry&quot;</span> <span style="color: #a52a2a;">&gt;&gt;</span>
    Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> GetAttribute <span style="color: #3cb371;">&quot;circulation&quot;</span> x, GetAttribute <span style="color: #3cb371;">&quot;date&quot;</span> x<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">&gt;&gt;</span>
    Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">iter</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> printfn <span style="color: #3cb371;">&quot;%s %s&quot;</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fst</span> x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">snd</span> x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span><span style="color: #6c6;">&#41;</span></pre></td></tr></table></div>

<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/gN0m0hqQ65E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/07/12/f-wrapping-net-library-calls/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/07/12/f-wrapping-net-library-calls/</feedburner:origLink></item>
		<item>
		<title>Continuous Integration: Community College Discussion</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/atLjaXJaZ5I/</link>
		<comments>http://www.markhneedham.com/blog/2009/07/11/continuous-integration-community-college-discussion/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 04:13:48 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
		
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1441</guid>
		<description><![CDATA[We ran a session on Continuous Integration at the most recent Community College in the ThoughtWorks Sydney office.
It was roughly based around a CI Maturity Model which I recently came across although the intention was to find out what other teams were doing CI wise.
I became a bit more aware of how little I know [...]]]></description>
			<content:encoded><![CDATA[<p>We ran a session on Continuous Integration at the most recent Community College in the ThoughtWorks Sydney office.</p>
<p>It was roughly based around a <a href="http://www.anthillpro.com/blogs/anthillpro-blog/2009/05/05/1241542860000.html">CI Maturity Model</a> which I recently came across although the intention was to find out what other teams were doing CI wise.</p>
<p>I became a bit more aware of how little I know about CI after listening to a <a href="http://www.se-radio.net/podcast/2009-04/episode-133-continuous-integration-chris-read">Software Engineering Radio interview with my colleague Chris Read</a> so I was keen to see how other teams are approaching this problem.</p>
<p>These were some of the most intersting parts of our discussion.</p>
<ul>
<li><strong>Handling external dependencies</strong> - the idea here is that you don't have control over these end points and their reliability and it is very frustrating for the build to fail due to a problem with a dependency rather than a problem in your application.
<p>Several teams were testing integration of their application against these in a separate build something which we are also doing with a set of NUnit tests which run every 10 minutes directly against our service level end point.</p>
<p>On my project we are also making use of an <a href="http://www.markhneedham.com/blog/2009/06/21/seams-some-thoughts/">impersonator</a> of our main integration point early in our build pipeline and then testing end to end integration at a later stage in the pipeline. We are not completely protected at the moment but we're getting there. </p>
<p>The trade off here is finding the balance between making the impersonator way too complicated such that people can't understand it and we can't be sure that it actually works. It seems that if we are doing anything beyond a state machine with regards to handling requests &#038; responses and are putting actual business logic inside the impersonator then we've probably gone too far.</li>
<li>The importance of creating a single artifact if using a build pipeline and then running every unit, integration and functional test against that same artifact was pointed out.
<p>On my project we are recompiling our code at each stage of the build pipeline (from the same revision of the code in Subversion) as we haven't yet found a good way of putting the artifact somewhere where each build stage can pick it up from - some colleagues are making use of Ivy &#038; Maven repositories to do this on Java projects.</p>
<p>We also spoke about keeping the configuration of the application separate to the artifact so that we can easily deploy the application into different environments.
</li>
<li>We discussed the fact that <strong>keeping the build time very low by paying constant attention to it</strong> is one of the most important things to do to ensure quick feedback for the development team yet it is often allowed to slip to the point where we end up with build times exceeding an hour.
<p>Most of the teams had a pre commit build time of less than 10 minutes although only a couple had a build time of less than 10 minutes on their CI server.</p>
<p>I've never seen it done but the idea of failing the build if it exceeded an agreed time was suggested forcing the team to address the problem rather than letting it gradually get slower and slower until the quick feedback cycle it originally provided has gone.</li>
<li>The idea of <strong>running performance tests in the build</strong> was something which I found quite interesting - several colleagues pointed out that the point here is not to test the performance of the application when it's live but more to see the trend in how well our application is performing so that if it suddently dips we can address this straight away rather than waiting until later on to do this.
<p>We also spoke about including security tests - such as trying to inject evil SQL into every field - in the build although it was also suggested that these should be there anyway as part of the functional tests that we include. I think testing for <a href="http://www.markhneedham.com/blog/2009/02/12/aspnet-mvc-preventing-xss-attacks/">XSS attacks</a> might also fall into this area.</li>
<li>Another interesting area of discussion was around <strong>whether it's acceptable for the build to break due to a change that we made</strong>. If we decide that it isn't then the only reason that it should fail is due to a problem with a depedency problems. To get to this stage it would need to be possible to run the entire build before checking in probably by having a grid of machines setup which we can assign local builds to run on. Dean Cornish has written an intersting post where he covers the idea of having <a href="http://deancornish.blogspot.com/2009/07/continuous-integration-without-ci.html">continuous integration without a CI server</a>.
<p>On all the projects I've worked on we haven't done this so in these situations I subscribe more to <a href="http://blog.runcoderun.com/post/72393206/its-okay-to-break-the-build">Stuart Halloway's thinking that it is actually ok to break the build in some circumstances</a>. We are leaning on the CI server a bit to give us feedback but I don't think it's too bad to do this if it allows us to check in more frequently.</li>
<li>We also spoke about the importance of getting the <strong>whole team to agree to a collection of CI practices</strong> if CI is really going to work on a project. These might include behaviours such as not checking in when the build's red, looking at why the build has broken if you're checkin broke it, rolling back your changes if you can't fix the problem quickly and so on.</li>
</ul>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/atLjaXJaZ5I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/07/11/continuous-integration-community-college-discussion/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/07/11/continuous-integration-community-college-discussion/</feedburner:origLink></item>
		<item>
		<title>F#: Downloading a file from behind a proxy</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/UHhoXphe6Nc/</link>
		<comments>http://www.markhneedham.com/blog/2009/07/11/f-downloading-a-file-from-behind-a-proxy/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 17:20:25 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
		
		<category><![CDATA[F#]]></category>

		<category><![CDATA[proxy]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1438</guid>
		<description><![CDATA[I've been continuing working on a little script to parse Cruise build data and the latest task was to work out how to download my Google Graph API created image onto the local disk.
I'm using the WebClient class to do this and the  code looks like this:

let DownloadGraph &#40;fileLocation:string&#41; &#40;uri:System.Uri&#41; = async &#123;
  [...]]]></description>
			<content:encoded><![CDATA[<p>I've been continuing working on a little script to <a href="http://www.markhneedham.com/blog/2009/07/08/f-parsing-cruise-build-data/">parse Cruise build data</a> and the latest task was to work out how to download my Google Graph API created image onto the local disk.</p>
<p>I'm using the <a href="http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx">WebClient</a> class to do this and the  code looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> DownloadGraph <span style="color: #6c6;">&#40;</span>fileLocation<span style="color: #a52a2a;">:</span>string<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>uri<span style="color: #a52a2a;">:</span>System<span style="color: #a52a2a;">.</span><span style="color: #060;">Uri</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span> async <span style="color: #6c6;">&#123;</span>
    <span style="color: #06c; font-weight: bold;">let</span> webClient <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">new</span> WebClient<span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
    webClient<span style="color: #a52a2a;">.</span><span style="color: #060;">DownloadFileAsync</span><span style="color: #6c6;">&#40;</span>uri, fileLocation<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#125;</span></pre></div></div>

<p>Sadly this doesn't work when I run it from the client site where I have access to the build metrics as there is a corporate proxy sitting in the way.</p>
<p>I tried Googling how to do this but all the ways that I tried kept resulting in the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="text">407 proxy authentication required</pre></div></div>

<p>Even though I was entering a user name and password!</p>
<p>I didn't succeed until my colleague showed me a way of getting past the proxy in C# which I could quite easily use in my code:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> DownloadGraph <span style="color: #6c6;">&#40;</span>fileLocation<span style="color: #a52a2a;">:</span>string<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>uri<span style="color: #a52a2a;">:</span>System<span style="color: #a52a2a;">.</span><span style="color: #060;">Uri</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span> async <span style="color: #6c6;">&#123;</span>
    <span style="color: #06c; font-weight: bold;">let</span> webClient <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">new</span> WebClient<span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
    webClient<span style="color: #a52a2a;">.</span><span style="color: #060;">Proxy</span> <span style="color: #a52a2a;">&lt;-</span> <span style="color: #06c; font-weight: bold;">new</span> WebProxy<span style="color: #6c6;">&#40;</span><span style="color: #3cb371;">&quot;proxyName:port&quot;</span>, <span style="color: #06c; font-weight: bold;">true</span>, null, <span style="color: #06c; font-weight: bold;">new</span> NetworkCredential<span style="color: #6c6;">&#40;</span><span style="color: #3cb371;">&quot;userName&quot;</span>, <span style="color: #3cb371;">&quot;password&quot;</span>, <span style="color: #3cb371;">&quot;corporateDomain&quot;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
    webClient<span style="color: #a52a2a;">.</span><span style="color: #060;">DownloadFileAsync</span><span style="color: #6c6;">&#40;</span>uri, fileLocation<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#125;</span></pre></div></div>

<p>One thing I was doing wrong was putting a 'http' at the start of the proxyName which I think in my case was wrong as I later learn that the proxy isn't a HTTP one.</p>
<p>I'm also making use of <a href="http://www.infoq.com/articles/pickering-fsharp-async">asynchronous workflows</a> in this example so that the actual downloading of the files will be done away from the main thread - this also gives me the option to download multiple files asynchronously if I want to.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/UHhoXphe6Nc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/07/11/f-downloading-a-file-from-behind-a-proxy/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/07/11/f-downloading-a-file-from-behind-a-proxy/</feedburner:origLink></item>
		<item>
		<title>F#: Convert sequence to comma separated string</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/w6jEu5hjVUI/</link>
		<comments>http://www.markhneedham.com/blog/2009/07/09/f-convert-sequence-to-comma-separated-string/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 12:32:55 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
		
		<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1435</guid>
		<description><![CDATA[I've been continuing playing around with parsing Cruise data as I mentioned yesterday with the goal today being to create a graph from the build data.
After recommendations from Dean Cornish and Sam Newman on Twitter I decided to give the Google Graph API a try to do this and realised that I would need to [...]]]></description>
			<content:encoded><![CDATA[<p>I've been continuing playing around with <a href="http://www.markhneedham.com/blog/2009/07/08/f-parsing-cruise-build-data/">parsing Cruise data</a> as I mentioned yesterday with the goal today being to create a graph from the build data.</p>
<p>After recommendations from <a href="http://twitter.com/deanrcornish/statuses/2513727860">Dean Cornish</a> and <a href="http://twitter.com/samnewman/statuses/2514527870">Sam Newman</a> on Twitter I decided to give the <a href="http://code.google.com/apis/chart/types.html#line_charts">Google Graph API</a> a try to do this and realised that I would need to create a comma separated string listing all the build times to pass to the Google API.</p>
<p>My initial thinking was that I could just pipe the sequence of values through 'Seq.fold' and add a comma after each value:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> ConvertToCommaSeparatedString <span style="color: #6c6;">&#40;</span>value<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> initialAttempt <span style="color: #a52a2a;">=</span> value <span style="color: #a52a2a;">|&gt;</span> Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">fold</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> acc x <span style="color: #a52a2a;">-&gt;</span> acc <span style="color: #a52a2a;">+</span> x <span style="color: #a52a2a;">+</span> <span style="color: #3cb371;">&quot;,&quot;</span><span style="color: #6c6;">&#41;</span> <span style="color: #3cb371;">&quot;&quot;</span>
    initialAttempt<span style="color: #a52a2a;">.</span><span style="color: #060;">Remove</span><span style="color: #6c6;">&#40;</span>initialAttempt<span style="color: #a52a2a;">.</span><span style="color: #060;">Length</span><span style="color: #c6c;">-1</span><span style="color: #6c6;">&#41;</span></pre></div></div>

<p>It works but you end up with a comma after the last value as well and then need to remove that on the next line which feels very imperative to me.</p>
<p>My next thought was that maybe I would be able to do this by making use of a recursive function which matched the sequence on each iteration and then when it was on the last value in the list to not add the comma.</p>
<p>I know how to do this for a list so I decided to go with that first:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> ConvertToCommaSeparatedString <span style="color: #6c6;">&#40;</span>value<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> convert <span style="color: #6c6;">&#40;</span>innerVal<span style="color: #a52a2a;">:</span>List<span style="color: #a52a2a;">&lt;</span>string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> acc <span style="color: #a52a2a;">=</span> 
        <span style="color: #06c; font-weight: bold;">match</span> innerVal <span style="color: #06c; font-weight: bold;">with</span>
            <span style="color: #a52a2a;">|</span> <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span> <span style="color: #a52a2a;">-&gt;</span> acc
            <span style="color: #a52a2a;">|</span> hd<span style="color: #a52a2a;">::</span><span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span> <span style="color: #a52a2a;">-&gt;</span> convert <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span> <span style="color: #6c6;">&#40;</span>acc <span style="color: #a52a2a;">+</span> hd<span style="color: #6c6;">&#41;</span>
            <span style="color: #a52a2a;">|</span> hd<span style="color: #a52a2a;">::</span>tl <span style="color: #a52a2a;">-&gt;</span> convert tl <span style="color: #6c6;">&#40;</span>acc <span style="color: #a52a2a;">+</span> hd <span style="color: #a52a2a;">+</span> <span style="color: #3cb371;">&quot;,&quot;</span><span style="color: #6c6;">&#41;</span>           
    convert <span style="color: #6c6;">&#40;</span>Seq<span style="color: #a52a2a;">.</span><span style="color: #060;">to_list</span> value<span style="color: #6c6;">&#41;</span> <span style="color: #3cb371;">&quot;&quot;</span></pre></div></div>

<p>That works as well but it seems a bit weird that we need to convert everything in a list to do it.</p>
<p>A bit of googling revealed an <a href="http://cs.hubfs.net/forums/thread/7596.aspx">interesting post by Brian McNamara</a> where he suggests creating an <a href="http://www.markhneedham.com/blog/2009/05/10/f-regular-expressionsactive-patterns/">active pattern</a> which would cast the 'seq' to a 'LazyList' (which is deprecated but won't be removed apparently) and then do some pattern matching against that instead.</p>
<p>The active pattern which Brian describes is like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">|</span>SeqCons<span style="color: #a52a2a;">|</span>SeqNil<span style="color: #a52a2a;">|</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>s<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;'</span>a<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">match</span> s <span style="color: #06c; font-weight: bold;">with</span>
    <span style="color: #a52a2a;">|</span> <span style="color: #a52a2a;">:</span>? LazyList<span style="color: #a52a2a;">&lt;'</span>a<span style="color: #a52a2a;">&gt;</span> <span style="color: #06c; font-weight: bold;">as</span> l <span style="color: #a52a2a;">-&gt;</span>
        <span style="color: #06c; font-weight: bold;">match</span> l <span style="color: #06c; font-weight: bold;">with</span>
        <span style="color: #a52a2a;">|</span> LazyList<span style="color: #a52a2a;">.</span><span style="color: #060;">Cons</span><span style="color: #6c6;">&#40;</span>a,b<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">-&gt;</span> SeqCons<span style="color: #6c6;">&#40;</span>a,<span style="color: #6c6;">&#40;</span>b <span style="color: #a52a2a;">:&gt;</span> seq<span style="color: #a52a2a;">&lt;</span>_<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
        <span style="color: #a52a2a;">|</span> LazyList<span style="color: #a52a2a;">.</span><span style="color: #060;">Nil</span> <span style="color: #a52a2a;">-&gt;</span> SeqNil
    <span style="color: #a52a2a;">|</span> _ <span style="color: #a52a2a;">-&gt;</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">|</span>SeqCons<span style="color: #a52a2a;">|</span>SeqNil<span style="color: #a52a2a;">|</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>LazyList<span style="color: #a52a2a;">.</span><span style="color: #060;">of_seq</span> s <span style="color: #a52a2a;">:&gt;</span> seq<span style="color: #a52a2a;">&lt;</span>_<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span></pre></div></div>

<p>This doesn't cover the three states of the sequence which I want to match so I adjusted it slightly to do what I want:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">|</span>SeqCons<span style="color: #a52a2a;">|</span>SeqNil<span style="color: #a52a2a;">|</span>SeqConsLastElement<span style="color: #a52a2a;">|</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>s<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;'</span>a<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">match</span> s <span style="color: #06c; font-weight: bold;">with</span>
    <span style="color: #a52a2a;">|</span> <span style="color: #a52a2a;">:</span>? LazyList<span style="color: #a52a2a;">&lt;'</span>a<span style="color: #a52a2a;">&gt;</span> <span style="color: #06c; font-weight: bold;">as</span> l <span style="color: #a52a2a;">-&gt;</span>
        <span style="color: #06c; font-weight: bold;">match</span> l <span style="color: #06c; font-weight: bold;">with</span>
        <span style="color: #a52a2a;">|</span> LazyList<span style="color: #a52a2a;">.</span><span style="color: #060;">Cons</span><span style="color: #6c6;">&#40;</span>a,b<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">-&gt;</span> 
            <span style="color: #06c; font-weight: bold;">match</span> b <span style="color: #06c; font-weight: bold;">with</span>
                <span style="color: #a52a2a;">|</span> LazyList<span style="color: #a52a2a;">.</span><span style="color: #060;">Nil</span> <span style="color: #a52a2a;">-&gt;</span> SeqConsLastElement<span style="color: #6c6;">&#40;</span>a<span style="color: #6c6;">&#41;</span>
                <span style="color: #a52a2a;">|</span> LazyList<span style="color: #a52a2a;">.</span><span style="color: #060;">Cons</span><span style="color: #6c6;">&#40;</span>_,_<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">-&gt;</span> SeqCons<span style="color: #6c6;">&#40;</span>a,<span style="color: #6c6;">&#40;</span>b <span style="color: #a52a2a;">:&gt;</span> seq<span style="color: #a52a2a;">&lt;</span>_<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
        <span style="color: #a52a2a;">|</span> LazyList<span style="color: #a52a2a;">.</span><span style="color: #060;">Nil</span> <span style="color: #a52a2a;">-&gt;</span> SeqNil
    <span style="color: #a52a2a;">|</span> _ <span style="color: #a52a2a;">-&gt;</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">|</span>SeqCons<span style="color: #a52a2a;">|</span>SeqNil<span style="color: #a52a2a;">|</span>SeqConsLastElement<span style="color: #a52a2a;">|</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>LazyList<span style="color: #a52a2a;">.</span><span style="color: #060;">of_seq</span> s <span style="color: #a52a2a;">:&gt;</span> seq<span style="color: #a52a2a;">&lt;</span>_<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span></pre></div></div>

<p>Our function to convert sequences to a comma separated string would now look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> ConvertToCommaSeparatedString <span style="color: #6c6;">&#40;</span>value<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> convert <span style="color: #6c6;">&#40;</span>innerVal<span style="color: #a52a2a;">:</span>seq<span style="color: #a52a2a;">&lt;</span>string<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> acc <span style="color: #a52a2a;">=</span> 
        <span style="color: #06c; font-weight: bold;">match</span> innerVal <span style="color: #06c; font-weight: bold;">with</span>
            <span style="color: #a52a2a;">|</span> SeqNil <span style="color: #a52a2a;">-&gt;</span> acc
            <span style="color: #a52a2a;">|</span> SeqConsLastElement<span style="color: #6c6;">&#40;</span>hd<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">-&gt;</span> convert <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span> <span style="color: #6c6;">&#40;</span>acc <span style="color: #a52a2a;">+</span> hd<span style="color: #6c6;">&#41;</span>
            <span style="color: #a52a2a;">|</span> SeqCons<span style="color: #6c6;">&#40;</span>hd,tl<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">-&gt;</span> convert tl <span style="color: #6c6;">&#40;</span>acc <span style="color: #a52a2a;">+</span> hd <span style="color: #a52a2a;">+</span> <span style="color: #3cb371;">&quot;,&quot;</span><span style="color: #6c6;">&#41;</span>           
    convert <span style="color: #6c6;">&#40;</span>value<span style="color: #6c6;">&#41;</span> <span style="color: #3cb371;">&quot;&quot;</span></pre></div></div>

<p>An example of this in action would be like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text">ConvertToCommaSeparatedString (seq { yield &quot;mark&quot;; yield &quot;needham&quot; });;
val it : string = &quot;mark,needham&quot;</pre></div></div>

<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/w6jEu5hjVUI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/07/09/f-convert-sequence-to-comma-separated-string/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/07/09/f-convert-sequence-to-comma-separated-string/</feedburner:origLink></item>
		<item>
		<title>F#: Parsing Cruise build data</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/CKgL0fa_Bhg/</link>
		<comments>http://www.markhneedham.com/blog/2009/07/08/f-parsing-cruise-build-data/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 12:46:05 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
		
		<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1432</guid>
		<description><![CDATA[I've been playing around a bit with the properties REST API that Cruise exposes to try and get together some build metrics and I decided it might be an interesting task to try and use F# for.
I'm making use of the 'search' part of the API to return the metrics of all the builds run [...]]]></description>
			<content:encoded><![CDATA[<p>I've been playing around a bit with the <a href="http://studios.thoughtworks.com/cruise-continuous-integration/1.3.0/help/Properties_API.html">properties REST API</a> that Cruise exposes to try and get together some build metrics and I decided it might be an interesting task to try and use F# for.</p>
<p>I'm making use of the 'search' part of the API to return the metrics of all the builds run on a certain part of the pipeline and I then want to parse those results so that I can extract just the name of the agent that ran that build and the duration of that build.</p>
<p>The first part of this task is to parse the data and extract just the information I'm interested in.</p>
<p>The data is like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text">cruise_agent,cruise_job_duration,cruise_job_id,cruise_job_result,cruise_pipeline_label,cruise_timestamp_01_scheduled,cruise_timestamp_02_assigned,cruise_timestamp_03_preparing,cruise_timestamp_04_building,cruise_timestamp_05_completing,cruise_timestamp_06_completed\n
BuildAgentOne (Sydney, PersonOne),319,14052,Passed,2223,2009-06-25 12:14:01 +1000,2009-06-25 12:14:02 +1000,2009-06-25 12:14:02 +1000,2009-06-25 12:14:35 +1000,2009-06-25 12:19:54 +1000,2009-06-25 12:19:55 +1000\n
BuildAgentTwo (Sydney, PersonTwo),422,14084,Passed,2224,2009-06-25 14:13:57 +1000,2009-06-25 14:13:58 +1000,2009-06-25 14:13:58 +1000,2009-06-25 14:14:48 +1000,2009-06-25 14:21:49 +1000,2009-06-25 14:21:50 +1000\n</pre></div></div>

<p>I first started off trying to do this extraction all in one regular expression but after a while realised that I'd probably have more success if I ran a regular expression over each line individually.</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">type</span> CruiseData <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span> Agent<span style="color: #a52a2a;">:</span> string<span style="color: #a52a2a;">;</span> Duration<span style="color: #a52a2a;">:</span> string <span style="color: #6c6;">&#125;</span></pre></div></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> ExtractValues <span style="color: #6c6;">&#40;</span>item<span style="color: #a52a2a;">:</span>string<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>  
   <span style="color: #06c; font-weight: bold;">let</span> matchBuildDuration item <span style="color: #a52a2a;">=</span> Regex<span style="color: #a52a2a;">.</span><span style="color: #06c; font-weight: bold;">Match</span><span style="color: #6c6;">&#40;</span>item, <span style="color: #3cb371;">&quot;(.*\)),([0-9]+),&quot;</span><span style="color: #6c6;">&#41;</span> 
   Regex<span style="color: #a52a2a;">.</span><span style="color: #060;">Split</span><span style="color: #6c6;">&#40;</span>item, <span style="color: #3cb371;">&quot;\n&quot;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> 
   <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> item <span style="color: #a52a2a;">-&gt;</span> 
        <span style="color: #06c; font-weight: bold;">let</span> m <span style="color: #a52a2a;">=</span> matchBuildDuration item
        <span style="color: #06c; font-weight: bold;">if</span><span style="color: #6c6;">&#40;</span>m<span style="color: #a52a2a;">.</span><span style="color: #060;">Success</span><span style="color: #6c6;">&#41;</span> 
        <span style="color: #06c; font-weight: bold;">then</span> <span style="color: #6c6;">&#123;</span> Agent <span style="color: #a52a2a;">=</span> m<span style="color: #a52a2a;">.</span><span style="color: #060;">Groups</span><span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span><span style="color: #c6c;">1</span><span style="color: #6c6;">&#93;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span><span style="color: #a52a2a;">;</span> Duration <span style="color: #a52a2a;">=</span> m<span style="color: #a52a2a;">.</span><span style="color: #060;">Groups</span><span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span><span style="color: #c6c;">2</span><span style="color: #6c6;">&#93;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span> <span style="color: #6c6;">&#125;</span> 
        <span style="color: #06c; font-weight: bold;">else</span> <span style="color: #6c6;">&#123;</span> Agent <span style="color: #a52a2a;">=</span> <span style="color: #3cb371;">&quot;&quot;</span><span style="color: #a52a2a;">;</span> Duration <span style="color: #a52a2a;">=</span> <span style="color: #3cb371;">&quot;&quot;</span><span style="color: #6c6;">&#125;</span>  <span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span>
   <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">filter</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> item <span style="color: #a52a2a;">-&gt;</span> item<span style="color: #a52a2a;">.</span><span style="color: #060;">Agent</span> <span style="color: #a52a2a;">&lt;&gt;</span> <span style="color: #3cb371;">&quot;&quot;</span> <span style="color: #a52a2a;">&amp;&amp;</span> item<span style="color: #a52a2a;">.</span><span style="color: #060;">Duration</span> <span style="color: #a52a2a;">&lt;&gt;</span> <span style="color: #3cb371;">&quot;&quot;</span><span style="color: #6c6;">&#41;</span></pre></td></tr></table></div>

<p>I realised when I started writing the let statement inside the Array.map function on line 4 that I was thinking about this problem way too imperatively. I actually backed out at that stage and had another go but I decided it would be interesting to see what each iteration of the solution would look like if I had actually completed it.</p>
<p>An improvement on that would be to not set up an empty 'CruiseData' like we are doing on line 8 but instead to make use of the Option type to define when we do and do not have a value:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> ExtractValues <span style="color: #6c6;">&#40;</span>item<span style="color: #a52a2a;">:</span>string<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>  
   <span style="color: #06c; font-weight: bold;">let</span> matchBuildDuration item <span style="color: #a52a2a;">=</span> Regex<span style="color: #a52a2a;">.</span><span style="color: #06c; font-weight: bold;">Match</span><span style="color: #6c6;">&#40;</span>item, <span style="color: #3cb371;">&quot;(.*\)),([0-9]+),&quot;</span><span style="color: #6c6;">&#41;</span> 
   Regex<span style="color: #a52a2a;">.</span><span style="color: #060;">Split</span><span style="color: #6c6;">&#40;</span>item, <span style="color: #3cb371;">&quot;\n&quot;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> 
   <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> item <span style="color: #a52a2a;">-&gt;</span> 
    <span style="color: #06c; font-weight: bold;">let</span> m <span style="color: #a52a2a;">=</span> matchBuildDuration item
    <span style="color: #06c; font-weight: bold;">if</span><span style="color: #6c6;">&#40;</span>m<span style="color: #a52a2a;">.</span><span style="color: #060;">Success</span><span style="color: #6c6;">&#41;</span> 
    <span style="color: #06c; font-weight: bold;">then</span> Some<span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#123;</span> Agent <span style="color: #a52a2a;">=</span> m<span style="color: #a52a2a;">.</span><span style="color: #060;">Groups</span><span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span><span style="color: #c6c;">1</span><span style="color: #6c6;">&#93;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span><span style="color: #a52a2a;">;</span> Duration <span style="color: #a52a2a;">=</span> m<span style="color: #a52a2a;">.</span><span style="color: #060;">Groups</span><span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span><span style="color: #c6c;">2</span><span style="color: #6c6;">&#93;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span> <span style="color: #6c6;">&#125;</span><span style="color: #6c6;">&#41;</span> 
    <span style="color: #06c; font-weight: bold;">else</span> None <span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span>
    <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">filter</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> item <span style="color: #a52a2a;">-&gt;</span> item<span style="color: #a52a2a;">.</span><span style="color: #060;">IsSome</span><span style="color: #6c6;">&#41;</span></pre></td></tr></table></div>

<p>It's still not great as we have imperative logic inside the Array.map function which looks pretty ugly. </p>
<p>At this stage I realised that I needed to excluded any lines which didn't match the regular expression so that I wouldn't have to care about them at all.</p>
<p>This was the next solution:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> ExtractValues <span style="color: #6c6;">&#40;</span>response<span style="color: #a52a2a;">:</span>string<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>  
    <span style="color: #06c; font-weight: bold;">let</span> matchBuildDuration item <span style="color: #a52a2a;">=</span> Regex<span style="color: #a52a2a;">.</span><span style="color: #06c; font-weight: bold;">Match</span><span style="color: #6c6;">&#40;</span>item, <span style="color: #3cb371;">&quot;(.*\)),([0-9]+),&quot;</span><span style="color: #6c6;">&#41;</span> 
    Regex<span style="color: #a52a2a;">.</span><span style="color: #060;">Split</span><span style="color: #6c6;">&#40;</span>response, <span style="color: #3cb371;">&quot;\n&quot;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> 
    <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">filter</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> <span style="color: #6c6;">&#40;</span>matchBuildDuration x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Success</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span>
    <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> <span style="color: #6c6;">&#40;</span>matchBuildDuration x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Groups</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span>
    <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> group <span style="color: #a52a2a;">-&gt;</span> <span style="color: #6c6;">&#123;</span> Agent <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span>group<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span><span style="color: #c6c;">1</span><span style="color: #6c6;">&#93;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span> Duration <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span>group<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span><span style="color: #c6c;">2</span><span style="color: #6c6;">&#93;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#125;</span> <span style="color: #6c6;">&#41;</span></pre></td></tr></table></div>

<p>This is better although we are now calling the 'matchBuildDuration' function twice which is a bit wasteful. </p>
<p><a href="http://twitter.com/davcamer">Dave</a> pointed out that if we run the data straight through the 'matchBuildDuration' function after splitting the new lines we can remove the need to call the function twice and then inline the function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ocaml"><span style="color: #06c; font-weight: bold;">let</span> ExtractValues <span style="color: #6c6;">&#40;</span>response<span style="color: #a52a2a;">:</span>string<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>  
    Regex<span style="color: #a52a2a;">.</span><span style="color: #060;">Split</span><span style="color: #6c6;">&#40;</span>response, <span style="color: #3cb371;">&quot;\n&quot;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span> 
    <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> Regex<span style="color: #a52a2a;">.</span><span style="color: #06c; font-weight: bold;">Match</span><span style="color: #6c6;">&#40;</span>x, <span style="color: #3cb371;">&quot;(.*\)),([0-9]+),&quot;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>  <span style="color: #a52a2a;">|&gt;</span>
    <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">filter</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> x<span style="color: #a52a2a;">.</span><span style="color: #060;">Success</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span>
    <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&gt;</span> x<span style="color: #a52a2a;">.</span><span style="color: #060;">Groups</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">|&gt;</span>
    <span style="color: #06c; font-weight: bold;">Array</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> group <span style="color: #a52a2a;">-&gt;</span> <span style="color: #6c6;">&#123;</span> Agent <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span>group<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span><span style="color: #c6c;">1</span><span style="color: #6c6;">&#93;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span> Duration <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span>group<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span><span style="color: #c6c;">2</span><span style="color: #6c6;">&#93;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Value</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#125;</span> <span style="color: #6c6;">&#41;</span></pre></td></tr></table></div>

<p>In all the functions we end up with the following data by executing this function:</p>

<div class="wp_syntax"><div class="code"><pre class="text">[|{Agent = &quot;BuildAgentOne&quot;; Duration = &quot;319&quot;};
  {Agent = &quot;BuildAgentTwo&quot;; Duration = &quot;422&quot;}|]</pre></div></div>

<p>My current thinking is that if I have more than one expression inside a function it's very probable that there's a better way of solving the problem and if I have conditional logic in there then I've gone very wrong.</p>
<p>I'd be interested to see if there's an even simpler way to solve this problem.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/CKgL0fa_Bhg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/07/08/f-parsing-cruise-build-data/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/07/08/f-parsing-cruise-build-data/</feedburner:origLink></item>
		<item>
		<title>Book Club: Why noone uses functional languages (Philip Wadler)</title>
		<link>http://feedproxy.google.com/~r/MarkNeedham/~3/zh3lN8edQ7o/</link>
		<comments>http://www.markhneedham.com/blog/2009/07/08/book-club-why-noone-uses-functional-languages-philip-wadler/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 14:29:56 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
		
		<category><![CDATA[Book Club]]></category>

		<category><![CDATA[functional-programming]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1428</guid>
		<description><![CDATA[Our latest technical book club discussion was based around Philip Wadler's paper 'Why noone uses functional langauges' which he wrote in 1998. I came across this paper when reading some of the F# goals in the FAQs on the Microsoft website.
These are some of my thoughts and our discussion of the paper:

One of the points [...]]]></description>
			<content:encoded><![CDATA[<p>Our latest technical book club discussion was based around Philip Wadler's paper '<a href="http://www.cse.iitb.ac.in/~as/fpcourse/sigplan-why.ps.gz">Why noone uses functional langauges</a>' which he wrote in 1998. I came across this paper when reading some of the <a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/faq.aspx#Goals">F# goals in the FAQs</a> on the Microsoft website.</p>
<p>These are some of my thoughts and our discussion of the paper:</p>
<ul>
<li>One of the points suggested in the paper is that <strong>functional languages aren't used because of their lack of availability</strong> on machines but as <a href="http://twitter.com/davcamer">Dave</a> pointed out this doesn't really seem to be such a big problem these days - certainly for F# I've found it relatively painless to get it setup and running and even for a language like Ruby people are happy to download and install it on their machines and it is also pretty much painless to do so.</li>
<li><a href="http://erik.doernenburg.com/">Erik</a> pointed us to an <a href="http://prog21.dadgum.com/3.html">interesting article which suggests that functional programming can be very awkward for solving certain problems</a> - I think this is definitely true to an extent although perhaps not as much as we might think. I am certainly seeing some benefit in <a href="http://www.markhneedham.com/blog/2009/04/25/oo-with-a-bit-of-functional-mixed-in/">an overall OO approach with some functional concepts mixed in</a> which seems to strike a nice balance between code which is descriptive yet concise in places. I'm finding the problems that F# is useful for tend to be very data intensive in nature.</li>
<li>Matt Dunn pointed out that an <a href="http://www.paulgraham.com/avg.html">e-commerce store written by Paul Graham</a>, which he later sold to Yahoo, was actually written in Lisp - to me this would seem like the type of problem that wouldn't be that well suited for a functional language but interestingly only part of the system was written in Lisp and the other part in C.<br />
<blockquote><p>
Viaweb at first had two parts: the editor, written in Lisp, which people used to build their sites, and the ordering system, written in C, which handled orders. The first version was mostly Lisp, because the ordering system was small. Later we added two more modules, an image generator written in C, and a back-office manager written mostly in Perl.
</p></blockquote>
</li>
<li>The article also suggests that it <strong>takes a while for Java programmers to come to grips with functional programs</strong> - I would agree with this statement to an extent although one of the things I found really hard when first reading functional programs is the non descriptiveness of the variable names. It seems to be more idiomatic to make use of single letter variable names instead of something more descriptive which I would use in an imperative language.
<p>I'm intrigued as to whether this will change as more people use functional languages or whether this is just something we will need to get used to.</li>
<li>The author makes a very valid point with regards to the <strong>risk that a project manager would be taking if they decided to use a functional language</strong> for a project:<br />
<blockquote><p>
If a manager chooses to use a functional language for  a project and the project fails, then he or she will certainly be ﬁred. If a manager chooses C++ and the project fails, then he or she has the defense that the same thing has happened to everyone else.
</p></blockquote>
<p>I'm sure I remember a similar thing being said about the reluctance to make use of Ruby a couple of years ago - it's something of a risk and human nature is often geared towards avoiding those!
</li>
<li>I think the <strong>availability of libraries</strong> is probably very relevant even today - it helps F# a lot that we have access to all the .NET libraries and I imagine it's also the same for Scala with the Java libraries. I don't know a lot about the Lisp world but I'm told that people often end up rolling their own libraries for some quite basic things since there aren't standard libraries available as there are in some other languages.</li>
<li>Another paper pointed out as being a good one to read was '<a href="http://www.defmacro.org/ramblings/fp.html">Functional Programming For The Rest Of Us</a>' - I haven't read it yet but it does look quite lengthy! Wes Dyer also has a couple of articles which I found interesting - one around <a href="http://blogs.msdn.com/wesdyer/archive/2007/01/15/thinking-functionally.aspx">thinking functionally</a> and the other around <a href="http://blogs.msdn.com/wesdyer/archive/2007/01/18/why-functional-programming-is-important-in-a-mixed-environment.aspx">how functional programming can fit in a mixed programming environment</a></li>
</ul>
<p>I think in general a lot of the points this paper raises have been addressed by some of the functional languages which are gaining prominence more recently - Erlang, F# and Scala to name a few.</p>
<p>It will definitely be interesting to see what role functional languages have to play in the <a href="http://memeagora.blogspot.com/2006/12/polyglot-programming.html">polyglot programming era</a> that my colleague Neal Ford foresees.</p>
<img src="http://feeds.feedburner.com/~r/MarkNeedham/~4/zh3lN8edQ7o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/07/08/book-club-why-noone-uses-functional-languages-philip-wadler/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.markhneedham.com/blog/2009/07/08/book-club-why-noone-uses-functional-languages-philip-wadler/</feedburner:origLink></item>
	</channel>
</rss>
