<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2340562896607928708</id><updated>2012-01-23T12:27:51.970-08:00</updated><category term='c#'/><category term='turtle'/><category term='Mocks'/><category term='lazy'/><category term='kata'/><category term='FsStory'/><category term='books'/><category term='haskell'/><category term='podcasts'/><category term='F#'/><category term='fizzbuzz'/><category term='bdd'/><category term='OO'/><category term='extension methods'/><category term='DI'/><title type='text'>Coffee Driven Development</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>40</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-5901740532014167239</id><published>2009-08-09T11:19:00.000-07:00</published><updated>2009-08-09T11:24:02.818-07:00</updated><title type='text'>... and Christian moves to his own blog as well!</title><content type='html'>Been fun running this blog, but now it's time for me too to move back to my old blog:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://christiangenne.blogspot.com"&gt;http://christiangenne.blogspot.com&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Hope to see you there!&lt;br /&gt;&lt;br /&gt;And Gustaf, thank &lt;span style="font-style:italic;"&gt;you&lt;/span&gt; :)&lt;br /&gt;&lt;br /&gt;/ Christian&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-5901740532014167239?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/5901740532014167239/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=5901740532014167239' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5901740532014167239'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5901740532014167239'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2009/08/and-christian-moves-to-his-own-blog-as.html' title='... and Christian moves to his own blog as well!'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-82983302129232892</id><published>2009-08-02T07:31:00.000-07:00</published><updated>2009-08-02T07:41:04.288-07:00</updated><title type='text'>Gustaf moves to his own blog.</title><content type='html'>From now on, I (Gustaf) will post all my posts on my original blog, &lt;a href="http://gustafnilssonkotte.blogspot.com/"&gt;http://gustafnilssonkotte.blogspot.com/&lt;/a&gt;. I would be very (!) happy if you added that feed to your readers.&lt;br /&gt;&lt;br /&gt;Sorry for the inconvenience.&lt;br /&gt;&lt;br /&gt;Christian: Thank you for a very nice time buddy! :)&lt;br /&gt;&lt;br /&gt;/Gustaf&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-82983302129232892?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/82983302129232892/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=82983302129232892' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/82983302129232892'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/82983302129232892'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2009/08/gustaf-moves-to-his-own-blog.html' title='Gustaf moves to his own blog.'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-2807800648078938113</id><published>2009-06-02T13:48:00.000-07:00</published><updated>2009-06-02T14:10:36.749-07:00</updated><title type='text'>Objective-C... yet another programming language?</title><content type='html'>Just recently installed XCode on my mac in order to develop applications for my iPhone. The first thing I realized was that I needed to learn yet another programming language. Just another syntax, or does Objective-C actually bring something new?&lt;br /&gt;&lt;br /&gt;It turns out Objective-C (which is a language derived from C, similar to C++, but still very different) has one feature I haven't seen before. First of, instead of calling a method on an object, your send a message to it. It's almost the same, but also requires you to pass along named parameters (which means more code to write, but easier to read). The new and cool feature appears when you want to send a message to an object, which should return a value, when the object turns out to be nil.&lt;br /&gt;&lt;pre class="c#" name="code"&gt;&lt;br /&gt;MyClass* myObject = nil;&lt;br /&gt;if ([myObject getValue] == 0.0) {&lt;br /&gt;  // myObject getValue retuned 0, OR myObject is nil&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So when executing this code, as opposed to how normal programming languages handles it, it doesn't crash. It simply returns 0.0 if the expected return type is a number, or nil if the expected return type is a pointer to an object. This also works for functions returning nothing (i.e. void), sending a void returning message to a nil object will simpy do nothing.&lt;br /&gt;&lt;br /&gt;In for example C# you always have to do null checks all over the code, in order to verify your object isn't nil.&lt;br /&gt;&lt;br /&gt;I'm not sure I like it or not, but if you learn to think "pure Objective-C" then perhaps this can be used as an advantage! I'm looking forward to learning Objective-C further, and to adapt these new concepts!&lt;br /&gt;&lt;br /&gt;I'll let you know when I've published my first iPhone app :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-2807800648078938113?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/2807800648078938113/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=2807800648078938113' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/2807800648078938113'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/2807800648078938113'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2009/06/objective-c-yet-another-programming.html' title='Objective-C... yet another programming language?'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-6038647255408980923</id><published>2009-04-25T09:20:00.001-07:00</published><updated>2009-04-25T10:29:40.500-07:00</updated><title type='text'>From for loop to anamorphism</title><content type='html'>&lt;h3&gt;Introduction&lt;/h3&gt;  &lt;div&gt;Sometimes you want to generate a sequence of objects. This is often done using a for loop:&lt;/div&gt;  &lt;pre name="code" class="c#"&gt;[Test]&lt;br /&gt;public void CreateListOfFoos()&lt;br /&gt;    {&lt;br /&gt;        var xs = new List&amp;lt;Foo&amp;gt;();&lt;br /&gt;        for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;        {&lt;br /&gt;            xs.Add(new Foo());&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        Assert.AreEqual(10, xs.Count());&lt;br /&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;In this post, I will show you how this code can be made more general, ultimately turning it into an anamorphism over lists.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Generalizing constructed type&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;First, what we want to do is to be able to create something other than a Foo. We apply ExtractVariable once and we also extract the constructor call to a lambda:&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;[Test]&lt;br /&gt;public void ExtractMethod()&lt;br /&gt;{&lt;br /&gt;    var times = 10;&lt;br /&gt;    Func&amp;lt;Foo&amp;gt;newFoo = () =&amp;gt; new Foo();&lt;br /&gt;&lt;br /&gt;    var xs = CreateFooNumberOfTimes(times, newFoo);&lt;br /&gt;&lt;br /&gt;    Assert.AreEqual(10, xs.Count());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private IEnumerable&amp;lt;Foo&amp;gt; CreateFooNumberOfTimes(int times, Func&amp;lt;Foo&amp;gt; newFoo)&lt;br /&gt;{&lt;br /&gt;    var xs = new List&amp;lt;Foo&amp;gt;();&lt;br /&gt;    for (int i = 0; i &amp;lt; times; i++)&lt;br /&gt;    {&lt;br /&gt;        xs.Add(newFoo.Invoke());&lt;br /&gt;    }&lt;br /&gt;    return xs;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;From the above code, it’s easy to generalize on the type created. Note that we could have skipped the “constructor lambda” and instead used the “where T : new()” constraint. &lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;[Test]&lt;br /&gt;public void GeneralizeTypeForFunc()&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    var times = 10;&lt;br /&gt;    Func&amp;lt;Foo&amp;gt; newFoo = () =&amp;gt; new Foo();&lt;br /&gt;&lt;br /&gt;    var xs = CreateTNumberOfTimes(times, newFoo);&lt;br /&gt;&lt;br /&gt;    Assert.AreEqual(10, xs.Count());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private IEnumerable&amp;lt;T&amp;gt; CreateTNumberOfTimes&amp;lt;T&amp;gt;(int times, Func&amp;lt;T&amp;gt; newFoo)&lt;br /&gt;{&lt;br /&gt;    var xs = new List&amp;lt;T&amp;gt;();&lt;br /&gt;    for (int i = 0; i &amp;lt; times; i++)&lt;br /&gt;    {&lt;br /&gt;        xs.Add(newFoo.Invoke());&lt;br /&gt;    }&lt;br /&gt;    return xs;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Generalizing type for accumulator&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;We have now parametrized Foo to T, but wouldn’t it be possible to parametrize from “int” to A, as well? Let’s begin with breaking out the int-specific code from the for loop:&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;[Test]&lt;br /&gt;public void ExtractForLoopLogic()&lt;br /&gt;{&lt;br /&gt;    Func&amp;lt;Foo&amp;gt; newFoo = () =&amp;gt; new Foo();&lt;br /&gt;&lt;br /&gt;    int i = 0; // Will be modified lots of times&lt;br /&gt;    Func&amp;lt;bool&amp;gt; expr = () =&amp;gt; i &amp;lt; 10;&lt;br /&gt;    Action inc = () =&amp;gt; i++;&lt;br /&gt;&lt;br /&gt;    var xs = CreateTUntil(newFoo, expr, inc);&lt;br /&gt;&lt;br /&gt;    Assert.AreEqual(10, xs.Count());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private IEnumerable&amp;lt;T&amp;gt;CreateTUntil&amp;lt;T&amp;gt;(Func&amp;lt;T&amp;gt; newT, Func&amp;lt;bool&amp;gt; expr, Action inc)&lt;br /&gt;{&lt;br /&gt;    var xs = new List&amp;lt;T&amp;gt;();&lt;br /&gt;    for (; expr.Invoke(); inc.Invoke())&lt;br /&gt;    {&lt;br /&gt;        xs.Add(newT.Invoke());&lt;br /&gt;    }&lt;br /&gt;    return xs;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;As indicated in the code, the variable “i” will be modified the closure called in &lt;em&gt;CreateTUntil&lt;/em&gt;&lt;strong&gt;.&lt;/strong&gt; Bart de Smet calls this a &lt;a href="http://bartdesmet.net/blogs/bart/archive/2008/10/26/about-cruel-lambdas-closures-typedreferences-cs0610-and-other-things-you-shouldn-t-do.aspx"&gt;cruel lambda&lt;/a&gt;. Except that it’s quite hard to understand a lambda that mutate its outer scope, refactoring code that’s using cruel lambdas can make your code go totally bananas!&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;Let’s rewrite the code to use a pure lambdas instead. To do this, we need to refactor the for loop to a while loop, since the first and third “parameters” to a for loop are &lt;em&gt;statements&lt;/em&gt; (not pure). We also parametrize from “int” to type parameter “A” instead.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Going pure and a more general constructor&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;[Test]&lt;br /&gt;public void ForLoopToWhileLoop()&lt;br /&gt;{&lt;br /&gt;    Func&amp;lt;Foo&amp;gt; newFoo = () =&amp;gt; new Foo();&lt;br /&gt;    Func&amp;lt;int,bool&amp;gt; expr = a =&amp;gt; a &amp;lt; 10;&lt;br /&gt;    Func&amp;lt;int,int&amp;gt; inc = i =&amp;gt; i + 1;&lt;br /&gt;&lt;br /&gt;    var xs = CreateTUntilUsingWhile(newFoo, 0, expr, inc);&lt;br /&gt;&lt;br /&gt;    Assert.AreEqual(10, xs.Count());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Now a pure method&lt;br /&gt;private IEnumerable&amp;lt;T&amp;gt; CreateTUntilUsingWhile&amp;lt;T&amp;gt;&lt;br /&gt;    (Func&amp;lt;T&amp;gt; newT, int init, Func&amp;lt;int,bool&amp;gt; expr, Func&amp;lt;int,int&amp;gt; inc)&lt;br /&gt;{&lt;br /&gt;    &lt;br /&gt;    var xs = new List&amp;lt;T&amp;gt;();&lt;br /&gt;&lt;br /&gt;    int i = init;&lt;br /&gt;    &lt;br /&gt;    while (expr.Invoke(i))&lt;br /&gt;    {&lt;br /&gt;        xs.Add(newT.Invoke());&lt;br /&gt;        i = inc.Invoke(i);&lt;br /&gt;    }&lt;br /&gt;    return xs;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;Now we don’t have any concrete types in the method signature, except bool, which I think is ok to have there at this point. But, as the observant reader might have noticed, the constructor can’t be called with a variable argument, i.e. the accumulated value. What we need to do is to “connect” the lambdas that generates values, like this:&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;[Test]&lt;br /&gt;public void ArgumentForConstructor()&lt;br /&gt;{&lt;br /&gt;    Func&amp;lt;int,Result&amp;lt;Foo,int&amp;gt;&amp;gt; gen = n =&amp;gt; new Result&amp;lt;Foo,int&amp;gt;(new Foo(), n + 1);&lt;br /&gt;    Func&amp;lt;int,bool&amp;gt; expr = a =&amp;gt; a &amp;lt; 10;&lt;br /&gt;    &lt;br /&gt;    var xs = GeneralizedCreateTWithArgsUntilUsingWhile(gen, 0, expr);&lt;br /&gt;&lt;br /&gt;    Assert.AreEqual(10, xs.Count());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private IEnumerable&amp;lt;T&amp;gt; GeneralizedCreateTWithArgsUntilUsingWhile&amp;lt;T,A&amp;gt;&lt;br /&gt;    (Func&amp;lt;a,Result&amp;gt;&amp;lt;T,A&amp;gt;&amp;gt; gen, A init, Func&amp;lt;A,bool&amp;gt; expr)&lt;br /&gt;{&lt;br /&gt;    var xs = new List&amp;lt;T&amp;gt;();&lt;br /&gt;&lt;br /&gt;    var i = init;&lt;br /&gt;&lt;br /&gt;    while (expr.Invoke(i))&lt;br /&gt;    {&lt;br /&gt;        var result = gen.Invoke(i);&lt;br /&gt;&lt;br /&gt;        xs.Add(result.Value);&lt;br /&gt;        i = result.Accumulator;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    return xs;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Going recursive&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;Now it’s up to the generating lambda to pass an argument to the constructor or not. What’s funny with this is that if we replace the while loop to a recursive call, we come pretty close to the definition of an anamorphism over lists in the introduction of &lt;a href="http://wwwhome.cs.utwente.nl/~fokkinga/mmf91m.ps"&gt;Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire&lt;/a&gt; by Erik Meijer et. al (link to postscript version). &lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;[Test]&lt;br /&gt;public void WhileLoopToRec()&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    Func&amp;lt;int, Result&amp;lt;Foo, int&amp;gt;&amp;gt; gen = n =&amp;gt; new Result&amp;lt;Foo, int&amp;gt;(new Foo(), n + 1);&lt;br /&gt;    Func&amp;lt;int, bool&amp;gt; expr = a =&amp;gt; a &amp;lt; 10;&lt;br /&gt;&lt;br /&gt;    var xs = CreateTUntilUsingRec(gen, 0, expr);&lt;br /&gt;&lt;br /&gt;    Assert.AreEqual(10, xs.Count());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private IEnumerable&amp;lt;T&amp;gt; CreateTUntilUsingRec&amp;lt;T,A&amp;gt;&lt;br /&gt;    (Func&amp;lt;A, Result&amp;lt;T, A&amp;gt;&amp;gt; gen, A init, Func&amp;lt;A, bool&amp;gt; expr)&lt;br /&gt;{&lt;br /&gt;    &lt;br /&gt;    if (!expr.Invoke(init))&lt;br /&gt;        return new List&amp;lt;T&amp;gt;();&lt;br /&gt;&lt;br /&gt;    var result = gen.Invoke(init);&lt;br /&gt;&lt;br /&gt;    return (new List&amp;lt;T&amp;gt; {result.Value})&lt;br /&gt;        .Concat(CreateTUntilUsingRec(gen, result.Accumulator, expr));&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Abstracting away from bool&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;It seems that the only thing left to do is to abstract away the dependency on “bool” in &lt;em&gt;CreateTUsingRec, &lt;/em&gt;but then we bump into a small problem,as you will see. What we do is to join the two lambdas into one and performing a null check inside the recursive function.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;[Test]&lt;br /&gt;public void MergeOnceMore()&lt;br /&gt;{&lt;br /&gt;    int? i = 0;&lt;br /&gt;    Func&amp;lt;int, Result&amp;lt;int?, int&amp;gt;&amp;gt; gen = n =&amp;gt; new Result&amp;lt;int?, int&amp;gt;(n &amp;lt; 10 ? i : null, n + 1);&lt;br /&gt;&lt;br /&gt;    var xs = CreateTUntilUsingRecNullCheck(gen, 0);&lt;br /&gt;&lt;br /&gt;    Assert.AreEqual(10, xs.Count());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private IEnumerable&amp;lt;T&amp;gt; CreateTUntilUsingRecNullCheck&amp;lt;T, A&amp;gt;&lt;br /&gt;    (Func&amp;lt;A, Result&amp;lt;T, A&amp;gt;&amp;gt; gen, A init)&lt;br /&gt;{&lt;br /&gt;    &lt;br /&gt;    var result = gen.Invoke(init);&lt;br /&gt;    if (result.Value == null)&lt;br /&gt;        return new List&amp;lt;T&amp;gt;();&lt;br /&gt;&lt;br /&gt;    return (new List&amp;lt;T&amp;gt; { result.Value })&lt;br /&gt;        .Concat(CreateTUntilUsingRecNullCheck(gen, result.Accumulator));&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Writing it in F# instead&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;The problem with this solution is that we have lost the ability to generate ordinary non-nullable structs and that’s bad! We could easily solve the problem with writing our own MyNullable&amp;lt;T&amp;gt; which would&amp;#160; allow both classes and structs as instantiators of the type variable, but instead of doing that, I’ll show you something similar: the option type in F#.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre name="code"&gt;let rec anamorphism n f = &lt;br /&gt;    match f n with&lt;br /&gt;      | option.None -&amp;gt; [] &lt;br /&gt;      | option.Some (e,next) -&amp;gt; e::(anamorphism next f)&lt;br /&gt;&lt;br /&gt;&amp;gt; anamorphism 0 (fun a -&amp;gt; if a &amp;lt; 5 then option.Some(&amp;quot;Bar&amp;quot; + a.ToString(), a + 1) else option.None);;&lt;br /&gt;val it : string list = [&amp;quot;Bar0&amp;quot;; &amp;quot;Bar1&amp;quot;; &amp;quot;Bar2&amp;quot;; &amp;quot;Bar3&amp;quot;; &amp;quot;Bar4&amp;quot;]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;Here, we removed the &lt;em&gt;Result&lt;/em&gt; class and used a pair instead, together with the &lt;em&gt;Option&lt;/em&gt; type, which is essentially the same as Nullable&amp;lt;T&amp;gt;, but without the restriction on type.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Conslusion&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;Functional programming is perhaps more abstract than imperative programming, but it is also seems to be more general, at least in this case. This post has only showed an anamorphism over lists, which is the simple case. Look &lt;a href="http://ulissesaraujo.wordpress.com/2009/04/08/anamorphisms-in-haskell/"&gt;here&lt;/a&gt; if you want to see a more advanced example.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-6038647255408980923?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/6038647255408980923/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=6038647255408980923' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/6038647255408980923'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/6038647255408980923'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2009/04/from-for-loop-to-anamorphism.html' title='From for loop to anamorphism'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-1369951050449610040</id><published>2009-03-04T12:00:00.000-08:00</published><updated>2009-03-04T12:43:44.462-08:00</updated><title type='text'>Why you love and hate Ruby</title><content type='html'>What strikes me when programming Ruby is how simple everything is! You've got utility functions for almost everything, and writing an application doesn't take more then a few lines of code. No need to setup a project, no need to think about types. You just write code!&lt;br /&gt;&lt;br /&gt;The downside of this, of course, is that the code easily becomes bad written, and as there are very few rules in Ruby, it isn't very easy reading others' code. This is why Ruby sometimes is classified as a "hard to lean language". There are simply too many ways of writing the same code.&lt;br /&gt;&lt;br /&gt;I don't agree that this makes the language more hard to learn, but perhaps makes it take longer to learn the language fully. For instance, I just recently learned you can write code in the following way:&lt;br /&gt;&lt;pre name="code" class="ruby"&gt;&lt;br /&gt;puts "Hello my friend" if userIsFriendly&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I've never seen code like that before, but it really opens up for some nice syntax! &lt;br /&gt;&lt;br /&gt;Another feature of Ruby is that Classes are open, and &lt;span style="font-weight:bold;"&gt;everything&lt;/span&gt; is an object, meaning you can extend/modify classes on the fly. Even classes are objects! For example:&lt;br /&gt;&lt;pre name="code" class="ruby"&gt;&lt;br /&gt;class Fixnum&lt;br /&gt;  def minutes&lt;br /&gt;    return Timespan.new(self)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;class Timespan&lt;br /&gt;  def ago&lt;br /&gt;    return Time.now - self&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;5.minutes.ago&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The syntax is almost perfect! How would you otherwise say "5 minutes ago"? That's what people often talk about when they describe Ruby. "The only truly object oriented language".&lt;br /&gt;&lt;br /&gt;Why Ruby isn't the perfect language, IMO, is because it's too "open". There are too many ways of writing code. Writing smaller applications, or better scripts, won't take you more than a few minutes, but when you need to work on a larger project, I would definitely go with a strictly typed language, such as C#, where you have better control over your code. Also, not surprisingly, no IDE can help you writing your code, as the code is dynamically typed, and you really can't say what functions are available on an object until you actually run the code.&lt;br /&gt;&lt;br /&gt;So, thumb up for this language when working on smaller applications. But for larger projects, I would recommend going for another one.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-1369951050449610040?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/1369951050449610040/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=1369951050449610040' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1369951050449610040'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1369951050449610040'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2009/03/why-you-love-and-hate-ruby.html' title='Why you love and hate Ruby'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-5625007096206572879</id><published>2009-02-26T12:52:00.000-08:00</published><updated>2009-02-26T12:52:00.513-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='FsStory'/><category scheme='http://www.blogger.com/atom/ns#' term='F#'/><category scheme='http://www.blogger.com/atom/ns#' term='bdd'/><title type='text'>Fluent language in FsStory</title><content type='html'>A nice feature for a story runner is to be able to provide arguments in a story sentence, like this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;ATableWithNumberOfLegs 4&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This example is not hard to understand, but there's some mental translation going on, since the order of the words is all screwed up. Let's try another one:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;TheNumberOfLegsOfATableIs 4&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Better, but still not good. First, it's longer than the previous example. Second, even if the grammar is correct, the word order is, well, unusual.. ;)&lt;br /&gt;&lt;br /&gt;What about this?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;ATableWith 4 Legs&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now we're talking!&lt;br /&gt;&lt;br /&gt;Here's a bigger and more complete example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;[&amp;lt;Fact&amp;gt;]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;let tableLegsScenario =&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    given (ATableWith 4 Legs)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt; |&gt; whens (ICutOf 1 Leg)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt; |&gt; thens (ItHasOnly 3 LegsLeft)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Note: As far as I know, RSpec/&lt;a href="http://cukes.info/"&gt;Cucumber&lt;/a&gt; is the only story runner(s) that is able to use variables inside a story sentence.&lt;br /&gt;&lt;br /&gt;The nice thing about this is that &lt;span style="font-style: italic;"&gt;I didn't have to change FsStory itself to make it work&lt;/span&gt;. Not a single line. So, what's the trick?&lt;br /&gt;&lt;br /&gt;If you split up a story sentence like this, you have to prepare the step definition code (the behind-the-scenes code) in a certain way:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;let ATableWith = fun n _ -&gt; ... do something here ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;..or if you prefer, without a lambda:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;let ATableWith n _ = ... do something here ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Then you have to define Legs:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;let Legs = id&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As you see, in this case, it was just a matter of discovering the usage, rather than implementing it in the language. Honestly, I had no idea of this usage when I started to write on FsStory. This is clearly one of the reasons why I like internal DSLs!&lt;br /&gt;&lt;br /&gt;Exercise: How would you implement the step definition for the following story sentence?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;given (ATableWith 4 LegsAnd 2 Chairs)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-5625007096206572879?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/5625007096206572879/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=5625007096206572879' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5625007096206572879'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5625007096206572879'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2009/02/fluent-language-in-fsstory.html' title='Fluent language in FsStory'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-8235590634863553551</id><published>2009-02-22T08:45:00.000-08:00</published><updated>2009-02-22T12:25:13.711-08:00</updated><title type='text'>ImpossibleEstimation and Pomodoro Technique</title><content type='html'>Sometimes when using Pomodoro Technique I find it real difficult to estimate how long a particular activity will take, i.e., when locating a bug or find out why the webserver won't read my files.&lt;br /&gt;&lt;br /&gt;In Pomodoro Technique, every activity should have a time estimate - how many Pomodori I think it will take. Though, this is sometimes impossible! "The problem is solved when I find the bug and since I don't know what the bug is related to, it's impossible to say how much time it will take to find it."&lt;br /&gt;&lt;br /&gt;The solution: instead of just writing a number besides the activity, I use the less-than sign (&lt;) before the number, indicating that I have a time-box for the activity, but that it might take less time. If I'm not done when the time-box is over, I have to ask a colleague to help me or ask my boss for extra resources - thus, escalating my problem. Then, I'm forced to have collected some data of the problem to help them to help me. The nice thing is that I still can have most of the benefits Pomodoro Technique gives me, i.e., increased focus when in a Pomodoro and possibility to get "the whole picture" during my breaks. The latter have proved to be an extra nice thing to have during hard problem-solving.&lt;br /&gt;&lt;br /&gt;And, if a colleague comes to the rescue, we can construct another time-box, to know when to escalate it further, or at least to notify the team or the boss that we have some nasty problems at hand.&lt;br /&gt;&lt;br /&gt;Yet, if I'm collecting metrics of my estimation skills, it is not a very good idea to track data for these bug-fixing Pomodori estimates. Put a "N/A" or a "-" in your records sheet and think for yourself: "Today was an exception, tomorrow will be a bug-free day." And don't forget to do your &lt;a href="http://blog.staffannoteberg.com/2008/02/02/daily-mind-map/"&gt;daily mind-map&lt;/a&gt; before you leave for home.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-8235590634863553551?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/8235590634863553551/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=8235590634863553551' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/8235590634863553551'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/8235590634863553551'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2009/02/impossibleestimation-and-pomodoro.html' title='ImpossibleEstimation and Pomodoro Technique'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-4297024923170143673</id><published>2009-02-09T04:00:00.000-08:00</published><updated>2009-02-11T00:30:25.412-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='FsStory'/><category scheme='http://www.blogger.com/atom/ns#' term='F#'/><category scheme='http://www.blogger.com/atom/ns#' term='bdd'/><title type='text'>[Announce] FsStory, executable stories in F#</title><content type='html'>Since &lt;a href="http://www.claudioperrone.com/blog/articles/2009/01/11/agile-tales-video-from-%C3%B8redev-2008/"&gt;Claudio Perrone's talk&lt;/a&gt; on Øredev, I have been thinking about what his &lt;a href="http://www.claudioperrone.com/blog/articles/2008/11/09/misbehave-01-oslos-mgrammar-for-bdd-executable-specifications/"&gt;MisBehave &lt;/a&gt;would look like in F#. In his talk, Claudio also mentioned &lt;a href="http://cukes.info/"&gt;Cucumber&lt;/a&gt;, a story runner written in Ruby. My plan was to make a lightweight DSL for writing stories in F# code, with the story parts separated from the implementation parts.&lt;br /&gt;&lt;br /&gt;Currently, FsStory enables the developer to write user story &lt;span style="font-style: italic;"&gt;scenarios &lt;/span&gt;(in  Given/When/Then form) in F# code, like this:&lt;br /&gt;&lt;pre&gt;#light&lt;br /&gt;&lt;br /&gt;open FsStoryRunner&lt;br /&gt;open MutatedTurtleMovesImpl&lt;br /&gt;open Xunit&lt;br /&gt;&lt;br /&gt;(*&lt;br /&gt;In order to impress my friends&lt;br /&gt;As a .NET programmer&lt;br /&gt;I want to draw funny fractal pictures&lt;br /&gt;*)&lt;br /&gt;&lt;br /&gt;[&amp;lt;fact&amp;gt;]&lt;br /&gt;let MoveTurtleToPosition() =&lt;br /&gt; given ATurtle&lt;br /&gt; |&amp;gt; andGiven IsRotated90DegreesToTheRight&lt;br /&gt; |&amp;gt; whens (TurtleWalksSteps 9)&lt;br /&gt; |&amp;gt; thens (TurtleIsLocatedAt (0,9))&lt;br /&gt; |&amp;gt; endStory&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Did you notice [&amp;lt;fact&amp;gt;] attribute just before the function  definition? It is a xUnit.net specific attribute, telling xUnit.net that the  function is a runnable test. So, why xUnit.net? Answer: xUnit.net is the  currently the only test framework that runs static test methods, which is what F# functions compiles to.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;Note: If you  think that the story above is too low level to be a "good" user story, you're right, but it's just an example..&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;The "ATurtle", "IsRotated90DegreesToTheRight",  "TurtleWalksSteps", etc, are &lt;i&gt;functions&lt;/i&gt; that you have to implement  yourself. What these functions do is not FsStory's business, &lt;i&gt;except&lt;/i&gt; that  they have the same type. It's a good thing to think about this in  advance.&lt;br /&gt;&lt;br /&gt;If you're testing something object oriented, i.e. a C# project,  then you're probably have to let the functions have the type &lt;b&gt;() -&gt; ()&lt;/b&gt;.  That is, they take no argument and return void, in C# lingo. You'd also need a  mutable variable to accomplish this.&lt;br /&gt;&lt;pre&gt;#light&lt;br /&gt;&lt;br /&gt;open Turtle&lt;br /&gt;open FsxUnit.Syntax&lt;br /&gt;&lt;br /&gt;let mutable turtle = new Turtle() // turtle must have type Turtle&lt;br /&gt;&lt;br /&gt;let ATurtle () = turtle &lt;- new Turtle()    // For reuse in same story let MovesOneStepForward () = turtle.Go() let IsMovedOneStepForward () = turtle.Position.X |&amp;gt; should equal 1 let RotationIs angle () = turtle.Direction |&amp;gt; should equal 0.0  &lt;/pre&gt;&lt;strike&gt;It's up to the developer what library she wants to use for her assertions. In this example, &lt;a href="http://www.codeplex.com/FsTest"&gt;FsTest&lt;/a&gt; was used, but she could go for &lt;a href="http://www.nunit.org/index.php"&gt;NUnit &lt;/a&gt;or &lt;a href="http://nbehave.org/"&gt;NBehave &lt;/a&gt;or something else.&lt;/strike&gt; I hadn't actually tried this and do not longer think this will work. Either use xUnit.net or FsTest (which is based on xUnit.net).&lt;br /&gt;&lt;br /&gt;Another style is to work with &lt;i&gt;immutable  objects&lt;/i&gt;. One example of immutable objects are value objects, in &lt;a href="http://en.wikipedia.org/wiki/Domain-driven_design"&gt;DDD&lt;/a&gt;.  Immutable objects correspond well to functional programming principles. Here is  an example of an implementation of a scenario when an immutable object is used  in the SUT (System Under Test).&lt;br /&gt;&lt;pre&gt;let ATurtle () = new TurtleImmutable()&lt;br /&gt;let IsRotated90DegreesToTheRight = fun (turtle : TurtleImmutable) -&amp;gt; turtle.Left()&lt;br /&gt;let TurtleWalksSteps steps = fun (turtle : TurtleImmutable) -&amp;gt; turtle.GoSteps(steps)&lt;br /&gt;let TurtleIsLocatedAt (x,y) = fun (turtle : TurtleImmutable) -&amp;gt; turtle.Position |&amp;gt;&lt;br /&gt;                  should equal (new Position(x,y)) ; turtle&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;To clarify, all methods on the (immutable) turtle return a new turtle and  that turtle is returned and then passed in as an argument to the next test  function (by FsStory). As you might have spotted, the example uses a lambda, an  anonymous functions (the "fun") instead of specifying an argument explicitly. It's a good thing to get a &lt;i&gt;running&lt;/i&gt; story before  actually implementing the logic and assertions. Using the function "id" (just  returning the argument) on the right-hand side is very helpful for getting  everything to run.&lt;br /&gt;&lt;br /&gt;You can find FsStory at &lt;a href="http://www.codeplex.com/fsstory"&gt;http://www.codeplex.com/fsstory&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-4297024923170143673?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/4297024923170143673/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=4297024923170143673' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/4297024923170143673'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/4297024923170143673'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2009/02/announce-fsstory-executable-stories-in.html' title='[Announce] FsStory, executable stories in F#'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-3036713073334081713</id><published>2009-02-06T07:38:00.000-08:00</published><updated>2009-02-06T10:10:58.459-08:00</updated><title type='text'>Trying out ruby for the very first time</title><content type='html'>So, I've made up my mind. The language of this year is going to be &lt;a href="http://www.ruby-lang.org/en/"&gt;Ruby&lt;/a&gt;! The reasons are many, but mostly I feel I can use this language more in my daily work, and on a podcast over at &lt;a href="http://altnetpodcast.com/episodes/13-ruby-on-rails"&gt;AltDotNet about Ruby&lt;/a&gt;, they mentioned something like "learning ruby will make you a better C# developer". Languages like lisp and python will have to wait for at least one year!&lt;br /&gt;&lt;br /&gt;About two weeks ago, I was out skiing with Gustaf and some friends of him. We ended up playing a lot of &lt;a href="http://en.wikipedia.org/wiki/Four_in_a_row"&gt;four in a row&lt;/a&gt;, a really funny and simple game. This game made me think about how to develop a &lt;a href="http://ai-depot.com/articles/minimax-explained/"&gt;min max AI&lt;/a&gt;, and suddenly I had written down the pseudo code for such an AI in my notebook. Here is a picture of the game is supposed to be played (from wikipedia):&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Connect_four_game.svg/250px-Connect_four_game.svg.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 250px; height: 214px;" src="http://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Connect_four_game.svg/250px-Connect_four_game.svg.png" alt="" border="0" /&gt;&lt;/a&gt;Now, back home at my computer, I started digging into the ruby documentations, and after not more than a few hours I had finished the first version of the AI! Turns out Ruby isn't that hard to learn, at least not if you just want to get some basic things done. It's like writing c#, except you skip the types, and instead of writing void foo(int x) { ... } you write def foo(x) ... end, and so on...&lt;br /&gt;&lt;br /&gt;Here is how my final version of the game looks when you run it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;C:\Documents and Settings\Christian\Desktop&gt;ruby 4irad.rb&lt;br /&gt;Run against ai (A), another player (P) or let two AIs play against eachother (X)&lt;br /&gt;A&lt;br /&gt;Enter AI 1 level (1 to 5):&lt;br /&gt;3&lt;br /&gt;Enter name of human player (default Human 1):&lt;br /&gt;&lt;br /&gt;---------------&lt;br /&gt;|0 1 2 3 4 5 6|&lt;br /&gt;---------------&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;---------------&lt;br /&gt;AI 1 (3) played: 3&lt;br /&gt;---------------&lt;br /&gt;|0 1 2 3 4 5 6|&lt;br /&gt;---------------&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | |O| | | |&lt;br /&gt;---------------&lt;br /&gt;Human 1, make your move:&lt;br /&gt;2&lt;br /&gt;Human 1 played: 2&lt;br /&gt;---------------&lt;br /&gt;|0 1 2 3 4 5 6|&lt;br /&gt;---------------&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | |X|O| | | |&lt;br /&gt;---------------&lt;br /&gt;AI 1 (3) played: 3&lt;br /&gt;---------------&lt;br /&gt;|0 1 2 3 4 5 6|&lt;br /&gt;---------------&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | | | | | |&lt;br /&gt;| | | |O| | | |&lt;br /&gt;| | |X|O| | | |&lt;br /&gt;---------------&lt;br /&gt;Human 1, make your move:&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;and so on...&lt;br /&gt;&lt;br /&gt;I wonder how much code can you post in a blog, without the post becoming too long? I would like to post the code as an attachment, but I'm not sure you can do that in blogger, so I'll simply put it here. To try it out, copy the code to a file, i.e. 4inarow.rb, then run it using &lt;i&gt;ruby 4inarow.rb&lt;/i&gt;. Enjoy ;)&lt;br /&gt;&lt;pre name="code" class="ruby"&gt;&lt;br /&gt;$debugEnabled = false&lt;br /&gt;def debug(text)&lt;br /&gt; if $debugEnabled then&lt;br /&gt;  puts &amp;quot;&amp;gt;&amp;gt;&amp;gt; &amp;quot; + text&lt;br /&gt; end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class State&lt;br /&gt;&lt;br /&gt; attr_reader :cols, :rows, :player, :winner, :lastPlayed, :isFull&lt;br /&gt; &lt;br /&gt; Player1 = &amp;quot;Player 1&amp;quot;&lt;br /&gt; Player2 = &amp;quot;Player 2&amp;quot;&lt;br /&gt;&lt;br /&gt; def initialize&lt;br /&gt;  @cols = []&lt;br /&gt;  (0..6).each { @cols &amp;lt;&amp;lt; [] }&lt;br /&gt;  @rows = []&lt;br /&gt;  (0..5).each { @rows &amp;lt;&amp;lt; Array.new(7, nil) }&lt;br /&gt;  @player = Player1&lt;br /&gt;  @winner = nil&lt;br /&gt;  @isFull = false&lt;br /&gt; end&lt;br /&gt; &lt;br /&gt; def deep_copy&lt;br /&gt;  return Marshal.load( Marshal.dump( self ) )&lt;br /&gt; end&lt;br /&gt; &lt;br /&gt; def to_s&lt;br /&gt;  return &amp;quot;---------------\n&amp;quot; + &lt;br /&gt;   &amp;quot;|&amp;quot; + (0..6).to_a.join(&amp;quot; &amp;quot;) + &amp;quot;|\n&amp;quot; +&lt;br /&gt;   &amp;quot;---------------\n&amp;quot; +&lt;br /&gt;   @rows.reverse.collect{|row| &amp;quot;|&amp;quot; + row.collect{|r| if !r then &amp;quot; &amp;quot; elsif r == Player2 then &amp;quot;X&amp;quot; else &amp;quot;O&amp;quot; end}.join(&amp;quot;|&amp;quot;) + &amp;quot;|&amp;quot;}.join(&amp;quot;\n&amp;quot;) + &amp;quot;\n&amp;quot; +&lt;br /&gt;   &amp;quot;---------------&amp;quot;&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def canPlay( i )&lt;br /&gt;  return @winner == nil &amp;amp;&amp;amp; @cols[i].length &amp;lt; 6&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def play( i )&lt;br /&gt;  if canPlay i then&lt;br /&gt;   rowIndex = @cols[i].length&lt;br /&gt;   @cols[i] &amp;lt;&amp;lt; @player&lt;br /&gt;   @rows[rowIndex][i] = @player&lt;br /&gt;   @lastPlayed = i&lt;br /&gt;&lt;br /&gt;   switchPlayer&lt;br /&gt;   updateWinner&lt;br /&gt;   updateIsFull&lt;br /&gt;  end&lt;br /&gt; end&lt;br /&gt; &lt;br /&gt; def updateIsFull&lt;br /&gt;  for p in @rows[5]&lt;br /&gt;   if !p then return end&lt;br /&gt;  end&lt;br /&gt;  @isFull = true&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def findWinner( row )&lt;br /&gt;  for i in 0..(row.length-4)&lt;br /&gt;   p = nil&lt;br /&gt;   n = 0&lt;br /&gt;   for j in 0..3&lt;br /&gt;    rp = getPlayed(row[i+j])&lt;br /&gt;    if rp.class != Fixnum then&lt;br /&gt;     if !p then&lt;br /&gt;      p = rp&lt;br /&gt;     elsif p != rp&lt;br /&gt;      p = 0&lt;br /&gt;      break&lt;br /&gt;     end&lt;br /&gt;     if rp != 0 then&lt;br /&gt;      n += 1&lt;br /&gt;     end&lt;br /&gt;    end&lt;br /&gt;   end&lt;br /&gt;   if p != 0 and n == 4 then&lt;br /&gt;    return p&lt;br /&gt;   end&lt;br /&gt;  end&lt;br /&gt;  return nil&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def updateWinner&lt;br /&gt;  for row in getAllRanges&lt;br /&gt;   p = findWinner row&lt;br /&gt;   if p then&lt;br /&gt;    @winner = p&lt;br /&gt;   end&lt;br /&gt;  end&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def switchPlayer&lt;br /&gt;  if @player == Player1 then&lt;br /&gt;   @player = Player2&lt;br /&gt;  else&lt;br /&gt;   @player = Player1&lt;br /&gt;  end&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def getPlayed(cell)&lt;br /&gt;  c = cell[0]&lt;br /&gt;  r = cell[1]&lt;br /&gt;  col = @cols[c]&lt;br /&gt;  if r &amp;gt;= col.length then&lt;br /&gt;   return r - col.length + 1&lt;br /&gt;  end&lt;br /&gt;  return @cols[c][r]&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def posValid( c, r )&lt;br /&gt;  return c &amp;gt;= 0 &amp;amp;&amp;amp; c &amp;lt;= 6 &amp;amp;&amp;amp; r &amp;gt;= 0 &amp;amp;&amp;amp; r &amp;lt;= 5&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def getDiagonal( c, r, dc )&lt;br /&gt;  d = []&lt;br /&gt;  while posValid( c, r )&lt;br /&gt;   d &amp;lt;&amp;lt; [c, r] &lt;br /&gt;   c += dc&lt;br /&gt;   r += 1&lt;br /&gt;  end&lt;br /&gt;  return d&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def getAllDiagonalsRanges&lt;br /&gt;  diags = []&lt;br /&gt;  for r in 0..5&lt;br /&gt;   diags &amp;lt;&amp;lt; getDiagonal( 0, r, 1 )&lt;br /&gt;  end&lt;br /&gt;  for c in 1..6&lt;br /&gt;   diags &amp;lt;&amp;lt; getDiagonal( c, 0, 1 )&lt;br /&gt;  end&lt;br /&gt;  for r in 0..5&lt;br /&gt;   diags &amp;lt;&amp;lt; getDiagonal( 6, r, -1 )&lt;br /&gt;  end&lt;br /&gt;  for c in 0..5&lt;br /&gt;   diags &amp;lt;&amp;lt; getDiagonal( c, 0, -1 )&lt;br /&gt;  end&lt;br /&gt;  return diags&lt;br /&gt; end&lt;br /&gt; &lt;br /&gt; def getAllColsRanges&lt;br /&gt;  return (0..6).collect{|c| (0..5).collect{|r| [c, r]}}&lt;br /&gt; end&lt;br /&gt; def getAllRowsRanges&lt;br /&gt;  return (0..5).collect{|r| (0..6).collect{|c| [c, r]}}&lt;br /&gt; end&lt;br /&gt; &lt;br /&gt; def getAllRanges&lt;br /&gt;  return  getAllColsRanges + &lt;br /&gt;    getAllRowsRanges + &lt;br /&gt;    getAllDiagonalsRanges&lt;br /&gt; end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class Node&lt;br /&gt;&lt;br /&gt; attr_reader :state, :children, :isLeaf&lt;br /&gt;&lt;br /&gt; def initialize( state, player )&lt;br /&gt;  if !player then&lt;br /&gt;   raise &amp;quot;player is nil&amp;quot;&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  @state = state&lt;br /&gt;  @isLeaf = true&lt;br /&gt;  @children = {}&lt;br /&gt;  @player = player&lt;br /&gt; end&lt;br /&gt; &lt;br /&gt; def getPoints( row )&lt;br /&gt;  points = 0.0&lt;br /&gt;  for i in 0..(row.length-4)&lt;br /&gt;   p = nil&lt;br /&gt;   n = 0&lt;br /&gt;   divider = 1&lt;br /&gt;   for j in 0..3&lt;br /&gt;    rp = @state.getPlayed(row[i+j])&lt;br /&gt;    if rp.class == Fixnum then&lt;br /&gt;     divider *= rp&lt;br /&gt;    else&lt;br /&gt;     if !p then&lt;br /&gt;      p = rp&lt;br /&gt;     elsif !rp then&lt;br /&gt;     &lt;br /&gt;     elsif p != rp&lt;br /&gt;      p = nil&lt;br /&gt;      break&lt;br /&gt;     end&lt;br /&gt;     if rp then&lt;br /&gt;      n += 1&lt;br /&gt;     end&lt;br /&gt;    end&lt;br /&gt;   end&lt;br /&gt;   if p then&lt;br /&gt;    blockPoints = calculatePoints n / divider&lt;br /&gt;    if p == @player then&lt;br /&gt;     points += blockPoints&lt;br /&gt;    else&lt;br /&gt;     points = points - blockPoints&lt;br /&gt;    end&lt;br /&gt;   end&lt;br /&gt;  end&lt;br /&gt;  if points.infinite? then&lt;br /&gt;   debug &amp;quot;Found winner: &amp;quot; + points.to_s + &amp;quot;: &amp;quot; + row.join(&amp;quot;|&amp;quot;)&lt;br /&gt;  end&lt;br /&gt;  return points&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def calculatePoints( n )&lt;br /&gt;  return n.to_f**3 / (4 - [n,4].min)&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def nodePoints&lt;br /&gt;  points = 0.0&lt;br /&gt;  for row in state.getAllRanges&lt;br /&gt;   p = getPoints(row)&lt;br /&gt;   if p != 0 then&lt;br /&gt;    #debug row.collect{|r| if r then r else &amp;quot; &amp;quot; end}.join(&amp;quot;|&amp;quot;) + &amp;quot; &amp;quot; + p.to_s&lt;br /&gt;   end&lt;br /&gt;   &lt;br /&gt;   newPoints = points + p&lt;br /&gt;   if newPoints.nan? then&lt;br /&gt;    raise &amp;quot;points + p == NaN: points=&amp;quot; + points.to_s + &amp;quot; p=&amp;quot; + p.to_s&lt;br /&gt;   end&lt;br /&gt;   points = newPoints&lt;br /&gt;  end&lt;br /&gt;  #puts state&lt;br /&gt;  #puts &amp;quot;Points: &amp;quot; + points.to_s&lt;br /&gt;  return points&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def allChildPoints&lt;br /&gt;  return children.values.collect {|child| child.totalPoints}&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def isMyTurn&lt;br /&gt;  return state.player == @player&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def totalPoints&lt;br /&gt;  if @totalPoints then&lt;br /&gt;   return @totalPoints&lt;br /&gt;  end&lt;br /&gt;  p = nil&lt;br /&gt;  if @isLeaf then&lt;br /&gt;   p = nodePoints&lt;br /&gt;  else&lt;br /&gt;   if isMyTurn then&lt;br /&gt;    p = allChildPoints.max&lt;br /&gt;   else&lt;br /&gt;    p = allChildPoints.min&lt;br /&gt;   end&lt;br /&gt;  end&lt;br /&gt;  if !p then&lt;br /&gt;   raise &amp;quot;nil points returned&amp;quot;&lt;br /&gt;  end&lt;br /&gt;  @totalPoints = p&lt;br /&gt;  return p&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def grow&lt;br /&gt;  if @totalPoints then&lt;br /&gt;   #puts &amp;quot;Gah, totalPoints is set&amp;quot;&lt;br /&gt;   @totalPoints = nil&lt;br /&gt;  end&lt;br /&gt;  if @isLeaf &amp;amp;&amp;amp; !@state.winner&lt;br /&gt;   #debug &amp;quot;Growing leaf: &amp;quot; + self.to_s&lt;br /&gt;   for i in 0..6&lt;br /&gt;    if @state.canPlay i then&lt;br /&gt;     childState = @state.deep_copy&lt;br /&gt;     childState.play i&lt;br /&gt;     child = Node.new(childState, @player)&lt;br /&gt;     children[i] = child&lt;br /&gt;    end&lt;br /&gt;   end&lt;br /&gt;   @isLeaf = false&lt;br /&gt;  else&lt;br /&gt;   for child in @children.values&lt;br /&gt;    child.grow&lt;br /&gt;   end&lt;br /&gt;  end&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def findNodeWithState( state )&lt;br /&gt;  if state.lastPlayed then&lt;br /&gt;   return @children[state.lastPlayed]&lt;br /&gt;  else&lt;br /&gt;   return nil&lt;br /&gt;  end&lt;br /&gt; end&lt;br /&gt; &lt;br /&gt; def to_s&lt;br /&gt;  return &amp;quot;Played: &amp;quot; + @state.lastPlayed.to_s + &amp;quot; Points: &amp;quot; + totalPoints.to_s&lt;br /&gt; end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class AIPlayer&lt;br /&gt;&lt;br /&gt; def initialize( level, name )&lt;br /&gt;  @level = level&lt;br /&gt;  @name = name&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def to_s&lt;br /&gt;  return @name + &amp;quot; (&amp;quot; + @level.to_s + &amp;quot;)&amp;quot;&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def selectBestNode&lt;br /&gt;  # Add some randomness&lt;br /&gt;  bestChild = nil&lt;br /&gt;  bestChildCount = nil&lt;br /&gt;  for child in @node.children.values&lt;br /&gt;   #puts child.state&lt;br /&gt;   #puts child.totalPoints&lt;br /&gt;   if !bestChild then&lt;br /&gt;    bestChild = child&lt;br /&gt;    bestChildCount = 1&lt;br /&gt;   else&lt;br /&gt;    if child.totalPoints &amp;gt; bestChild.totalPoints then&lt;br /&gt;     bestChild = child&lt;br /&gt;     bestChildCount = 1&lt;br /&gt;    elsif child.totalPoints == bestChild.totalPoints then&lt;br /&gt;     bestChildCount = bestChildCount + 1&lt;br /&gt;     if rand(bestChildCount) == 0 then&lt;br /&gt;      bestChild = child&lt;br /&gt;     end&lt;br /&gt;    end&lt;br /&gt;   end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  #Just to make sure&lt;br /&gt;  if !bestChild then&lt;br /&gt;   raise &amp;quot;bestChild is nil&amp;quot;&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  debug &amp;quot;Best points: &amp;quot; + bestChild.totalPoints.to_s&lt;br /&gt;  &lt;br /&gt;  &lt;br /&gt;  @node = bestChild&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def play(state)&lt;br /&gt;  if state.lastPlayed then&lt;br /&gt;   debug &amp;quot;Finding last played node&amp;quot;&lt;br /&gt;   @node = @node.findNodeWithState(state)&lt;br /&gt;   @node.grow&lt;br /&gt;  end&lt;br /&gt;  debug &amp;quot;Finding the best node&amp;quot;&lt;br /&gt;  selectBestNode&lt;br /&gt;  @node.grow&lt;br /&gt;  state.play @node.state.lastPlayed&lt;br /&gt; end&lt;br /&gt; &lt;br /&gt; def setupGame(state, player)&lt;br /&gt;  @node = Node.new(state, player)&lt;br /&gt;  debug &amp;quot;Creating min max tree (&amp;quot; + (7**@level).to_s + &amp;quot; nodes)...&amp;quot;&lt;br /&gt;  (1..@level).each{ @node.grow } &lt;br /&gt; end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class HumanPlayer&lt;br /&gt;&lt;br /&gt; def initialize(name)&lt;br /&gt;  @name = name&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def to_s&lt;br /&gt;  return @name&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; def play(state)&lt;br /&gt;  puts @name + &amp;quot;, make your move:&amp;quot;&lt;br /&gt;  &lt;br /&gt;  begin&lt;br /&gt;   toPlay = gets&lt;br /&gt;   if toPlay == &amp;quot;q\n&amp;quot; then&lt;br /&gt;    exit&lt;br /&gt;   end&lt;br /&gt;   toPlay = toPlay.to_i&lt;br /&gt;   if !state.canPlay toPlay&lt;br /&gt;    puts &amp;quot;Can't play &amp;quot; + toPlay.to_s + &amp;quot;. Select another:&amp;quot;&lt;br /&gt;    selectOther = true&lt;br /&gt;   else&lt;br /&gt;    state.play toPlay&lt;br /&gt;   end&lt;br /&gt;  end while selectOther&lt;br /&gt; end&lt;br /&gt; &lt;br /&gt; def setupGame(state, player)&lt;br /&gt;  &lt;br /&gt; end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def runGame&lt;br /&gt;&lt;br /&gt; puts &amp;quot;Run against ai (A), another player (P) or let two AIs play against eachother (X)?&amp;quot;&lt;br /&gt; gameType = readline.strip&lt;br /&gt; case gameType&lt;br /&gt;  when &amp;quot;A&amp;quot;, &amp;quot;a&amp;quot;&lt;br /&gt;   player1 = createAIPlayer&lt;br /&gt;   player2 = createHumanPlayer&lt;br /&gt;   &lt;br /&gt;  when &amp;quot;P&amp;quot;, &amp;quot;p&amp;quot;&lt;br /&gt;   player1 = createHumanPlayer&lt;br /&gt;   player2 = createHumanPlayer&lt;br /&gt;   &lt;br /&gt;  when &amp;quot;X&amp;quot;, &amp;quot;x&amp;quot;&lt;br /&gt;   player1 = createAIPlayer&lt;br /&gt;   player2 = createAIPlayer&lt;br /&gt;   &lt;br /&gt;  else&lt;br /&gt;   puts &amp;quot;Invalid input: #{gameType}&amp;quot;&lt;br /&gt;   exit&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; players = [player1, player2]&lt;br /&gt; state = State.new&lt;br /&gt; &lt;br /&gt; player1.setupGame(state, State::Player1)&lt;br /&gt; player2.setupGame(state, State::Player2)&lt;br /&gt;&lt;br /&gt; while (!state.winner &amp;amp;&amp;amp; !state.isFull)&lt;br /&gt;  for player in players&lt;br /&gt;   puts state&lt;br /&gt;   player.play(state)&lt;br /&gt;   puts player.to_s + &amp;quot; played: &amp;quot; + state.lastPlayed.to_s&lt;br /&gt;   if state.winner then break end&lt;br /&gt;  end&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt; if state.winner&lt;br /&gt;  puts state&lt;br /&gt;  puts &amp;quot;Winner: &amp;quot; + state.winner.to_s&lt;br /&gt; elsif state.isFull&lt;br /&gt;  puts state&lt;br /&gt;  puts &amp;quot;Draw!&amp;quot;&lt;br /&gt; end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;$aiCounter = 0&lt;br /&gt;def createAIPlayer&lt;br /&gt; $aiCounter = $aiCounter + 1&lt;br /&gt; name = &amp;quot;AI &amp;quot; + $aiCounter.to_s&lt;br /&gt; puts &amp;quot;Enter &amp;quot; + name + &amp;quot; level (1 to 5):&amp;quot;&lt;br /&gt; level = readline.to_i&lt;br /&gt; return AIPlayer.new(level, name)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;$humanCounter = 0&lt;br /&gt;def createHumanPlayer&lt;br /&gt; $humanCounter = $humanCounter + 1&lt;br /&gt; defaultName = &amp;quot;Human &amp;quot; + $humanCounter.to_s&lt;br /&gt; puts &amp;quot;Enter name of human player (default &amp;quot; + defaultName + &amp;quot;):&amp;quot;&lt;br /&gt; name = readline.strip&lt;br /&gt; if !name || name == &amp;quot;&amp;quot; then&lt;br /&gt;  name = defaultName&lt;br /&gt; end&lt;br /&gt; return HumanPlayer.new(name)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def runTests&lt;br /&gt;&lt;br /&gt; state = State.new&lt;br /&gt; &lt;br /&gt; state.play(3)&lt;br /&gt; state.play(3)&lt;br /&gt; state.play(4)&lt;br /&gt; state.play(4)&lt;br /&gt; state.play(5)&lt;br /&gt; state.play(5)&lt;br /&gt; state.play(6)&lt;br /&gt;&lt;br /&gt; #test getPlayed&lt;br /&gt; if state.getPlayed([3, 0]) != State::Player1 then raise &amp;quot;getPlayed doesn't work&amp;quot; end&lt;br /&gt; if state.getPlayed([3, 1]) != State::Player2 then raise &amp;quot;getPlayed doesn't work&amp;quot; end&lt;br /&gt; if state.getPlayed([3, 2]) != 1 then raise &amp;quot;getPlayed doesn't work&amp;quot; end&lt;br /&gt; if state.getPlayed([3, 2]).class != Fixnum then raise &amp;quot;getPlayed doesn't work&amp;quot; end&lt;br /&gt;&lt;br /&gt; #test findWinner&lt;br /&gt; winningRow = (3..6).collect{|c| [c, 0]}&lt;br /&gt; if !state.findWinner(winningRow) then raise &amp;quot;findWinner doesn't work. winningRow=&amp;quot; + winningRow.to_s end&lt;br /&gt; &lt;br /&gt; #test state.winner&lt;br /&gt; if state.winner == nil then raise &amp;quot;winner should be set&amp;quot; end&lt;br /&gt;  &lt;br /&gt; puts &amp;quot;All tests ok&amp;quot;&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;runGame&lt;br /&gt;#runTests&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-3036713073334081713?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/3036713073334081713/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=3036713073334081713' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/3036713073334081713'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/3036713073334081713'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2009/02/trying-out-ruby-for-very-first-time.html' title='Trying out ruby for the very first time'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-471097246752250019</id><published>2009-02-02T23:51:00.000-08:00</published><updated>2009-02-10T13:55:22.109-08:00</updated><title type='text'>BDD using NBehave + Rhino Mocks AMC</title><content type='html'>&lt;span style="font-weight:bold;"&gt;Update&lt;/span&gt;: I found &lt;a href="http://blog.aslakhellesoy.com/2006/12/11/the-bdd-cargo-cult"&gt;an old blog post by Aslak Hellesøy&lt;/a&gt; (the main developer behind &lt;a href="http://cukes.info/"&gt;Cucumber&lt;/a&gt;) that touches on this subject.&lt;div&gt;&lt;br /&gt;Some time ago, I investigated what &lt;a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development"&gt;BDD &lt;/a&gt;is all about. In essence, it's TDD with a twist. For example, the word "test" implies that we're testing what someone (we?) already made, but TDD says we're going to write the tests &lt;span style="font-style: italic;"&gt;before &lt;/span&gt;the actual implementation of the unit of code. Somewhat, the word "test" has a direction backwards, while "should" has a direction forward. Hence, "should" is more comprehensive to use when describing and specifying the future. Makes sense?&lt;br /&gt;&lt;br /&gt;What I'd like to show you is a piece of code I wrote to see how &lt;a href="http://nbehave.org/"&gt;NBehave&lt;/a&gt;'s story runner could work with &lt;a href="http://ayende.com/projects/rhino-mocks.aspx"&gt;Rhino Mocks&lt;/a&gt;' Auto Mocking Container[&lt;a href="http://blog.eleutian.com/CommentView,guid,762249da-e25a-4503-8f20-c6d59b1a69bc.aspx"&gt;1&lt;/a&gt;, &lt;a href="http://ayende.com/Blog/archive/2007/06/08/The-Auto-Mocking-Container.aspx"&gt;2&lt;/a&gt;].&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c#"&gt;[Story, Test]&lt;br /&gt;public void GetMoneyWhenPassGO()&lt;br /&gt;{&lt;br /&gt; // Set up and initialize&lt;br /&gt; var mocks = new MockRepository();&lt;br /&gt; var container = new Rhino.Testing.AutoMocking.AutoMockingContainer(mocks);&lt;br /&gt; container.Initialize();&lt;br /&gt;&lt;br /&gt; // Resolve and obtain references&lt;br /&gt; IPlayerTurn turn = container.Create&amp;lt;PlayerTurn&amp;gt;();&lt;br /&gt; IBoard board = container.Resolve&amp;lt;IBoard&amp;gt;();&lt;br /&gt; IPlayer player = container.Resolve&amp;lt;IPlayer&amp;gt;();&lt;br /&gt; turn.AddPlayers(new List&amp;lt;Player&amp;gt;() {player});&lt;br /&gt;&lt;br /&gt; // Story begins here&lt;br /&gt; var story = new Story("Player recieves money when passes 'GO'");&lt;br /&gt;&lt;br /&gt; story&lt;br /&gt;     .AsA("Player")&lt;br /&gt;     .IWant("to recieve money when I pass 'GO'")&lt;br /&gt;     .SoThat("I can buy things that generate money");&lt;br /&gt;&lt;br /&gt; story&lt;br /&gt;     .WithScenario("Normal play scenario")&lt;br /&gt;     .Given("A board with 4 squares", () =&gt; Expect.Call(board.NumberOfSquares).Return(4))&lt;br /&gt;     .And("a player near 'GO'", () =&gt; Expect.Call(board.GetIndexForPlayer(player)).Return(2))     &lt;br /&gt;     .And("mockery has started", () =&gt; mocks.ReplayAll())&lt;br /&gt;     .When("player passes 'GO'",() =&gt; turn.PlayerSequence(player, 3))&lt;br /&gt;     .Then("the player earns $4000", () =&gt; player.AssertWasCalled(x =&gt; x.Credit(4000)));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;When this test is run with ReSharper, the test passes and outputs the story with indentation. Nice! (Except the "mockery has started" part of the story..)&lt;br /&gt;&lt;br /&gt;Now a question pops up: since we have only specified and tested the first (top-level) interaction, what about the rest of the interactions? I think this is a good question that leads us further down the rabbit hole.&lt;br /&gt;&lt;br /&gt;A written user story comes from a dialogue with a person with domain knowledge. Hopefully, after some discussion, we have understood some of the moving parts of the domain problem the customer wants us to solve. Let's assume that we want to implement a single feature at a time, a couple of questions arise: should we start top-down or bottom-up? And how far "up" should we go, i.e. should we start with the UI or the domain model, if we choose a top-down approach?&lt;br /&gt;&lt;br /&gt;If we choose to start with the domain model, then I think the above way of specifying the behavior looks nice. The key question is where the classes in the story come from originally. I have no easy answer for that. Of course they should originate from the domain problem, but how? &lt;a href="http://deepfriedbytes.com/podcast/episode-6-talking-domain-driven-design-with-david-laribee-part-1/"&gt;"The model is the code - the code is the model"&lt;/a&gt;, but it probably takes a while to "get it right". Maybe code like the above could help us to see if we have understood the problem in the first place?&lt;br /&gt;&lt;br /&gt;Now back to the question: "what about the rest of the interactions"? We have ensured that the class which is in "the center" of the particular interaction chain (the player turn) lives in a faked world (a small board and a player near go) and we finally assert that when something happens (player passes 'GO') then some state has changed (the player gets money). The nice thing is that we now know more about what functionality the dependent classes should provide. For example, IBoard needs to have a method &lt;span style="font-style: italic;"&gt;GetIndexForPlayer&lt;/span&gt; and if there is a class implementing that interface, then a &lt;span style="font-style: italic;"&gt;NotImplementedException&lt;/span&gt; is probably thrown from that method, in order to compile. Next step could be to start thinking on that particular method and choose to either write a mocked unit test or an "ordinary" unit test.&lt;br /&gt;&lt;br /&gt;Of course, real acceptance tests are also needed, but the purpose and scope of those tests are quite different. At least that's what &lt;span style="font-style: italic;"&gt;I&lt;/span&gt; think.&lt;br /&gt;&lt;br /&gt;What do &lt;span style="font-style: italic;"&gt;you &lt;/span&gt;think?&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-471097246752250019?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/471097246752250019/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=471097246752250019' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/471097246752250019'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/471097246752250019'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2009/02/bdd-using-nbehave-rhino-mocks-amc.html' title='BDD using NBehave + Rhino Mocks AMC'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-5732742218549856300</id><published>2009-01-16T00:05:00.000-08:00</published><updated>2009-01-16T05:44:27.674-08:00</updated><title type='text'>The small things: Fisher Space Pen</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_lKbnbukR-QY/SXBZAPFrC4I/AAAAAAAAACM/7ge1kcwXC4M/s1600-h/moleskine_fisher.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 240px;" src="http://2.bp.blogspot.com/_lKbnbukR-QY/SXBZAPFrC4I/AAAAAAAAACM/7ge1kcwXC4M/s320/moleskine_fisher.png" alt="" id="BLOGGER_PHOTO_ID_5291827422745529218" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;I got this pen from my girlfriend as a christmas gift. It's a &lt;a href="http://en.wikipedia.org/wiki/Space_Pen"&gt;Fisher Space Pen&lt;/a&gt; and I'm very happy for it!&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;I believe that it's really worth investing in the cheap stuff you use daily, like pen and paper (though, in this case my girlfriend did the investment, but that's another story). Of course, that's assuming that you &lt;span style="font-style: italic;"&gt;use &lt;/span&gt;pen and paper. You &lt;span style="font-style: italic;"&gt;really &lt;/span&gt;should - pen and paper are great tools!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-5732742218549856300?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/5732742218549856300/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=5732742218549856300' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5732742218549856300'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5732742218549856300'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2009/01/small-things-fisher-space-pen.html' title='The small things: Fisher Space Pen'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_lKbnbukR-QY/SXBZAPFrC4I/AAAAAAAAACM/7ge1kcwXC4M/s72-c/moleskine_fisher.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-169113937749219076</id><published>2008-12-08T11:41:00.000-08:00</published><updated>2008-12-08T23:08:38.182-08:00</updated><title type='text'>PomodoroButtButtButt</title><content type='html'>&lt;a href="http://www.oredev.org/"&gt;Øredev&lt;/a&gt; was a fantastic conference! I can't stress that enough! So why haven't I blogged about it? Well, there's so many blogs already about it (google on "øredev" and "blog") so I don't really know how to contribute more content, just repeat what's already been said. That's why.&lt;br /&gt;&lt;br /&gt;Instead, I'm going to write a note on one thing that I've taken with me from a talk at Øredev: &lt;span style="font-style: italic;"&gt;the Pomodoro Technique&lt;/span&gt;. It's essentially a technique that takes agile to the personal productivity level: working in small and timeboxed iterations (25 minutes), with short breaks (3-5 minutes) between each iteration and longer breaks (15-30 minutes) between 4 iterations in a row. Oh, by the way, an iteration is called a &lt;span style="font-style: italic;"&gt;pomodoro&lt;/span&gt;, italian for tomato. Why tomato? Because the inventor of the Pomodoro Technique, Francesco Cirillo, used an egg timer formed as a tomato during the early phase developing the technique. Further, each day starts with planning and ends with collecting and visualizing the data collected, ready to be analysed and &lt;a href="http://www.pragprog.com/titles/dlret/agile-retrospectives"&gt;retrospected&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;So, the idea is very simple, but of course there a lot more to it. What you should start with is to read &lt;a href="http://blog.staffannoteberg.com/"&gt;Staffan Nöteberg&lt;/a&gt;'s &lt;a href="http://blog.staffannoteberg.com/2008/02/22/pomodoro-technique-in-5-minutes/"&gt;Pomodoro Technique in 5 minutes&lt;/a&gt;. Actually, when the videos from Øredev get published, you should start there: Staffan Nöteberg did an &lt;span style="font-style: italic;"&gt;excellent&lt;/span&gt; talk on the Pomodoro Technique, sometimes using &lt;a href="http://www.flickr.com/photos/chrishedgate/3045641884/"&gt;hats&lt;/a&gt; and &lt;a href="http://www.flickr.com/photos/chrishedgate/3045641630/"&gt;dolls&lt;/a&gt; to illustrate his points.&lt;br /&gt;&lt;br /&gt;There's also a quite large &lt;a href="http://www.tecnicadelpomodoro.it/docs/francesco-cirillo/2007/ThePomodoroTechnique_v1-3.pdf"&gt;pdf&lt;/a&gt; by Francesco Cirillo available, but I haven't had time to read that one yet.&lt;br /&gt;&lt;br /&gt;I've just tried out the Pomodoro Technique myself for a couple of days now and some days have contained more pomodoros than others. Basically, I bought an egg timer for 25 SEK (around $3) and started with the fixed timebox part of the technique and logged the results. The second day I started to do some naïve estimation for each task. I also started with post-it notes for tasks, my personal pull system.&lt;br /&gt;&lt;br /&gt;If you follow and read the links in this post, you'll see that I don't really do that much of the Pomodoro Technique! That's ok with me, I'm aware of that and that's why the title of this post is "PomodoroButtButtButt" (paraphrasing Jeff Sutherland's &lt;a href="http://www.agilejava.eu/2008/11/27/scrumbutt/"&gt;ScrumButt&lt;/a&gt;). I'm just getting used to the habit though, and making the human beings around me used to it as well. No need to be extreme here..&lt;br /&gt;&lt;br /&gt;By the way, I'm still recording my workday in &lt;a href="http://www.timesnapper.com/"&gt;TimeSnapper&lt;/a&gt;, now &lt;a href="http://www.timesnapper.com/professional.aspx"&gt;TimeSnapper Professional&lt;/a&gt;. But that's a future blog post.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-169113937749219076?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/169113937749219076/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=169113937749219076' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/169113937749219076'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/169113937749219076'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/12/pomodorobuttbuttbutt.html' title='PomodoroButtButtButt'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-9206331226092623413</id><published>2008-11-13T09:29:00.000-08:00</published><updated>2008-11-13T13:03:18.938-08:00</updated><title type='text'>TimeSnapper against MultiTasking</title><content type='html'>I have been working at &lt;a href="http://www.dotway.se/"&gt;Dotway&lt;/a&gt; now, for almost two weeks. When your environment change, there's a good opportunity for changing habits as well. So, I started with the habit of using &lt;a href="http://www.timesnapper.com/"&gt;TimeSnapper&lt;/a&gt; every morning. TimeSnapper is a tool that takes a screenshot every 5 seconds or so, and has the ability to "play" the images, as a movie. The movie obviously has a higher image frequency than 5 seconds, so a whole day takes approximately 5-10 minutes to play.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.timesnapper.com/image/v2.6/ts-mainform-play-btn.png"&gt;&lt;img style="border-width: 0px; margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 112px; height: 88px;" src="http://www.timesnapper.com/image/v2.6/ts-mainform-play-btn.png" alt="" border="0" /&gt;&lt;/a&gt;Essentially, you gain the ability to self-monitor - seeing yourself in third person. Early and frequent feedback is a really good thing to have in most areas, like TDD for development or Scrum for projects. In my opinion, TimeSnapper gives the same kind of early and frequent feedback. So, every morning I play the movie of yesterday, write down the activities in time intervals, analyse my behavior, and ask myself the question "What should I do &lt;span style="font-style: italic;"&gt;today&lt;/span&gt; that make my morning analysis more joyful &lt;span style="font-style: italic;"&gt;tomorrow&lt;/span&gt;?". Let me explain..&lt;br /&gt;&lt;br /&gt;I don't like when I'm forced to write: "08:00-11:00, XX:ed, YY:ed and ZZ:ed" - that's not really informative, i.e., how much time did I spent on XX, compared to YY? But it's not the logging problem in itself that I have problems with, it's that I &lt;span style="font-style: italic;"&gt;know&lt;/span&gt; that it's bad for productivity to multitask, but &lt;span style="font-style: italic;"&gt;still&lt;/span&gt; I do it. Without knowing it as well, it seems. Constantly context switching is bad for productivity. So, I should only focus on &lt;span style="font-style: italic;"&gt;one task&lt;/span&gt; simultaneously to make tomorrow morning a good start at the day.&lt;br /&gt;&lt;br /&gt;Look, if you're using &lt;a href="http://en.wikipedia.org/wiki/Getting_Things_Done"&gt;GTD&lt;/a&gt; (a nice time management methodology), but instead of doing something useful instead read &lt;a href="http://lifehacker.com/"&gt;LifeHacker&lt;/a&gt; (a nice site/blog) every 10th minute, something is utterly wrong! Not really getting things done, are you? Though, it &lt;span style="font-style: italic;"&gt;can be hard&lt;/span&gt; to see for yourself. TimeSnapper lets you visualize your behaviour at the computer, putting your (potentially) multi-tasking in an embarrasingly bright light to yourself.&lt;br /&gt;&lt;br /&gt;Thanks to &lt;a href="http://www.hanselman.com/tools/"&gt;Scott Hanselman&lt;/a&gt; for making the &lt;a href="http://www.hanselman.com/tools/"&gt;tools list&lt;/a&gt; where I found TimeSnapper.&lt;br /&gt;&lt;br /&gt;As a side note, I really like &lt;a href="http://www.43folders.com/"&gt;43folders&lt;/a&gt;' new direction. Or, at least, &lt;a href="http://www.43folders.com/2008/09/10/time-attention-creative-work"&gt;this particular post&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-9206331226092623413?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/9206331226092623413/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=9206331226092623413' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/9206331226092623413'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/9206331226092623413'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/11/timesnapper-against-multitasking.html' title='TimeSnapper against MultiTasking'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-5790442726420536151</id><published>2008-10-30T11:41:00.000-07:00</published><updated>2008-10-31T03:59:32.707-07:00</updated><title type='text'>Turtle Graphics :: The big refactoring</title><content type='html'>&lt;a href="http://coffeedrivendevelopment.blogspot.com/2008/10/turtle-graphics-changing-interface.html"&gt;Last time on "Turtle Graphics"&lt;/a&gt;, we ended up having the type Turtle -&gt; [Turtle] on functions. The combine function had the type [Turtle] -&gt; (Turtle -&gt; [Turtle]) - Turtle. Let's have some "fun"!&lt;br /&gt;&lt;br /&gt;First, we add a helpful parameter to the turtle - penIsDown, i.e., the turtle is writing.&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;data Turtle = Turtle &lt;span style="font-family: monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; x :: Double,&lt;span style="font-family: monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; y :: Double,&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; alpha :: Double, -- alpha = 0 means East&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt; penIsDown :: Bool&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;deriving (Show)&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;We then add two useful functions for pen modification:&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;penDown t = let t' = t {penIsDown = True} in Logged {value = t', logs = [t']}&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;br /&gt;penUp t = let t' = t {penIsDown = False} in Logged {value = t', logs = [t']}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Second, we &lt;span style="font-style: italic;"&gt;assume&lt;/span&gt; that it would be useful to split the functionality of returning a value and log, pretty much following &lt;a href="http://en.wikipedia.org/wiki/Separation_of_concerns"&gt;separation of concerns&lt;/a&gt;. We could do this in a tuple, but I prefer having names on things:&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;data Logged l = Logged {&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;value :: l, &lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;logs :: [l]&lt;/span&gt; &lt;span style="font-family:courier new;"&gt; }&lt;/span&gt; &lt;span style="font-family:courier new;"&gt; deriving (Show)&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;This has some implications on our code. All the "core" functions must now return both a turtle value and a singleton log. Oh, and by the way, the Command type changed as well.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;type Command = Turtle -&gt; Logged Turtle&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;go, left, right, penDown, penUp :: Command&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;go t =  let t' = t {x = x t + step * cos (alpha t),&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;                   y = y t + step * sin (alpha t)&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;                  &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;       &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;in Logged {value = t', logs = [t']}&lt;/span&gt;  &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;br /&gt;left = rotate (pi/2)&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;right = rotate (-pi/2)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;penDown t = let t' = t {penIsDown = True} in Logged {value = t', logs = [t']}&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;penUp t = let t' = t {penIsDown = False} in Logged {value = t', logs = [t']}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;We must also change the function for combining functions:&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;(|&gt;|) :: Logged Turtle -&gt; Command -&gt; Logged Turtle&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;br /&gt;logged |&gt;| f = let logged' = f (value logged)&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;              in Logged {value = value logged',&lt;br /&gt;                         logs = logs logged' ++ logs logged}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;As you see, there's a lot of duplicate code. Let's do an &lt;a href="http://www.refactoring.com/catalog/extractMethod.html"&gt;ExtractMethod&lt;/a&gt; (sort of):&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;logThis val = Logged {value = val, logs = [val]}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;go, penDown, penUp :: Command&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;go t =  logThis $ t {x = x t + step * cos (alpha t), &lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;                    y = y t + step * sin (alpha t)&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;  &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;penDown t = logThis $ t {penIsDown = True}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;penUp t = logThis $ t {penIsDown = False}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;rotate :: Double -&gt; Command&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;rotate v t = logThis $ t {alpha = alpha t + v}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Still, we could be more generic in our logging type. That is, there is still a restriction on that the value returned and the log have the same type: the log is a list of the same type as the value has. We try to relax this restriction:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;data Logged v l = Logged {&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt; value :: v,&lt;br /&gt; &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;logs :: l&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;}&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;deriving (Show)&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Maybe we did too much, logs isn't a list anymore. However, if it's really necessary, we'll find out through &lt;a href="http://en.wikipedia.org/wiki/Type_inference"&gt;type inference&lt;/a&gt;. It's not obvious that we really need a &lt;span style="font-style: italic;"&gt;list&lt;/span&gt;, just something we can &lt;span style="font-style: italic;"&gt;append&lt;/span&gt; "stuff" to. Anyway, we get some compiler errors now:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;`Logged Turtle' is not applied to enough type arguments&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;    Expected kind `?', but `Logged Turtle' has kind `k -&gt; *'&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;    In the type synonym declaration for `Command'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We "fix" this by removing the Command type synonym and all references to it. I have a feeling that we'll need to fiddle some more with the types, so right now they're only in the way. If the types really are needed, we will find out (by a compiler error)! Though, it could be interesting to see what the type of e.g., "go" is:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; :t go&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;go :: Turtle -&gt; Logged Turtle [Turtle]&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Ah, just what I expected, but was too lazy too write. ;) Let's check the combining function:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; :t (|&gt;|)&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;(|&gt;|) :: Logged v [a] -&gt; (v -&gt; Logged v1 [a]) -&gt; Logged v1 [a]&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Hmm, this is rather weird! Before, the function was strongly bound to the Turtle type, which doesn't seem to be the case anymore. Moreover, we see that the input value type (v) doesn't need to be the same as the output value type (v1). How cool is that!?! It would be hard for me to look at the function and calculate the type myself, but Haskell just inferred the most generic type it could find. Coolness!&lt;br /&gt;&lt;br /&gt;As always, a &lt;a href="http://en.wikipedia.org/wiki/Software_design_pattern"&gt;design pattern&lt;/a&gt; can be hard to spot, especially if you haven't spotted it before. Here's how Gregg Irwin puts it (from an &lt;a href="http://www.oredev.org/"&gt;Øredev&lt;/a&gt; &lt;a href="http://www.oredev.org/download/18.5bd7fa0510edb4a8ce4800018798/Jimmy_Nilsson_-_Patterns.pdf"&gt;presentation&lt;/a&gt; by &lt;a href="http://jimmynilsson.com/blog/"&gt;Jimmy Nilsson&lt;/a&gt;):&lt;br /&gt;&lt;blockquote&gt;1. You use it without being aware that you’re using it&lt;br /&gt;2. You hear about it, read up on it, and tinker a bit&lt;br /&gt;3. You learn more and start using it explicitly, if naively&lt;br /&gt;4. You get the fire and evangelize (optional)&lt;br /&gt;5. Something ”clicks”&lt;br /&gt;6. You learn more and apply it ”less naively”and more implicitly&lt;br /&gt;7. Time passes and you see flaws&lt;br /&gt;8. You question the concept (often because you misapplied it)&lt;br /&gt;9. You either forget about it or add knowledge and experience&lt;br /&gt;(Repeat steps 5-9 if necessary)&lt;br /&gt;10. You use it without being aware that you are using it&lt;/blockquote&gt;Essentially, our Logger type is a &lt;a href="http://en.wikibooks.org/wiki/Haskell/Category_theory"&gt;monad&lt;/a&gt;. Or, actually, the type of |&gt;| resembles &gt;&gt;=, which is the associative function that composes a particular monad.  Since monads are important in Haskell, some syntactic sugar (the do-notation) has been added to make it easier to work with them.&lt;br /&gt;&lt;br /&gt;Let's try to instantiate the monad class:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;instance Monad (Logged a b) where&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;We get a compiler error:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;Kind mis-match&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Expected kind `* -&gt; *', but `Logged a b' has kind `*'&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;In the instance declaration for `Monad (Logged a b)'&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;We try to remove both type parameters:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;instance Monad (Logged) where&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;..but still an error (yet, another one)&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;`Logged' is not applied to enough type arguments&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Expected kind `* -&gt; *', but `Logged' has kind `* -&gt; * -&gt; *'&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;In the instance declaration for `Monad (Logged)'&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;As you might see, we need to bind one of the types, whereas the other one needs to be "free". This puts us in a dilemma, since we know that we have both a type "v" and "v1". Thus, the type of the log must be bound (or, at least given a parametrized name):&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;data Logged l v = Logged {  --notice the different order&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;     &lt;br /&gt; value :: v,&lt;span style="font-family: monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; logs :: l&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;deriving (Show)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;instance Monad (Logged l) where&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;So Logged is missing out one type parameter. It's kind of a function over types, that takes a type and returns another type - just as the error message above implied. Though, we get an error again when we try to implement bind:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;instance Monad (Logged l) where&lt;/span&gt; &lt;span style="font-family:courier new;"&gt; l &gt;&gt;= f = l |&gt;| f&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Couldn't match expected type `[a]'&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;against inferred type `l' (a rigid variable)&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;  &lt;br /&gt;`l' is bound by the instance declaration at writer5.hs:46:0&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;Expected type: Logged [a] v&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;     &lt;br /&gt;Inferred type: Logged l a1&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;   &lt;br /&gt;In the first argument of `(|&gt;|)', namely `l'&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;   &lt;br /&gt;In the expression: l |&gt;| f&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Now, we actually need to say that the log is a list. Maybe we can remove this requirement in a later blog post, but right now we go with the compiler.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;data Logged l v = Logged {&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt; &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;value :: v,&lt;span style="font-family: monospace;"&gt;&lt;br /&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;logs :: [l]&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;}&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;deriving (Show)&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Hey, it works! Or, at least it compiles. But that tends to be synonyms in Haskell ;) We specify "return":&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;instance Monad (Logged l)&lt;br /&gt;where&lt;/span&gt; &lt;span style="font-family:courier new;"&gt; l &gt;&gt;= f = l |&gt;| f&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;return val = Logged {value = val, logs = []}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Notice that we can still use |&gt;| as usual:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; start |&gt;| go |&gt;| go &lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;Logged {value = Turtle {x = 2.0, y = 0.0, alpha = 0.0, penIsDown = True},&lt;br /&gt;logs = [Turtle {x = 2.0, y = 0.0, alpha = 0.0, penIsDown = True},&lt;br /&gt;Turtle {x = 1.0, y = 0.0, alpha = 0.0, penIsDown = True},&lt;br /&gt;Turtle {x = 0.0, y = 0.0, alpha = 0.0, penIsDown = True}]}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;So, why all this trouble? Well, it wasn't &lt;span style="font-style: italic;"&gt;that&lt;/span&gt; hard! Essentially, all we did was to make the logging a bit more separated and generic. Then we adjusted the types a bit to make them align with Haskell's monad class. The &lt;span style="font-style: italic;"&gt;big win&lt;/span&gt; is that our Logger is now reusable if we want to log something else than turtles in the future. So, by adjusting towards a common pattern, we gained &lt;span style="font-style: italic;"&gt;both&lt;/span&gt; syntactic sugar &lt;span style="font-style: italic;"&gt;and reusability&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Of course, there are some ways to improve. I'll perhaps cover this in future posts. Oh, and by the way, our Logger monad is actually the &lt;a href="http://www.haskell.org/all_about_monads/html/writermonad.html"&gt;Writer monad&lt;/a&gt;. Just thought you should know that.. ;)&lt;br /&gt;&lt;br /&gt;Conclusion: &lt;a href="http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads/"&gt;Brian Beckman&lt;/a&gt; was right, we've invented monads by ourselves, maybe without thinking about it. Or?&lt;br /&gt;&lt;br /&gt;Note: Since I'm not a master in &lt;a href="http://coffeedrivendevelopment.blogspot.com/2008/10/category-theory.html"&gt;category theory&lt;/a&gt;, I'm not sure if Logger actually was a "real" monad (strictly speaking), before we changed the order of the type parameters and removed a type parameter in the monad instantiation, making it have the right &lt;a href="http://en.wikipedia.org/wiki/Type_system#Types_of_types"&gt;kind&lt;/a&gt;. Any ideas?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-5790442726420536151?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/5790442726420536151/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=5790442726420536151' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5790442726420536151'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5790442726420536151'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/turtle-graphics-big-refactoring.html' title='Turtle Graphics :: The big refactoring'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-1309140267985449374</id><published>2008-10-28T03:09:00.000-07:00</published><updated>2008-10-28T06:07:59.247-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='F#'/><category scheme='http://www.blogger.com/atom/ns#' term='kata'/><title type='text'>My first F#, Binary Chop</title><content type='html'>Yesterday, I really felt like trying out &lt;a href="http://research.microsoft.com/fsharp/"&gt;F#&lt;/a&gt;. To get some inspiration, I visited &lt;a href="http://www.pragprog.com/"&gt;PragProgs&lt;/a&gt; &lt;a href="http://codekata.pragprog.com/2007/01/code_kata_backg.html"&gt;katas&lt;/a&gt;, and chose &lt;a href="http://codekata.pragprog.com/2007/01/kata_two_karate.html"&gt;Kata Two -- Karate Chop&lt;/a&gt;. Or, actually, I just implemented a "functional" solution. I tried to make an imperative pointer-based solution (which I might post later, when I have resolved a strange bug). Anyway, I had never coded F# before, so there might be a few places where I could have e.g. chosen a library function instead of implementing it myself. If you have any suggestions or general comments, please post them!&lt;br /&gt;&lt;br /&gt;By the way, is there any way to program in literate F#?&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family: courier new;"&gt;#light&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-family: georgia;"&gt;First, a few helper functions for triples.&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family: courier new;"&gt;let fst3 (a,b,c) = a&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;let snd3 (a,b,c) = b&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;let trd3 (a,b,c) = c&lt;/span&gt;&lt;/pre&gt;Define the middle index, for simplicity return 0 if list is empty.&lt;pre&gt;&lt;span style="font-family: courier new;"&gt;let middleIndex xs = if List.is_empty xs then 0 else (List.length xs - 1)/2&lt;/span&gt;&lt;/pre&gt;Define a function that returns the middle element of a list, and two functions that returns the first/last remaining halfs. Note that there might not be a middle element, so we use the option type.&lt;pre&gt;&lt;span style="font-family: courier new;"&gt;let firstHalf xs = Seq.take (middleIndex xs) xs&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;let middleElem xs = &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; match (xs) with&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;    | [] -&gt; None&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;    | xs -&gt; Some (xs.Item (middleIndex xs))&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;let lastHalf xs = &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; match xs with &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;    | [] -&gt; Seq.empty&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;    | xs2 -&gt; Seq.skip (middleIndex xs + 1) xs2&lt;/span&gt;&lt;/pre&gt;Let us group the functions in a convenient triple.&lt;pre&gt;&lt;span style="font-family: courier new;"&gt;let splitInHalf xs = (firstHalf xs, middleElem xs, lastHalf xs)&lt;/span&gt;&lt;/pre&gt;Now, define a recursive function that 1) splits the list in three parts 2) reuse some definitions 3) return "None" if middle is empty 4) otherwise we can test the middle for equality, if equal then return the current index 5) if not equal, choose appropriate half and recurse.&lt;pre&gt;&lt;span style="font-family: courier new;"&gt;let rec exists x xs i = &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; let triple = splitInHalf xs in                  //1&lt;/span&gt; &lt;br /&gt;&lt;span style="font-family: courier new;"&gt; let maybeMiddle = snd3 triple                   //2&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; let firstPart = Seq.to_list (fst3 triple)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; let lastPart = Seq.to_list (trd3 triple)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; if Option.is_none maybeMiddle then None         //3&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; else let middle = Option.get maybeMiddle in     //4&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;      if x.Equals middle then Some(i)            //5&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;      elif middle &gt; x then exists x firstPart (middleIndex firstPart)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;                    else exists x lastPart ((i+1) + middleIndex lastPart)&lt;/span&gt;&lt;/pre&gt;This is the function a user would call.&lt;pre&gt;&lt;span style="font-family: courier new;"&gt;let public ex x xs = exists x xs (middleIndex xs)&lt;/span&gt;&lt;/pre&gt;Some tests, just to check, plus a helper function for equality over options.&lt;pre&gt;&lt;span style="font-family: courier new;"&gt;//Tests&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;let eq x y = if Option.is_none x then Option.is_none y else x.Equals y&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;let res = [&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 3 [];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 3 [1];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 1 [1];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 1 [1;3;5];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 3 [1;3;5];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 5 [1;3;5];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 0 [1;3;5];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 2 [1;3;5];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 4 [1;3;5];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 6 [1;3;5];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 1 [1;3;5;7];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 3 [1;3;5;7];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 5 [1;3;5;7];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 7 [1;3;5;7];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 0 [1;3;5;7];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 2 [1;3;5;7];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 4 [1;3;5;7];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 6 [1;3;5;7];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ex 8 [1;3;5;7];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;let answers = [None; None; Some 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; Some 0; Some 1; Some 2; None; None; None; None;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; Some 0; Some 1; Some 2; Some 3; None; None; None; None; None&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; ]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;let asserts = Seq.for_all2 eq res answers&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;asserts&lt;/span&gt;&lt;/pre&gt;Now, I must say that I had a real good time developing this! Ok, some minor things didn't went as smoothly as I'd hope for, but it was actually the first time that I tried F#. I've never had such a good experience with a language the first day of use.&lt;br /&gt;&lt;br /&gt;I will definitly be posting more F# posts in the future!&lt;br /&gt;&lt;br /&gt;Cheers&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-1309140267985449374?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/1309140267985449374/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=1309140267985449374' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1309140267985449374'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1309140267985449374'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/my-first-f-binary-chop.html' title='My first F#, Binary Chop'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-6001593060152204218</id><published>2008-10-22T09:56:00.000-07:00</published><updated>2008-10-28T06:06:57.438-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='books'/><title type='text'>Category Theory</title><content type='html'>Guess what landed on my hallway floor today: "&lt;a href="http://www.amazon.com/Category-Computer-Scientists-Foundations-Computing/dp/0262660717"&gt;Basic Category Theory for Computer Scientists&lt;/a&gt;". Thank you Mr Mailman!&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://mitpress.mit.edu/images/products/books/0262660717-f30.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px;" src="http://mitpress.mit.edu/images/products/books/0262660717-f30.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;There's a lot of "greek" in there! Hopefully, I'll decipher it (and understand it, of course).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-6001593060152204218?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/6001593060152204218/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=6001593060152204218' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/6001593060152204218'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/6001593060152204218'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/category-theory.html' title='Category Theory'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-3417417960046773853</id><published>2008-10-21T00:24:00.000-07:00</published><updated>2008-10-22T05:59:05.536-07:00</updated><title type='text'>CoachTV</title><content type='html'>In the middle of August, &lt;a href="http://www.loudthinking.com/"&gt;David Heinemeier Hansson&lt;/a&gt; &lt;a href="http://twitter.com/d2h"&gt;twittered&lt;/a&gt; this:&lt;br /&gt;&lt;blockquote&gt;Lars Pind is doing video coaching: &lt;a href="http://coachtvblog.com/?p=3"&gt;http://coachtvblog.com/?p=3&lt;/a&gt; -- good thoughts on probability and significance. &lt;span style="font-style: italic;"&gt;4:59 PM Aug 16th&lt;br /&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;Since that date, I have been following &lt;a href="http://pinds.com/"&gt;Lars Pind&lt;/a&gt;'s fantastic video blog, &lt;a href="http://coachtvblog.com/"&gt;CoachTV&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I think it's hard to analyze yourself from the outside. Reminds me of a quote of &lt;a href="http://en.wikipedia.org/wiki/Richard_Feynman"&gt;Richard Feynman&lt;/a&gt;, the 1965 Nobel prize winner in physics:&lt;br /&gt;&lt;blockquote&gt;"The first principle is that you must not fool yourself—and you are the easiest person to fool."&lt;br /&gt;&lt;/blockquote&gt;It's hard to summarize what Pind's message is, so I rather not try. Instead, I want to re-post a comment that I did on one of his episodes. What triggered me to post the comment was that Pind talked about eating and that the next time the viewer ate something, he/she should try to really look at the food, feel the texture, slowly swallow, feel the taste, etc, etc. You get the picture? Obviously, the overall "taste experience" is not only about always eating good food, but rather that it is up to &lt;span style="font-style: italic;"&gt;you&lt;/span&gt;, if you bother to enjoy it or not. Made me think about music:&lt;br /&gt;&lt;blockquote&gt;Lars, you said that when we eat or drink something, we should try to feel the taste and texture more. That reminded me of the composer &lt;a href="http://en.wikipedia.org/wiki/John_Cage"&gt;John Cage&lt;/a&gt;, who had the same opinion about sound. His most famous piece is “4′33″, which is 4 minutes and 33 seconds of silence, written for piano. In summary, Cage had the opinion that there’s music everywhere, but it’s up to us to listen to it. So, even rush hour traffic can be music. Or when it’s so quiet that you can hear our own blood flow and pulse.&lt;br /&gt;&lt;br /&gt;It’s up to ourselves to broaden our senses and perspectives, so that we can enjoy the music in our everyday life, also when sound is not involved per se.&lt;/blockquote&gt;Could it be that the same reasoning goes for your other sensations and feelings as well? If not, why?&lt;br /&gt;&lt;br /&gt;Note: I actually wrote this post &lt;span style="font-style: italic;"&gt;before&lt;/span&gt; watching episode #26 of CoachTV, where Lars asks the viewers to "tell our friends" about his show. Just thought you should know that.. :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-3417417960046773853?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/3417417960046773853/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=3417417960046773853' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/3417417960046773853'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/3417417960046773853'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/coachtv.html' title='CoachTV'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-1505063322729419592</id><published>2008-10-20T23:49:00.000-07:00</published><updated>2008-10-28T06:11:18.286-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='podcasts'/><title type='text'>Podcast: Herding Code</title><content type='html'>There is a fairly new (May 2008) podcast that I listened to a lot lately (including their old episodes): &lt;a href="http://herdingcode.com/"&gt;Herding Code&lt;/a&gt;. These episodes are particularly nice:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://herdingcode.com/?p=28"&gt;Glenn Block on Prism, Unity, and MEF (part 1)&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://herdingcode.com/?p=31"&gt;Glenn Block on Prism, Unity, and MEF (part 2)&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://herdingcode.com/?p=45"&gt;Matt Podwysocki on F# and Functional Programming&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://herdingcode.com/?p=41"&gt;Interviewing Software Developers&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;Enjoy :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-1505063322729419592?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/1505063322729419592/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=1505063322729419592' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1505063322729419592'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1505063322729419592'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/podcast-herding-code.html' title='Podcast: Herding Code'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-8369573630513556013</id><published>2008-10-19T04:46:00.000-07:00</published><updated>2008-10-19T06:56:57.797-07:00</updated><title type='text'>TDD, reflection and the PropertiesEqual extension method</title><content type='html'>In my last post I wrote about the extension method ForEach, which is a very simple but useful method, at least when it comes to readability. &lt;br /&gt;&lt;br /&gt;In this post I'll try to explain another extension method, which I'll call PropertiesEqual. It's purpose is to extend the object class with a method to compare the properties of two objects:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public static bool PropertiesEqual&amp;LT;T&amp;GT;(this T obj1, T obj2)&lt;br /&gt;{&lt;br /&gt;  return true if all properties of obj1 equals all properties of obj2&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You probably know of the object.Equals method, which per default compares the memory addresses of two objects. That is, two objects are equal only if they are exactly the same object. If you really want to compare the content of two objects, you need to override this function in your class and manually compare them.&lt;br /&gt;&lt;br /&gt;When I designed this function, I started with two simple test cases:&lt;br /&gt;1. If two objects of the same class have the same public properties, yield true&lt;br /&gt;2. If two objects of the same class have different public properties, yield false&lt;br /&gt;&lt;br /&gt;Translated to code, this becomes:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;class TestClass&lt;br /&gt;{&lt;br /&gt;  public int A { get; set; }&lt;br /&gt;  public int B { get; set; }&lt;br /&gt;}&lt;br /&gt;[TestMethod()]&lt;br /&gt;public void PropertiesEqualTest()&lt;br /&gt;{&lt;br /&gt;  Assert.IsTrue(new Test { A = 1, B = 1 }.PropertiesEqual(new Test { A = 1, B = 1 }));&lt;br /&gt;  Assert.IsFalse(new Test { A = 1, B = 1 }.PropertiesEqual(new Test { A = 1, B = 0 }));&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The implementation is very straight forward:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public static bool PropertiesEqual&amp;LT;T&amp;GT;(this T obj1, T obj2)&lt;br /&gt;{&lt;br /&gt;  return typeof(T).GetProperties().All(property =&gt;&lt;br /&gt;    {&lt;br /&gt;      var prop1 = property.GetValue(obj1, null);&lt;br /&gt;      var prop2 = property.GetValue(obj2, null);&lt;br /&gt;      return prop1.EqualsTo(prop2);&lt;br /&gt;    });&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;typeof(T).GetProperties() will return all public properties of T, and property.GetValue(obj, null) will return the value of a given property. The All extension method returns true only if all elements of the sequence satisfy the given condition, i.e. all properties are equal.&lt;br /&gt;&lt;br /&gt;The test cases will pass fine, and we now have a simple way of comparing properties of two objects! In the next post I'll try to describe how to extend the method with support for recursive properties (compare properties of a property), and in the one after that I'll write about how to implement an IEqualityComparer based on this method.&lt;br /&gt;&lt;br /&gt;EDIT: Note, these series aren't much of the type "here is a revolutionary new technique", but more of "here is how I would write the code for this". My focus is to get you to understand how I think when I design methods, not to tell you that "this is the way"!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-8369573630513556013?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/8369573630513556013/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=8369573630513556013' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/8369573630513556013'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/8369573630513556013'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/tdd-reflection-and-propertiesequal.html' title='TDD, reflection and the PropertiesEqual extension method'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-1226686355690109594</id><published>2008-10-19T02:50:00.000-07:00</published><updated>2008-10-28T06:07:46.197-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='kata'/><title type='text'>Code Kata : Monopoly</title><content type='html'>Yesterday evening, me and two other friends arranged a highly spontaneus and inofficial &lt;a href="http://codekata.pragprog.com/"&gt;code kata&lt;/a&gt;, at my friend's apartment. The task was to develop a Monopoly game, using TDD. It was sort of an experiment as well, we wanted to see how TDD could help us &lt;span style="font-style: italic;"&gt;discovering&lt;/span&gt; design, rather than &lt;span style="font-style: italic;"&gt;inventing&lt;/span&gt; it.&lt;br /&gt;&lt;br /&gt;We started by talking about the domain, listing some words that we thought were important to the game. Then we made a small domain model diagram, just with boxes and lines (the relations had no directions or multiplicities). This was fun! It felt like we were back in school again.. :)&lt;br /&gt;&lt;br /&gt;After that, we started to make some &lt;a href="http://en.wikipedia.org/wiki/User_story"&gt;user stories&lt;/a&gt;, each on a small piece of paper. Here are the stories we came up with (in prioritized order):&lt;br /&gt;&lt;ul&gt;&lt;li&gt;A player walks the number of steps the dices show.&lt;/li&gt;&lt;li&gt;In the beginning of the game, the ordering of player moves is determined.&lt;/li&gt;&lt;li&gt;Players act in the predetermined order (was later removed, redundant with previous).&lt;/li&gt;&lt;li&gt;A player hits or passes "Go" and earn 4000.&lt;/li&gt;&lt;li&gt;A player buys the street he/she is on.&lt;/li&gt;&lt;/ul&gt;Now, we could start to implement the game! :) But, as we realized, the first story was very hard, since we had no code to start with. Actually, starting to write the first test was probably the hardest. I think we made the conclusion that the first test we tried to write was to big, so we divided the test into smaller parts, and BOOM, it went much more smoother. Also, we tried to follow &lt;a href="http://en.wikipedia.org/wiki/You_Ain%27t_Gonna_Need_It"&gt;YAGNI&lt;/a&gt;, as much as possible.&lt;br /&gt;&lt;br /&gt;An interesting detail is that we kind of get stuck on the "the dices show" part of the first story. It's obviously something that has to do with random number(s), but how should we test that? I.e., we thought that only players would need dices, but if a player use the Random() system method, then we must capture it in a public state in order to know if the player actually walked the number of steps that the dices show. Not nice! We felt the urge to really talk about this, to see if logical arguments could lead us into a good, and hopefully pragmatic, solution to this problem. We ended up with &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;dependency injecting&lt;/a&gt; an "IDice" - something that could give us a random number between two and twelwe, and mocking the IDice in the test. Nice! Now we could manipulate the player through fake dices, without having &lt;a href="http://martinfowler.com/bliki/CodeSmell.html"&gt;code smells&lt;/a&gt; all over the place!&lt;br /&gt;&lt;br /&gt;Though, in retrospective, it would have been nice with an expert on TDD in the room. In the end,  you would like yourself (or the group) to ask good questions and answer them logically, but without experience, asking the right questions in the right time is hard. A teacher behind the back, mentoring and supporting would be nice to have. Reminds me a little about Polya, "&lt;a href="http://en.wikipedia.org/wiki/How_to_Solve_It"&gt;How to Solve It&lt;/a&gt;". Though, doing the excercise without a "master" was probably a good idea, in some way: it made us (more) convinced about what we were doing, and if we weren't convinced, we had to talk about it.&lt;br /&gt;&lt;br /&gt;We did the excercise for about six hours, but we had dinner and wine during that time as well, and perhaps we weren't that effective all the time :) Anyway, we implemented all the stories, except the last one (a half-baked story).&lt;br /&gt;&lt;br /&gt;Thank you for a very nice evening!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-1226686355690109594?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/1226686355690109594/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=1226686355690109594' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1226686355690109594'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1226686355690109594'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/code-kata-monopoly.html' title='Code Kata : Monopoly'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-6879395916934310521</id><published>2008-10-16T01:36:00.001-07:00</published><updated>2008-10-16T02:40:09.634-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='haskell'/><category scheme='http://www.blogger.com/atom/ns#' term='turtle'/><title type='text'>Turtle Graphics :: Changing Interface</title><content type='html'>Recalling, my first idea of the Turtle Graphics posts was to see if it was likely that a beginner in functional programming would have invented monads by himself/herself.&lt;br /&gt;&lt;br /&gt;Let's change the interface a bit, such that a command is a function from log to log.&lt;br /&gt;&lt;br /&gt;(I will reuse definitions from the &lt;a href="http://coffeedrivendevelopment.blogspot.com/2008/10/turtle-graphics-basics.html"&gt;original post&lt;/a&gt;, i.e. turtle definition and step)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; type Command = [Turtle] -&gt; [Turtle]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; start =  Turtle { x=0, y=0, alpha=0 }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; go, left, right :: Command&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&gt; go [] = []&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&gt; go (t:ts) =  t {x = x t + step * cos (alpha t),&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&gt; &amp;nbsp;&amp;nbsp;y = y t + step * sin (alpha t),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;&gt; &lt;/span&gt;&lt;span style="font-family:courier new;"&gt; &amp;nbsp;&amp;nbsp;alpha = alpha t} : t : ts&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;br /&gt;&gt; rotate :: Double -&gt; Command&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&gt; rotate v [] = []&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; rotate v (t:ts) = t {alpha = alpha t + v} : t : ts&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; left = rotate (pi/2)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; right = rotate (-pi/2)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; execute :: [Command] -&gt; Turtle -&gt; [Turtle]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; execute cs l = (foldr (.) id cs) [l]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Basically, the primitive commands have to work on the whole list and consider the case of an empty list, whereas commands defined in terms of other commands don't have that restriction. It's also easy to define new commands:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; go3 = go . go . go&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;But the primitive commands don't look good at all! But, it's an easy fix..&lt;br /&gt;&lt;br /&gt;The problem is of course with the type of &lt;span style="font-family:courier new;"&gt;Command&lt;/span&gt;. A primitive command only cares about the first turtle in the list (the newest one), so why should a command take a whole list as parameter?&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&gt; type Command = Turtle -&gt; [Turtle]&lt;/span&gt;  &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;br /&gt;&gt; go, left, right :: Command&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&gt; go t =  return $ t {x = x t + step * cos (alpha t),&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; &amp;nbsp;&amp;nbsp;y = y t + step * sin (alpha t),&lt;/span&gt;&lt;br /&gt;&gt; &lt;span style="font-family:courier new;"&gt; &amp;nbsp;&amp;nbsp;alpha = alpha t}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&gt; rotate :: Double -&gt; Command&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; rotate v t = return $ t {alpha = alpha t + v}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&gt; left = rotate (pi/2)&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&gt; right = rotate (-pi/2)&lt;/span&gt;&lt;br /&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&gt; --       [Turtle] -&gt; (Turtle -&gt; [Turtle]) -&gt; [Turtle]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; |&gt;| :: [Turtle] -&gt; Command              -&gt; [Turtle]&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&gt; [] |&gt;| f = []&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&gt; (l:ls) |&gt;| f = (f l) ++ (l:ls)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So, for &lt;span style="font-family: courier new;"&gt;go&lt;/span&gt; and &lt;span style="font-family: courier new;"&gt;rotate&lt;/span&gt;, we use a little trick, using &lt;span style="font-family: courier new;"&gt;return&lt;/span&gt; instead of putting the result in a list. What is much more interesting is that we now have a little asymmetry &lt;span style="font-family: courier new;"&gt;&lt;/span&gt;in the type of a command. It is not possible anylonger to use &lt;span style="font-family: courier new;"&gt;(.)&lt;/span&gt; for combining commands, so we define &lt;span style="font-family: courier new;"&gt;|&gt;|&lt;/span&gt;  to do that for us.&lt;br /&gt;&lt;br /&gt;We must use a little lambda to define new commands..&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;&gt; go3 = \t -&gt; t |&gt;| go |&gt;| go |&gt;| go&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;..but that shouldn't be too much of a problem.&lt;br /&gt;&lt;br /&gt;We're not done yet, but at least we took care about our little logging problem we had the previous post.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-6879395916934310521?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/6879395916934310521/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=6879395916934310521' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/6879395916934310521'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/6879395916934310521'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/turtle-graphics-changing-interface.html' title='Turtle Graphics :: Changing Interface'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-1212553967695620015</id><published>2008-10-16T00:33:00.000-07:00</published><updated>2009-03-12T13:11:33.656-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='extension methods'/><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>The ForEach extension method (C#)</title><content type='html'>Everyone knows about the foreach keyword, but have you ever noticed there are no equivalent extension method for IEnumerables? I.e. execute "this action" on each element. Perhaps the most simple extension method of them all, which is the reason I'll share it with you :)&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;public static class MyExtensionMethods&lt;br /&gt;{&lt;br /&gt;  public static void ForEach&amp;LT;T&amp;GT;(this IEnumerable&amp;LT;T&amp;GT; list, Action&amp;LT;T&amp;GT; action)&lt;br /&gt;  {&lt;br /&gt;    foreach(var item in list) action(item);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;For you who aren't familiar with extension methods, what it really says is that, ForEach is a member method of IEnumerable&amp;LT;T&amp;GT;, and can be called by simply writing &lt;br /&gt;&lt;pre name="code" class="c#"&gt;myList.ForEach(myAction)&lt;/pre&gt;&lt;br /&gt;For example, to output each item of a list to the console, you could write:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;myList.ForEach(x =&gt; Console.Write(x));&lt;/pre&gt;&lt;br /&gt;instead of&lt;br /&gt;&lt;pre name="code" class="c#"&gt;foreach(var x in in myList) Console.Write(x);&lt;/pre&gt;&lt;br /&gt;In this case you might not gain so much in terms of code size, but I think it makes the code more readable. You are reading text from left to right, so it makes sense having the for each statement to the right, right? :)&lt;br /&gt;&lt;pre name="code" class="c#"&gt;myList.WhereThis().SelectThat().DoThis();&lt;/pre&gt;&lt;br /&gt;(There is probably some kind of cool name for this pattern, like "The opposite of law of demeter"... Gustaf probably has more knowledge in this!)&lt;br /&gt;&lt;br /&gt;Here is a more complex example of a combination of extension methods:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;myList.Where(x =&gt; x.IsValid()).Select(x =&gt; x.ComputeValue()).Where(x =&gt; x &gt; 0).ForEach(x =&gt; Console.Write(x));&lt;/pre&gt;&lt;br /&gt;Where and Select are also extension methods of IEnumerable&lt;T&gt;. Without these you would have to write:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;foreach(var x in myList) &lt;br /&gt;{&lt;br /&gt;  if (x.IsValid()) &lt;br /&gt;  {&lt;br /&gt;    var value = x.ComputeValue();&lt;br /&gt;    if (value &gt; 0) Console.Write(value);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;8 lines instead of 1! Impressed? :)&lt;br /&gt;&lt;br /&gt;So, extension methods! Learn them, use them, and write your own!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-1212553967695620015?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/1212553967695620015/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=1212553967695620015' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1212553967695620015'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1212553967695620015'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/foreach-extension-method-c.html' title='The ForEach extension method (C#)'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-2581188379867956717</id><published>2008-10-15T23:54:00.000-07:00</published><updated>2008-10-28T06:09:34.128-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='haskell'/><category scheme='http://www.blogger.com/atom/ns#' term='turtle'/><title type='text'>Some thoughts on Turtle Graphics Basics</title><content type='html'>In the comments of &lt;a href="http://coffeedrivendevelopment.blogspot.com/2008/10/turtle-graphics-basics.html"&gt;last turtle post&lt;/a&gt;, Joel pointed out a way to get commands composable. I didn't quite get it to work, probably because of that &lt;span style="font-family: courier new;"&gt;Command&lt;/span&gt; is a type synonym and not a concrete type. The first time I saw the proposed solution, to combine commands with  &lt;span style="font-family: courier new;"&gt;&amp;lt;|&amp;gt;&lt;/span&gt;, I got a little afraid. I'll explain why..&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;Update&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Joel pointed out (in a mail) that Haskell98 doesn't permit instances of type synonyms or lists of types. If the flag "fglasgow-exts" is used, then the problem is solved.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;/Update&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The biggest reason I got afraid was that it is still up to the consumer of the code to combine commands in a "good" way, i.e., to log intermediate positions. But, It would be perfectly legal to combine commands with &lt;span style="font-family: courier new;"&gt;(.)&lt;/span&gt; instead of &lt;span style="font-family: courier new;"&gt;(&amp;lt;|&amp;gt;)&lt;/span&gt;, making the API quite risky. I now think a bit different..&lt;br /&gt;&lt;br /&gt;It would be possible to kind of wrap the commands into a data structure of their own (as the Command pattern in OO):&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;&gt; data C = C { unC :: Command }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In some way, this is very type-safe, since it will be cumbersome to un-wrap the commands to combine them in a "bad" way..&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: courier new;"&gt;&gt; go2 = C { (unC go) . (unC go) }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;..thus, users are forced to use our special operator for combining commands. But there something I don't like about this. We're kind of forcing the users into a datastructure, just to restrict them. I think we loose a lot of nice functions (i.e. from the prelude) this way. Of course, we could define functions that makes it easier for the user, but the user still has to learn those functions. What I'm trying to say is that it sometimes is bad to invent a whole new API style from scratch, when for example a monadic style API has the benefit that more people know about it &lt;span style="font-style: italic;"&gt;and&lt;/span&gt; that we can "piggyback" on all the tutorials on monads. Or maybe it's just me that is too focused on monads at the moment. ;)&lt;br /&gt;&lt;br /&gt;I guess this could be a nice discussion? What do you think?&lt;br /&gt;&lt;br /&gt;Right now, my biggest reason to be afraid of Joels solution is that it uses type classes, which is a very exotic feature, compared to other functional languages. There are obviously nice, but it would be hard to see how a "nice" Turtle Graphics API could be ported to i.e. F#, if we based the API on type classes.&lt;br /&gt;&lt;br /&gt;Next: Changing interface on primitive commands&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-2581188379867956717?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/2581188379867956717/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=2581188379867956717' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/2581188379867956717'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/2581188379867956717'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/some-thoughts-on-turtle-graphics-basics.html' title='Some thoughts on Turtle Graphics Basics'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-3138372148231375782</id><published>2008-10-14T04:02:00.000-07:00</published><updated>2008-10-14T04:35:07.356-07:00</updated><title type='text'>Joining Dotway!</title><content type='html'>Last Friday I joined &lt;a href="http://www.dotway.se/"&gt;Dotway&lt;/a&gt;! Dotway is a .NET consulting company and was founded in Malmö, but I will be working in the Gothenburg office.&lt;br /&gt;&lt;br /&gt;Dotway is a &lt;a href="https://partner.microsoft.com/global/program/programoverview/40013031"&gt;Microsoft Gold partner&lt;/a&gt; (achieved through competencies) and &lt;a href="https://solutionfinder.microsoft.com/SDK/Partners/PartnersDirectory.aspx?sortby=locationgrp_up&amp;amp;page=4&amp;amp;location=c1e886851b524bb399933bfc66b49c1f&amp;amp;competency=5562dd8a42b740ec8d99645669362322&amp;amp;vertical=41767ef4285a40e39f6da510ff46d4a9"&gt;offers&lt;/a&gt; services such as software development, design support, and training/mentoring. Dotway also practices &lt;span style="font-style: italic;"&gt;agile&lt;/span&gt; and TDD, and "&lt;a href="http://www.dotway.se/tabid/105/xmmid/519/xmid/177/xmview/2/Default.aspx"&gt;Dotway consultants are Certified ScrumMasters&lt;/a&gt;".&lt;br /&gt;&lt;br /&gt;I will start working in the beginning of November. Very exciting!&lt;br /&gt;&lt;br /&gt;Some links:&lt;br /&gt;&lt;a href="http://www.dotway.se/"&gt;Dotway&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.mikael-freidlitz.com/blog/"&gt;Mikael Freidlitz&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.johannormen.com/blog/"&gt;Johan Normén&lt;/a&gt;&lt;br /&gt;&lt;a href="http://blog.noop.se/"&gt;Magnus Mårtensson&lt;br /&gt;&lt;/a&gt;&lt;a href="http://www.oredev.org/"&gt;Øredev&lt;/a&gt; (a conference and a sister company)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-3138372148231375782?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/3138372148231375782/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=3138372148231375782' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/3138372148231375782'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/3138372148231375782'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/joining-dotway.html' title='Joining Dotway!'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-6021540060666861009</id><published>2008-10-13T01:26:00.000-07:00</published><updated>2008-10-16T00:03:35.923-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='haskell'/><category scheme='http://www.blogger.com/atom/ns#' term='turtle'/><title type='text'>Turtle Graphics Basics</title><content type='html'>Some weeks ago, I watched &lt;a href="http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads/"&gt;Don't fear the Monads&lt;/a&gt;, again. Beckman said something like "there nothing about monads that you don't already know" and "after some time, you'd be inventing monads yourself, but you probably wouldn't know that they were monads". Beckman inspired me to start a small (!) project on &lt;a href="http://en.wikipedia.org/wiki/Turtle_graphics"&gt;Turtle Graphics&lt;/a&gt; blog posts.&lt;br /&gt;&lt;br /&gt;The idea is to write a very small piece of Haskell code in each post, where every post is a Turtle Graphics implementation. Or, not implementation, but rather just a model, so no real graphics. You can see them as small Turtle Graphics APIs, that focus entirerly on the data and not anything about presenting the data.&lt;br /&gt;&lt;br /&gt;Now, not every API is an internal &lt;a href="http://en.wikipedia.org/wiki/Domain_Specific_Language"&gt;DSL&lt;/a&gt;, but for these posts I'll try to put on my language sun-glasses..&lt;br /&gt;&lt;br /&gt;First we need a definition of a Turtle:&lt;br /&gt;&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;&gt; data Turtle = Turtle {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt;           x :: Double,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt;           y :: Double,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt;           alpha :: Double -- alpha = 0 means East&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt;   }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt;   deriving (Show)&lt;/span&gt;&lt;/pre&gt;The first idea (the trivial/naïve implementation) is to define functions with the type signatures &lt;span style="font-family:courier new;"&gt;Turtle -&gt; Turtle&lt;/span&gt;. The functions I have in mind is &lt;span style="font-style: italic;"&gt;go&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;left&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;right&lt;/span&gt;, and &lt;span style="font-style: italic;"&gt;rotate&lt;/span&gt;, hopefully all self-explanatory. Let's do a type synonym, because it's fun:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; type Command = Turtle -&gt; Turtle&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here are the commands, very basic:&lt;pre&gt;&lt;span style="font-family:courier new;"&gt;&gt; go, left, right :: Command&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; go t = t { x = x t + step * cos (alpha t),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt;            y = y t + step * sin (alpha t),&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt;            alpha = alpha t }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; left t = t { alpha = alpha t + pi/2 }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; right t = t { alpha = alpha t - pi/2 }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; rotate :: Double -&gt; Command&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; rotate v t = t { alpha = alpha t + v }&lt;br /&gt;&lt;br /&gt;&gt; step = 1&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;Note that &lt;span style="font-family:courier new;"&gt;rotate&lt;/span&gt; has a different type. Also note the order of the arguments: we can use currying so that if we give rotate just one argument (an angle), we will get a &lt;span style="font-family:courier new;"&gt;Command&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Let us just see what the user code looks like:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; let t = Turtle {x = 1, y = 1, alpha = 0}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; t&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Turtle {x = 1.0, y = 1.0, alpha = 0.0}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; go t&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Turtle {x = 2.0, y = 1.0, alpha = 0.0}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We can combine functions with the operator &lt;span style="font-family:courier new;"&gt;(.)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; (go . go) t&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Turtle {x = 3.0, y = 1.0, alpha = 0.0}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And, of course, we can define new functions in terms of the basic ones:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; let go2 = go . go&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; go2 t &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Turtle {x = 3.0, y = 1.0, alpha = 0.0}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The presentation is a bit rough, especially for non-zero angles..&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; right t&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Turtle {x = 1.0, y = 1.0, alpha = -1.5707963267948966}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;..but I assume you can calculate from radians to degrees in your head.. ;)&lt;br /&gt;&lt;br /&gt;As you might have seen, the ordering of arguments for &lt;span style="font-family:courier new;"&gt;rotate&lt;/span&gt; was important:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; (go . (rotate pi) . go) t&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Turtle {x = 1.0, y = 1.0000000000000002, alpha = 3.141592653589793}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If the ordering of arguments were different, the above would not have been possible to write. Instead we would have been forced to write a lambda-expression, which is maybe not the best example of a good user code (if you can avoid it, at least).&lt;br /&gt;&lt;br /&gt;Note that we also can remove the definitions of &lt;span style="font-family:courier new;"&gt;left&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;right&lt;/span&gt;, and replace them with&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; left' = rotate (pi/2)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; right' = rotate (-pi/2)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now, a nice feature would be to let a user give a list of &lt;span style="font-family:courier new;"&gt;Command&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;s&lt;/span&gt; and a &lt;span style="font-family:courier new;"&gt;Turtle&lt;/span&gt; to a function, returning the final position:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; runR :: [Command] -&gt; Turtle -&gt; Turtle&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; runR [] t = t&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; runR (c:cs) t = runR cs (c t)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here's where I think it can be useful to know something about functions is general. For example, functions with type &lt;span style="font-family:courier new;"&gt;a -&gt; a&lt;/span&gt; forms a monoid [&lt;a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html"&gt;1&lt;/a&gt;,&lt;a href="http://en.wikipedia.org/wiki/Monoid"&gt;2&lt;/a&gt;] together with the operator &lt;span style="font-family:courier new;"&gt;(.)&lt;/span&gt; and identity &lt;span style="font-family:courier new;"&gt;id&lt;/span&gt;. This allows us to write &lt;span style="font-family:courier new;"&gt;runR&lt;/span&gt; in a more readable form (now called &lt;span style="font-family:courier new;"&gt;run&lt;/span&gt;):&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; run :: [Command] -&gt; Turtle -&gt; Turtle&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; run cs t = (foldr (.) id cs) t&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A monoid is a nice thing to be, but what is really the purpose with the &lt;span style="font-family:courier new;"&gt;run&lt;/span&gt; function? Isn't the whole goal with a "Turtle Graphics" that we compute a &lt;span style="font-style: italic;"&gt;list of positions&lt;/span&gt;, so that we can plot them somewhere? It's no use just to calculate a final position: the picture will become pretty boring eventually.. ;)&lt;br /&gt;&lt;br /&gt;What we need is a function that takes a list of commands and a start position, returning a list of positions:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; runHistory :: [Command] -&gt; Turtle -&gt; [Turtle]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We make &lt;span style="font-family:courier new;"&gt;runHistory &lt;/span&gt;a wrapper function around &lt;span style="font-family:courier new;"&gt;runToList&lt;/span&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; runHistory cs t = runToList cs [t]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; runToList :: [Command] -&gt; [Turtle] -&gt; [Turtle]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; runToList [] ts = ts&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; runToList _ [] = [] &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; runToList (c:cs) (t:ts) =  runToList cs ((c t) : t : ts)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;Update&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Joel pointed out (in the comments) that &lt;span style="font-family:courier new;"&gt;runHistory&lt;/span&gt; could be written as:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&gt; runHistory = flip (scanr ($)) . reverse&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It's of course a &lt;span style="font-style: italic;"&gt;lot&lt;/span&gt; better than my definition(s), from a readable code perspective. I imagine that my version is more performant (since I do not &lt;span style="font-family:courier new;"&gt;reverse&lt;/span&gt;), but as we all know, performance only matters until you notice the problem (as I pointed out in the comments myself). So, I fell into the trap of pre-optimizing.. Oh my, oh my.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;/Update&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let's try it out:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; runHistory [go,right,rotate pi, go,go] t&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;[Turtle {x = 2.0, y = 3.0, alpha = 1.5707963267948966},Turtle {x = 2.0, y = 2.0, alpha = 1.5707963267948966},Turtle {x = 2.0, y = 1.0, alpha = 1.5707963267948966},Turtle {x = 2.0, y = 1.0, alpha = -1.5707963267948966},Turtle {x = 2.0, y = 1.0, alpha = 0.0},Turtle {x = 1.0, y = 1.0, alpha = 0.0}]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Hmm, the presentation gets real ugly. Perhaps an implementation of &lt;span style="font-family:courier new;"&gt;(Show)&lt;/span&gt; would be nice, but that's out of scope for this post.&lt;br /&gt;&lt;br /&gt;I think there's some &lt;a href="http://martinfowler.com/bliki/CodeSmell.html"&gt;smelly code&lt;/a&gt; here. We have "bolted on" a history feature on top of the basic commands, so the only knowledge of a history (or log) is in &lt;span style="font-family:courier new;"&gt;runHistory&lt;/span&gt; itself. From a logging perspective, the basic functions &lt;span style="font-style: italic;"&gt;do not compose&lt;/span&gt; &lt;span style="font-style: italic;"&gt;at all&lt;/span&gt;! Let me show you what I mean..&lt;br /&gt;&lt;br /&gt;Suppose we define a &lt;span style="font-family:courier new;"&gt;go2&lt;/span&gt; in terms of &lt;span style="font-family:courier new;"&gt;go&lt;/span&gt; (see above). Clearly, we would like &lt;span style="font-family:courier new;"&gt;go2&lt;/span&gt; to write to the log twice. With the current implementation, this is impossible! There's no way that &lt;span style="font-family:courier new;"&gt;runHistory&lt;/span&gt; can write more than one time to the log, per command. Well, it could be done, but it's not pretty:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; let go2 = [go,go]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;(*Main&gt; let prog = [go,go2,go] -- type error)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; let prog = [[go],go2,[go]]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; runHistory (concat prog) t&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Even uglier:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; let go3 = [go2,[go]]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;(*Main&gt; let prog = [[go],go3,[go]] -- type error)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; let prog = [[[go]],go3,[[go]]]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;*Main&gt; runHistory (concat (concat prog)) t&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;From this, it's obvious that we need to let the basic functions be aware of history/logging. That would be the goal of future posts.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Comment/spoiler: &lt;a href="http://www.kennknowles.com/"&gt;Kenn Knowles&lt;/a&gt; has also written a &lt;a href="http://www.kennknowles.com/blog/2008/04/16/drawing-fractals-in-haskell-with-a-cursor-graphics-dsel-and-a-cute-list-representation/"&gt;blog post&lt;/a&gt; on a Turtle Graphics implementation. Though, the aim of my post(s) is to start out real basic and then evolve the code through several posts. Basically, Kenn's implementation is a "state-over-writer" &lt;a href="http://en.wikibooks.org/wiki/Haskell/Monad_transformers"&gt;monad transformer&lt;/a&gt;, with a self-implemented &lt;a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/dlist"&gt;difference list&lt;/a&gt; as the writer monoid.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-6021540060666861009?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/6021540060666861009/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=6021540060666861009' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/6021540060666861009'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/6021540060666861009'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/turtle-graphics-basics.html' title='Turtle Graphics Basics'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-7176639933921468545</id><published>2008-10-08T10:17:00.000-07:00</published><updated>2008-10-16T02:17:47.900-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Mocks'/><category scheme='http://www.blogger.com/atom/ns#' term='DI'/><category scheme='http://www.blogger.com/atom/ns#' term='OO'/><title type='text'>Better DI</title><content type='html'>I was watching "&lt;a href="http://www.infoq.com/presentations/Mock-Objects-Nat-Pryce-Steve-Freeman"&gt;InfoQ: Mock Roles Not Objec&lt;/a&gt;&lt;a href="http://www.infoq.com/presentations/Mock-Objects-Nat-Pryce-Steve-Freeman"&gt;t States&lt;/a&gt;" with Nat Pryce and Steve Freeman yesterday. I had read the &lt;a href="http://www.jmock.org/oopsla2004.pdf"&gt;paper&lt;/a&gt; previous to watching the video, and I pretty much expected the same content but in a different form. Boy was I wrong..&lt;br /&gt;&lt;br /&gt;In particular, they talked a few minutes on an argument against &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;Dependency Injection&lt;/a&gt; (DI), namely that the constructor gets lots and lots of arguments. I used DI the first time a couple of weeks ago, so I'm pretty new to it, and I wasn't sure I was doing exactly the right thing. And, yes, the constructors at some places became huge.&lt;br /&gt;&lt;br /&gt;What the InfoQ presentation thought me was that not all interfaces a class use are  &lt;span style="font-style: italic;"&gt;dependencies&lt;/span&gt; per se. Classes can for example be equipped with default parts or policies, that can be changed after creation. The important thing is that the instance will work anyway, which is not the case if an instance is missing out a dependency.&lt;br /&gt;&lt;br /&gt;This nice picture (screenshot from their slides) made it click for me:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_lKbnbukR-QY/SOzv_Bj-kwI/AAAAAAAAAAg/7HbIvJA3WSo/s1600-h/mock_di.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://1.bp.blogspot.com/_lKbnbukR-QY/SOzv_Bj-kwI/AAAAAAAAAAg/7HbIvJA3WSo/s320/mock_di.png" alt="" id="BLOGGER_PHOTO_ID_5254838731264660226" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Big thanks to &lt;a href="http://www.johannormen.com/blog/"&gt;Johan Normén&lt;/a&gt; who mentioned the &lt;a href="http://www.jmock.org/oopsla2004.pdf"&gt;paper&lt;/a&gt; on the &lt;a href="http://www.swenug.com/"&gt;SweNug&lt;/a&gt; meeting two weeks ago!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-7176639933921468545?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/7176639933921468545/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=7176639933921468545' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/7176639933921468545'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/7176639933921468545'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/10/better-di.html' title='Better DI'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_lKbnbukR-QY/SOzv_Bj-kwI/AAAAAAAAAAg/7HbIvJA3WSo/s72-c/mock_di.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-7807768936379320549</id><published>2008-09-23T23:37:00.001-07:00</published><updated>2008-10-28T06:09:46.074-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='F#'/><title type='text'>A thought on F#</title><content type='html'>A while ago I read &lt;a href="http://wadler.blogspot.com/2008/09/caml-trading.html"&gt;this blogpost&lt;/a&gt; by &lt;a href="http://homepages.inf.ed.ac.uk/wadler/"&gt;Phil Wadler&lt;/a&gt;. The post is a comment on an &lt;a href="http://journals.cambridge.org/action/displayIssue?jid=JFP&amp;amp;volumeId=18&amp;amp;issueId=04"&gt;article&lt;/a&gt; from an issue of Journal of Functional Programming. In that, Yaron Minsky and Stephen Weeks from &lt;a href="http://www.janestcapital.com/"&gt;Jane Street Capital&lt;/a&gt; talks about their experiences with functional programming on Wall Street. Wadler quotes from the article:&lt;br /&gt;&lt;blockquote&gt;When we first tried switching over from VB to C#, one of the most disturbing features of the language for the partners who read the code was inheritance. They found it difficult to figure out which implementation of a given method was being invoked from a given call point, and therefore, difficult to reason about the code. It is worth mentioning that OCaml actually does support inheritance as part of its object system. That said, objects are an obscure part of the language, and inheritance even more so. At Jane Street, we almost never use objects and never use inheritance. We use standard functional programming techniques and code reviewers find that style more comprehensible. In particular, they can reason by following static properties of the code (module boundaries and functor applications) rather than dynamic properties (what class an object is).&lt;/blockquote&gt;&lt;br /&gt;This made me think of F#, which is inspired by OCaml. Will users of F# tend to not use any OO, just as in Jane Street Capital? Or, put in another way, will F# users tend to only be &lt;span style="font-style: italic;"&gt;consumers&lt;/span&gt; of OO code (i.e., the .NET framework) and provide a "nice" OO-style API for client code, but "underneath the surface" focus entirerly on the functional part of F#?&lt;br /&gt;&lt;br /&gt;Media: Wadler does a funny thing, in the very last minute of his talk (&lt;a href="http://www.youtube.com/watch?v=2t9q-936yxw"&gt;Faith, Evolution, and Programming Languages&lt;/a&gt;).&lt;br /&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-7807768936379320549?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/7807768936379320549/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=7807768936379320549' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/7807768936379320549'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/7807768936379320549'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/09/thought-on-f.html' title='A thought on F#'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-1481862983187853884</id><published>2008-09-23T00:14:00.000-07:00</published><updated>2008-09-23T01:09:55.906-07:00</updated><title type='text'>Attending events</title><content type='html'>Tomorrow I will &lt;a href="http://www.swenug.com/blogs/goteborg/archive/2008/09/17/swenug-gbg-bjuder-p-229-eventet-b-228-ttre-kod-i-net.aspx"&gt;attend&lt;/a&gt; &lt;a href="http://www.swenug.com/blogs/goteborg/default.aspx"&gt;SweNug Gbg&lt;/a&gt; for the first time. Exciting! I had a real good time at the &lt;a href="http://dotway.se/Default.aspx?tabid=271"&gt;open space&lt;/a&gt; (in Gothenburg) one month ago and I'm hoping that tomorrow will be fun as well. But it's not without sacrifices: I'll have to catch the earliest train to Ystad (@ 05.40) to attend a driving lesson on Thursday.&lt;br /&gt;&lt;br /&gt;On 3 October, I will attend Microsofts &lt;a href="http://www.microsoft.com/sverige/msdn/events/factor10/"&gt;half-day on data access&lt;/a&gt;, in Malmö, where &lt;a href="http://research.microsoft.com/%7Eemeijer/"&gt;Erik Meijer&lt;/a&gt; and &lt;a href="http://jimmynilsson.com/blog/"&gt;Jimmy Nilsson&lt;/a&gt; will talk. Erik Meijer is one of the (many) reasons that I chose to focus on .NET rather than Java. After you read this post, go and read all his papers! :)&lt;br /&gt;&lt;br /&gt;A funny quote from the event page:&lt;br /&gt;&lt;blockquote&gt;&lt;span class="brodtext"&gt;[...] in the first half of the last century,         mathematicians invented monads, which have         subsequently migrated to the computer science         mainstream via functional programming (mainly         Haskell) and have recently shown up as LINQ in C#         3.0 and Visual Basic. &lt;/span&gt;&lt;/blockquote&gt;Jimmy Nilsson is the author of &lt;a href="http://www.amazon.com/Applying-Domain-Driven-Design-Patterns-Examples/dp/0321268202"&gt;Applying Domain-Driven Design and Patterns&lt;/a&gt;, which I read this summer, great book! And, by the way, Jimmy is the &lt;a href="http://www.computersweden.idg.se/2.2683/1.159414"&gt;second best developer in Sweden&lt;/a&gt; (rated by ComputerSweden).&lt;br /&gt;&lt;br /&gt;Finally, a note-to-self: try to write shorter posts and more often.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-1481862983187853884?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/1481862983187853884/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=1481862983187853884' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1481862983187853884'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1481862983187853884'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/09/attending-events.html' title='Attending events'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-4290231124073812202</id><published>2008-09-06T03:56:00.001-07:00</published><updated>2008-09-06T11:10:36.067-07:00</updated><title type='text'>ClickOnce deployment or not?</title><content type='html'>Some weeks ago, I developed a small program in C# called P4 Explorer. To distribute it, i used a technique called &lt;a href="http://msdn.microsoft.com/en-us/library/t71a733d%28VS.80%29.aspx"&gt;ClickOnce deployment&lt;/a&gt;, which is a part of Visual Studio C# Express 2008. This blog post will be about what ClickOnce deployments are, how they can be used, and why you in the end shouldn't use them...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:180%;"&gt;ClickOnce&lt;/span&gt;&lt;br /&gt;ClickOnce deployments will simplify your deployment a lot. To deploy your application, all you need to do is:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;specify the location to publish the application (i.e. a network drive, web site or ftp),&lt;/li&gt;&lt;li&gt;specify from what location the users will install it from (propably the same as 1), and&lt;br /&gt;&lt;/li&gt;&lt;li&gt;decide whether the application should be installed at the start menu, or always must be started from the install path (2).&lt;/li&gt;&lt;/ol&gt;Furhermore you can specify if the application automatically should check for updates, which is really great if you plan to update your application frequently. You then specify how often the it should check for updates, and when an update is found, the user will be notified and can download the latest version. All by only selecting "The application should check for updates". Very nice!&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_F5z3mmdTM8I/SMJyks0BXsI/AAAAAAAAB_Y/uovBNC4xwJE/s1600-h/ClickOnce_Updates.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://4.bp.blogspot.com/_F5z3mmdTM8I/SMJyks0BXsI/AAAAAAAAB_Y/uovBNC4xwJE/s320/ClickOnce_Updates.jpg" alt="" id="BLOGGER_PHOTO_ID_5242878891042889410" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Now, for you to deploy a new version, all you do is click "Publish". When the user then starts the program, the update will be found and installed (if permitted by the user). Very easy!&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-size:180%;"&gt;Limitations&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Application Path&lt;/span&gt;&lt;br /&gt;ClickOnce deployment doesn't come for free though. First of, the application won't be installed under "C:\Program Files\MyCompany\MySoftware" as you might expect, but at some secret place (hidden from the user and developer). If you decide to let the installer add a shortcut to your start menu, there is no way to find out where this shortcut points, as it's not a normal shortcut. Right clicking on it and selecting properties will not give you the information you expect, i.e. the path to the executable.&lt;br /&gt;Now, there is a simple way of finding out the path of the executable. When starting the installed application, the property Application.ExecutablePath will be correctly set and you can thus be used to, for example, add to the PATH environment variable:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;void UpgradePathVariable()&lt;br /&gt;{&lt;br /&gt;    string l_ApplicationFolder = Path.GetDirectoryName(Application.ExecutablePath);&lt;br /&gt;    string l_PathVariable = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);&lt;br /&gt;    if (!l_PathVariable.Contains(l_ApplicationFolder))&lt;br /&gt;    {&lt;br /&gt;        l_PathVariable = l_OldPathVariable + ";" + l_ApplicationFolder;&lt;br /&gt;    }&lt;br /&gt;    Environment.SetEnvironmentVariable("PATH", l_PathVariable, EnvironmentVariableTarget.Machine);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Parameters&lt;/span&gt;&lt;br /&gt;Also, because the program doesn't have a fixed installation path, there is no support for sending parameters to the program. I.e. args in static void Main(string[] args) will always be empty. The documentation at MSDN states that the only way to send parameters to a click once application is to deploy it at a web page, and let the user send html-parameters to it, i.e.:&lt;br /&gt;http://www.yourdomain.com/yourapplication.exe?parameter1=val1&amp;amp;parameter2=val2&lt;br /&gt;But&lt;br /&gt;&lt;ol&gt;&lt;li&gt;this requires your application to be deployed using a web page, and&lt;/li&gt;&lt;li&gt;you must always download the installer every time you need to sent parameters to the program.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;Say for example you want to let the user start the program by right clicking on a file in explorer and selecting "Open in my program". You would need to add a line in your registry pointing to the executable file:&lt;br /&gt;&lt;br /&gt;pathtoyourexe "%1"&lt;br /&gt;&lt;br /&gt;This would be both slow and hacky if using the recommended http-style mentioned above. Instead, by using the Application.ExecutablePath property, you can actually access the physical path to the installed executable, and thus add this path to the registry. With this setup correctly, right clicking on a file and selecting your program to open it with, will open the program and send the parameter correctly to it!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Settings&lt;/span&gt;&lt;br /&gt;But, even though everything might seem to work out fine, you will soon notice that the Settings object defined in your application will be different if starting the program using the shortcut from your start menu, or right clicking on a file. They are not sharing the same application settings. And if you upgrade the program, all settings in your "right clicked version" will be gone, because the new version will be installed in a new folder.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:180%;"&gt;In the end&lt;/span&gt;&lt;br /&gt;Even though click once applications are really easy to deploy, and give you a lot for free, the limitations just makes them really hard to work with. Making simple applications might work, but as soon as you want the user to interact with it through the explorer shell, you are better off making your own installer (or using a free one such as &lt;a href="http://nsis.sourceforge.net/"&gt;NSIS&lt;/a&gt;). So in the end I decided to deploy my program manually using a source control, which made all my problems go away. Sending parameters worked, settings worked, and the application had a fixed folder!&lt;br /&gt;&lt;br /&gt;The only thing I was really missing from ClickOnce was the ability for the program to automatically check for updates, which was something I really needed to make sure everyone using my application had the latest version (hopefully having less bugs in it). But it turned out this wasn't so hard to make on my own! The pseudo code for it, printed here, is divided in two programs - your main application, yourapp.exe, and the upgrade application, upgrade.exe.&lt;br /&gt;&lt;br /&gt;yourapp.exe:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Is there a new version available (i.e. check using source control)&lt;/li&gt;&lt;li&gt;Copy upgrade.exe (and dependencies) to a new folder "temp"&lt;/li&gt;&lt;li&gt;Start new process temp\upgrade.exe&lt;/li&gt;&lt;li&gt;Close program&lt;/li&gt;&lt;/ol&gt;upgrade.exe:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Make sure all instances of yourapp.exe are closed&lt;/li&gt;&lt;li&gt;Download the latest version (i.e. using source control)&lt;/li&gt;&lt;li&gt;Start new process ..\&lt;yourapplication&gt;yourapp.exe&lt;/yourapplication&gt;&lt;/li&gt;&lt;li&gt;Close program&lt;/li&gt;&lt;/ol&gt;You can always make it faster by running the check for new version in it's own thread, and only checking every x hour, but the main thing is that I get the same features as if running with ClickOnce, but don't need to limit my application as much as you do with ClickOnce applications!&lt;br /&gt;&lt;br /&gt;So, ClickOnce, good for small application, not good for more advanced applications.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-4290231124073812202?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/4290231124073812202/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=4290231124073812202' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/4290231124073812202'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/4290231124073812202'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/09/clickonce-deployment-or-not.html' title='ClickOnce deployment or not?'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_F5z3mmdTM8I/SMJyks0BXsI/AAAAAAAAB_Y/uovBNC4xwJE/s72-c/ClickOnce_Updates.jpg' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-1742580053754400192</id><published>2008-08-29T04:27:00.000-07:00</published><updated>2008-10-28T06:10:59.890-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='podcasts'/><title type='text'>Podcasts</title><content type='html'>I admit it: I'm a podcast junkie! When running, commuting, walking, whatever: my iPod Nano (2nd generation) is with me practically everywhere.&lt;br /&gt;&lt;br /&gt;Here's some podcasts that I follow. Most of them are followed by recommended episodes. Enjoy!&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.hanselminutes.com/"&gt;Hanselminutes&lt;/a&gt;&lt;br /&gt;-&lt;a href="http://www.hanselminutes.com/default.aspx?showID=137"&gt; What is Done? - A Conversation with Scrum Co-Creator Ken Schwaber&lt;/a&gt;&lt;br /&gt;-&lt;a href="http://www.hanselminutes.com/default.aspx?showID=136"&gt; Lean Software Development with Tom and Mary Poppendieck&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.hanselminutes.com/default.aspx?showID=121"&gt;Quetzal Bradley on Testing after Unit Tests&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.se-radio.net/"&gt;Software Engineering Radio&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.se-radio.net/podcast/2008-08/episode-108-simon-peyton-jones-functional-programming-and-haskell"&gt;Simon Peyton Jones on Functional Programming and Haskell&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.se-radio.net/podcast/2008-07/episode-105-retrospectives-linda-rising"&gt;Retrospectives with Linda Rising&lt;/a&gt; (This almost made me cry, seriously!)&lt;br /&gt;- &lt;a href="http://www.se-radio.net/podcast/2008-05/episode-95-new-guardiancouk-website-matt-wall-and-erik-doernenburg"&gt;The New Guardian.co.uk website with Matt Wall and Erik DoernenBurg&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://deepfriedbytes.com/"&gt;Deep Fried Bytes&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://deepfriedbytes.com/podcast/episode-6-talking-domain-driven-design-with-david-laribee-part-1/"&gt;Talking Domain-Driven Design with David Laribee - Part 1&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://deepfriedbytes.com/podcast/episode-7-talking-domain-driven-design-with-david-laribee-ndash-part-2/"&gt;Talking Domain-Driven Design with David Laribee - Part 2&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.pragprog.com/podcasts"&gt;Pragmatic Podcasts&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://podcasts.pragprog.com/2008-05/andy-hunt-on-pragmatic-wetware.mp3"&gt;Andy Hunt on Pragmatic Wetware&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://podcasts.pragprog.com/2008-05/dave-thomas-on-pragmatic-publishing.mp3"&gt;Dave Thomas on Pragmatic Publishing&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.thoughtworks.com/what-we-say/podcasts.html"&gt;ThoughtWorks - IT Matters Podcast&lt;/a&gt;&lt;br /&gt;- Domain Specific Languages - Part 1 of 2 (no permalink)&lt;br /&gt;- Domain Specific Languages - Part 2 of 2 (no permalink)&lt;br /&gt;&lt;br /&gt;&lt;a href="http://altnetpodcast.com/"&gt;Alt.NET podcast&lt;/a&gt;&lt;br /&gt;- (Listen to all episodes..)&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.netobjectives.com/blog-categories/Lean-Agile%2BStraight%2BTalk"&gt;Lean Agile Straight Talk&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.netobjectives.com/blogs/overcoming-impediments-to-tdd-test-driven-development"&gt;Overcoming Impediments to Test-Driven Development&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.netobjectives.com/blogs/tdd-test-driven-development-design-patterns"&gt;Test-Driven Development and Design Patterns&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.microsoft.com/sverige/msdn/radio/default.mspx"&gt;MSDN Radio&lt;/a&gt; (In Swedish. The homepage is not up to date. &lt;a href="http://buzzfrog.blogs.com/"&gt;Dag König's blog&lt;/a&gt; links to more episodes.)&lt;br /&gt; - &lt;a href="http://buzzfrog.blogs.com/zabrak/2008/06/msdn-radio-so-1.html"&gt;Magnus Mårtensson, Dotway&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://agiletoolkit.libsyn.com/"&gt;Agile Toolkit Podcast&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://agiletoolkit.libsyn.com/index.php?post_id=187523"&gt;Ruby Dave&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://agiletoolkit.libsyn.com/index.php?post_id=252431"&gt;Smalltalk Dave&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetrocks.com"&gt;.NET Rocks!&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=266"&gt;Jon Harrop Makes Us F#&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=362"&gt;James Kovacs Inverts our Control&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=356"&gt;XML Literals Panel from TechEd 2008&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.informit.com/podcasts/channel.aspx?c=DADF92CA-3BDC-484E-9CD8-CBFE0CFC0DE6"&gt;OnSoftware&lt;/a&gt; (Video, so this I watch at the computer)&lt;br /&gt;- &lt;a href="http://www.informit.com/podcasts/episode.aspx?e=e5aaf8fb-9502-4fc4-ab97-1f9d309ff5ce"&gt;F#&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Also, &lt;a href="http://channel9.msdn.com/"&gt;Channel 9&lt;/a&gt; has great video content!&lt;br /&gt;- &lt;a href="http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads/"&gt;Brian Beckman: Don't Fear the Monads&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://channel9.msdn.com/shows/Going+Deep/Anders-Hejlsberg-Herb-Sutter-Erik-Meijer-Brian-Beckman-Software-Composability-and-the-Future-of/"&gt;Anders Hejlsberg, Herb Sutter, Erik Meijer, Brian Beckman: Software Composability and the Future of Programming Languages&lt;/a&gt;&lt;br /&gt;-&lt;a href="http://channel9.msdn.com/posts/Charles/Brian-Beckman-Monads-Monoids-and-Mort/"&gt; Brian Beckman: Monads, Monoids, and Mort&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-1742580053754400192?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/1742580053754400192/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=1742580053754400192' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1742580053754400192'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/1742580053754400192'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/08/podcasts.html' title='Podcasts'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-2586354859704871468</id><published>2008-08-08T01:55:00.001-07:00</published><updated>2008-08-08T05:09:41.504-07:00</updated><title type='text'>Google Calendar</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_F5z3mmdTM8I/SJw3dcJjqFI/AAAAAAAAByU/LVdTb_rV5is/s1600-h/google+calendar.bmp"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://3.bp.blogspot.com/_F5z3mmdTM8I/SJw3dcJjqFI/AAAAAAAAByU/LVdTb_rV5is/s320/google+calendar.bmp" alt="" id="BLOGGER_PHOTO_ID_5232117846009686098" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;img src="file:///C:/DOCUME%7E1/chrgen/LOCALS%7E1/Temp/moz-screenshot.jpg" alt="" /&gt;&lt;br /&gt;I love &lt;a href="http://www.google.com/calendar"&gt;Google Calendar&lt;/a&gt;, mostly because of the free sms service, sending you an sms x minutes before an event. What's the point of otherwise having a calendar if you only get notifications from it when sitting in front of a computer, such as how ms exchange and similar works. "You need to be at the dentist in 30 minutes", well hopefully I'm sitting in front of the computer when this message pops up. Of course you can always install a mail service in your mobile, and receive a mail when an appointment is about to happened, and you can buy an expensive mobile phone having it's own calendar which you then can sync with the computer from time to time. But for me, I just want to say "at that time, I need to be there, and wherever I am 30 minutes before that, notify me". That's why I love google calendar, because you can be anywhere and still get a notification!&lt;br /&gt;&lt;br /&gt;Now, that's all glorious and so, but as always there are some problems - I want to be able to sync it with other calendars, so that I don't need to open up a browser, enter the adress, and log in every time I need to add an event. For &lt;a href="http://www.mozilla.com/en-US/thunderbird/"&gt;Mozilla Thunderbird&lt;/a&gt; there is an add on to support calendars called &lt;a href="http://www.mozilla.org/projects/calendar/lightning/"&gt;Lighting&lt;/a&gt;, and for this add on there is another one called &lt;a href="https://addons.mozilla.org/en-US/thunderbird/addon/4631"&gt;Provider for Google Calendar&lt;/a&gt;, which makes it possible to view and update the google calendar from the calendar within Thunderbird. Niiice!&lt;br /&gt;&lt;br /&gt;But hey, wait a minute, the reason I wanted to support google calendar in the first place was because of it's sms service, but as it turns out, the google calendar provider for thunderbird doesn't support this :( So in the end I still need to access google calendar from within a web-browser, and change the settings of all my events manually.&lt;br /&gt;&lt;br /&gt;So that's where I'm now, still looking for the perfect solution. Google calendar is still the best option for me, but if you know about a better one then please let me know...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-2586354859704871468?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/2586354859704871468/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=2586354859704871468' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/2586354859704871468'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/2586354859704871468'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/08/google-calendar.html' title='Google Calendar'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_F5z3mmdTM8I/SJw3dcJjqFI/AAAAAAAAByU/LVdTb_rV5is/s72-c/google+calendar.bmp' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-8448506328056291455</id><published>2008-06-02T05:15:00.000-07:00</published><updated>2008-06-02T07:13:39.541-07:00</updated><title type='text'>Hacking VS C# 2008 Express</title><content type='html'>&lt;a href="http://www.microsoft.com/express/vcsharp/"&gt;VS C# 2008 Express&lt;/a&gt; is a really nice product in many ways - you've got most of the stuff you need, but most of all, it's free!&lt;br /&gt;&lt;br /&gt;Though, I was playing around for it a bit, and needed to support compiling against a specific platform (x86 instead of Any CPU which is default). The reason for this was that, in my project, I was refering to a dll written in managed c++ - compiled for x86! Mixing x64 and x86 is never a good solution, and as C# projects per default compiles against Any CPU they will run in 64-bits mode on a 64-bit computer. When the application tries to load the 32-bit dll, the application crashes...&lt;br /&gt;&lt;br /&gt;Now, in VS C# 2008 &lt;span style="font-weight: bold;"&gt;Express&lt;/span&gt;, you &lt;span style="font-weight: bold;"&gt;cannot&lt;/span&gt; specify another target platform but the Any CPU platform. Guess they think that "if you need support for specific platforms, you should be able to pay for a commercial version".&lt;br /&gt;&lt;br /&gt;I was reading through MSDN, and found this note:&lt;br /&gt;&lt;blockquote&gt;Note   /platform is not available in the development environment in Visual C# Express.&lt;/blockquote&gt;&lt;br /&gt;"Damn", I thought, "this is the end. But what does &lt;span style="font-style: italic;"&gt;development environment&lt;/span&gt; mean?". I had to try it out manually using the command prompt, so I compiled the project in VS and copied the command line output from the output window, being something like:&lt;br /&gt;&lt;pre&gt;C:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 ...&lt;br /&gt;&lt;/pre&gt;I put the line in a batch file, and added the /target:x86 flag, compiled it and... it worked! The project got compiled using x86 as a target platform.&lt;br /&gt;&lt;br /&gt;As it turns out, VS C# 2008 Express &lt;span style="font-weight: bold;"&gt;does&lt;/span&gt; support the /target option, and probably others as well. They only lack the support for the option in the IDE, which is kinda a hacky way of disabling a feature, IMO.&lt;br /&gt;&lt;br /&gt;Now, I don't want to build the project using a batch file every time, I would like to build it inside VS IDE so that others can easily build it. As the VS project files are nothing but XML, you can easily open them up in any editor you like to see their contents. I did this to find out if there was some way to add a target flag to the compiler, but unfortunately I couldn't find anything interesting (of course that would be &lt;span style="font-style: italic;"&gt;too&lt;/span&gt; easy!). I continued with searching the web, and found this strange property:&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-weight: bold;"&gt;CSharpProjectConfigurationProperties3.PlatformTarget Property&lt;/span&gt;&lt;br /&gt;This member provides internal-only access to C# project configuration properties.&lt;br /&gt;...&lt;br /&gt;External components can access these properties through the Properties collection for the appropriate Visual Studio automation object.&lt;br /&gt;&lt;/blockquote&gt;"Well", I said to my self, "the &lt;span style="font-style: italic;"&gt;Properties collection&lt;/span&gt; would probably refer to the XML properties found in the project file, so once again I opened up the project file, and browsed to two property groups being:&lt;br /&gt;&lt;pre&gt;&amp;lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "&amp;gt;&lt;/pre&gt;and&lt;br /&gt;&lt;pre&gt;&amp;lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "&amp;gt;&lt;/pre&gt;In these groups, I added the PlatformTarget property:&lt;br /&gt;&lt;pre&gt;&amp;lt;PlatformTarget&amp;gt;x86&amp;lt;/PlatformTarget&amp;gt;&lt;/pre&gt; I saved the file, reloaded it in VS, recompiled the project... and viola, it worked! All you needed to do was to find out the magic property to add to the project file! :)&lt;br /&gt;&lt;br /&gt;This proves that C# Express contains more under the hood than what's visible to the eye. With a simple editor, and some knowledge on how to search MSDN, you can get a lot out of this product!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-8448506328056291455?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/8448506328056291455/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=8448506328056291455' title='15 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/8448506328056291455'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/8448506328056291455'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/06/hacking-vs-c-2008-express.html' title='Hacking VS C# 2008 Express'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>15</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-2788684090490038707</id><published>2008-05-23T07:15:00.000-07:00</published><updated>2008-05-23T14:26:36.265-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='haskell'/><category scheme='http://www.blogger.com/atom/ns#' term='lazy'/><category scheme='http://www.blogger.com/atom/ns#' term='fizzbuzz'/><title type='text'>Lazy FizzBuzz in Haskell</title><content type='html'>&lt;div&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51);font-family:'Trebuchet MS';font-size:100%;"  &gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;Christian has been writing too many FizzBuzzes [&lt;/span&gt;&lt;a href="http://coffeedrivendevelopment.blogspot.com/2008/05/c-fizzbuzz.html"&gt;&lt;span class="Apple-style-span"&gt;1&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt;,&lt;/span&gt;&lt;a href="http://coffeedrivendevelopment.blogspot.com/2008/05/c-lambda-lazy-list-fizzbuzz.html"&gt;&lt;span class="Apple-style-span"&gt;2&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt;,&lt;/span&gt;&lt;a href="http://coffeedrivendevelopment.blogspot.com/2008/05/fizzbuzz-with-f.html"&gt;&lt;span class="Apple-style-span"&gt;3&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt;] right now, without me responding! We also got a comment in the &lt;/span&gt;&lt;a href="http://coffeedrivendevelopment.blogspot.com/2008/04/ok-so-heres-my-first-real-post-its-spin.html"&gt;&lt;span class="Apple-style-span"&gt;first fizzbuzz post&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt; from &lt;/span&gt;&lt;a href="http://twitter.com/sganslandt"&gt;&lt;span class="Apple-style-span"&gt;sganslandt&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt; I've actually had the code for this post for two weeks now, but haven't had the time to finish it. &lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;The post is based on lazy evaluation, but the neat thing is that is the default behavior in Haskell, in contrast too most other languages. Among other things, lazy evaluation allows you to reason in another way when construcing you software.&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;Having lazy evaluation in a language makes it "kind of important" to have control over the side-effects, so lazy evaluation and pure functions live in symbiosis in Haskell. &lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;Let's start out with a regular divides..&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;pre&gt;&gt; divides m n = m `mod` n == 0&lt;br /&gt;&lt;/pre&gt;We then construct a function that takes a divisor, a string, a number, and returns a string. Note that the number is the last argument.&lt;br /&gt;&lt;pre&gt;&gt; genWord :: Int -&gt; String -&gt; Int -&gt; String&lt;br /&gt;&gt; genWord x str i&lt;br /&gt;&gt;  | i `divides` x = str&lt;br /&gt;&gt;  | otherwise = ""&lt;br /&gt;&lt;/pre&gt;Now the fun begins! We can generate infinite lists with "fizzes" and "buzzes", just by mapping genWord over an infinite list.&lt;br /&gt;&lt;pre&gt;&gt; fizzes = map (genWord 3 "Fizz") [1..]&lt;br /&gt;&gt; buzzes = map (genWord 5 "Buzz") [1..]&lt;br /&gt;&gt; nums = [1..]&lt;br /&gt;&lt;/pre&gt;So, for example, the first ten elements of fizzes looks like this:&lt;br /&gt;&lt;blockquote&gt;["","","Fizz","","","Fizz","","","Fizz",""]&lt;/blockquote&gt;Now, having three infinite lists, we need to combine those to one infinite list. Is this hard to digest? I guess it's not the "normal routine" that one follows when constructing imperative programs. So, how could this in any way be useful outside the esoteric world of academia? Let us talk a bit more on that after all code, focusing on one thing at a time. Here's combine:&lt;br /&gt;&lt;pre&gt;&gt; combine :: String -&gt; String -&gt; Int -&gt; String&lt;br /&gt;&gt; combine str1 str2 n&lt;br /&gt;&gt;  | null ret = show n&lt;br /&gt;&gt;  | otherwise = ret&lt;br /&gt;&gt;  where ret = str1 ++ str2&lt;br /&gt;&lt;/pre&gt;In other words, we concatenate the "fizz" string with the "buzz" string and if the concatenated string is empty, we return a number instead (as a string). This makes the case of "n%15" unnecessary, which is nice. The idea for creating combine is that if we know how to combine one element, we are much closer to a solution for our problem. Though, we do restrict ourselves when defining the type for combine. The only requirement for "n" is that it is showable, but the example is just much more clearer when not using parametrized types..&lt;br /&gt;&lt;br /&gt;A problem left to solve is the problem of having "a bunch of lists" and returning one list. The function zip does exactly that, and zip3 is function that takes three lists and returns a list of triples (source here).&lt;br /&gt;&lt;br /&gt;From ghc's Prelude documentation:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;    zip3 :: [a] -&gt; [b] -&gt; [c] -&gt; [(a, b, c)]&lt;br /&gt;zip3 takes three lists and returns a list of triples, analogous to zip.&lt;br /&gt;&lt;br /&gt;&lt;/blockquote&gt;Now, we could rewrite combine to take a triple instead of three arguments. Currying is perhaps not well-known concept, but essentially it is another way of applying arguments to a function:&lt;br /&gt;&lt;br /&gt;f(x,y) // Normal style (un-curried)&lt;br /&gt;f x y -- Curried&lt;br /&gt;&lt;br /&gt;So, we define a function, uncurry3, which takes a function (with three arguments) as the first argument, and returns a function which takes one argument, a triple. A funny thing is that the type declaration is longer than the implementation:&lt;br /&gt;&lt;pre&gt;&gt; uncurry3 :: (a -&gt; b -&gt; c -&gt; d) -&gt; ((a, b, c) -&gt; d)&lt;br /&gt;&gt; uncurry3 f (a,b,c) = f a b c&lt;br /&gt;&lt;/pre&gt;Here's the "grande finale"! We apply fizzes, buzzes and nums to zip3, giving an infinite list of triples. We then map the uncurried function (of the "third order") on that infinite list, giving an infinite list of fizzbuzzes with numbers in between.&lt;br /&gt;&lt;pre&gt;&gt; resultInf :: [String]&lt;br /&gt;&gt; resultInf = map (uncurry3 combine) (zip3 fizzes buzzes nums)&lt;br /&gt;&lt;/pre&gt;Finally, we take the first 100 elements of the infinite list, and print it to stdout.&lt;br /&gt;&lt;pre&gt;&gt; result100 :: [String]&lt;br /&gt;&gt; result100 = take 100 resultInf&lt;br /&gt;&lt;/pre&gt;&lt;pre&gt;&gt; printResult :: IO ()&lt;br /&gt;&gt; printResult = mapM_ putStrLn result100&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;The complete program is 15 lines of code, exclusive type type declarations, and I argue that the code is easy to read and understand. So, let us talk about why we want to create an esoteric and academic example like this, but first a quick recap..&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span style="font-family:courier new;"&gt;divides&lt;/span&gt; is the mathematical operator '|'&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span style="font-family:courier new;"&gt;genWord&lt;/span&gt; is a rule for generating words&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span style="font-family:courier new;"&gt;fizzes&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;buzzes&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;nums&lt;/span&gt; generates sets of values&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span style="font-family:courier new;"&gt;combine&lt;/span&gt; is a rule for combining three elements into one&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span style="font-family:courier new;"&gt;uncurry3&lt;/span&gt; converts a curried function to function of triples&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span style="font-family:courier new;"&gt;result100&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;printResult&lt;/span&gt; deals with presenting the result&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;Do you see the trace of math here? We treat the lists as sets and define some transformations which makes it easier to reason about the problem, in the spirit of &lt;/span&gt;&lt;a href="http://www.amazon.com/How-Solve-Mathematical-Princeton-Science/dp/069111966X/ref=pd_bbs_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1211551398&amp;amp;sr=8-1"&gt;&lt;span class="Apple-style-span"&gt;Poyla&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt;. We decompose the problem into smaller sub-problems, which makes it much more easy to reason about the program. &lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/Separation_of_concerns"&gt;&lt;span class="Apple-style-span"&gt;Separation of Concerns&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt; (SoC) is an OO principle that advocates exactly this.&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;When I think about it, OO has a lot of nice principles which improves the quality of the software, but I argue that OO is extremely hard to get right! Principles such as &lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/Law_of_Demeter"&gt;&lt;span class="Apple-style-span"&gt;LawOfDemeter&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt;, &lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/Separation_of_concerns"&gt;&lt;span class="Apple-style-span"&gt;SoC&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt;, &lt;/span&gt;&lt;a href="http://martinfowler.com/bliki/MinimalInterface.html"&gt;&lt;span class="Apple-style-span"&gt;MinimalInterface&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt; and the &lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;&lt;span class="Apple-style-span"&gt;Liskov substitution principle&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt;, are just a few principles which (often) make good OO software, but are hard to get right all at once. Further, I'm not convinced that the&lt;/span&gt;&lt;span class="Apple-style-span"&gt;se principles are discussed among developers in their daily work, if the developers don't have a "pattern background" or in other ways are &lt;/span&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span"&gt;interested&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"&gt; in OO design. Time pressure is probably the killer here. What is discussed on &lt;/span&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span"&gt;your&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"&gt; (developer) meetings at work?&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow: hidden;"&gt;&lt;span class="Apple-style-span"&gt;Finally, if you haven't seen it, watch Brians Beckman's &lt;/span&gt;&lt;a href="http://channel9.msdn.com/ShowPost.aspx?PostID=358968"&gt;&lt;span class="Apple-style-span"&gt;"Don't fear the monads"&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span"&gt; on Channel9.&lt;br /&gt;&lt;br /&gt;Note: as before, this post is written in Literate Haskell, which means that you can save the entire content in this post, paste it into a file and load that file with ghci. Try it!&lt;br /&gt;&lt;br /&gt;Update: You might want to read Don Stewart's post (called "&lt;a href="http://cgi.cse.unsw.edu.au/%7Edons/blog/2008/05/04#strict"&gt;Haskell is a strict language&lt;/a&gt;") on the optimizations that's going on in ghc. Very nice!&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-2788684090490038707?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/2788684090490038707/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=2788684090490038707' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/2788684090490038707'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/2788684090490038707'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/05/lazy-fizzbuzz-in-haskell.html' title='Lazy FizzBuzz in Haskell'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-7874100142026785320</id><published>2008-05-22T14:51:00.000-07:00</published><updated>2008-05-22T15:31:25.311-07:00</updated><title type='text'>FizzBuzz with F#</title><content type='html'>Once again, I'm going to give a solution to the fizz buzz problem, but this time in a, for me, completely new language - &lt;span style="font-weight:bold;"&gt;F#&lt;/span&gt;!&lt;br /&gt;&lt;br /&gt;I've been following the articles on &lt;a href="http://diditwith.net/CategoryView,category,Why%2BI%2BLove%2BF%23.aspx"&gt;Why I Love F#&lt;/a&gt; by Dustin Campell, but even though I was inspired by the language, I never got to download F# and try it... until this very day! I installed it an hour ago, experimented some, and just have to share my experience. As a first mission I tried to write a solution for the fizz buzz problem, and (with some basic knowledge from haskell programming) it didn't take me too long to find a solution. Here it is:&lt;br /&gt;&lt;pre name="code" name="f#"&gt;&lt;br /&gt;let fizzBuzz (x:int) = &lt;br /&gt; match x with&lt;br /&gt; | x when x % 15 = 0 -&gt; "FizzBuzz"&lt;br /&gt; | x when x % 3 = 0 -&gt; "Fizz"&lt;br /&gt; | x when x % 5 = 0 -&gt; "Buzz"&lt;br /&gt; | x   -&gt; x.ToString()&lt;br /&gt;&lt;br /&gt;let main = System.Console.WriteLine(String.concat "\n" (List.map fizzBuzz [1..100]))&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;More or less the same solution as the one given by bugrit in the last post. The &lt;span style="font-style:italic;"&gt;fizzBuzz&lt;/span&gt; function takes an integer and returns the fizzbuzz string representation of that number, using pattern matching. Unfortunate, you have to specify the type of the argument because F# can't infer the type when using x.ToString() (because ToString() is a member function of object, which can be of any type).&lt;br /&gt;&lt;br /&gt;The &lt;span style="font-style:italic;"&gt;main&lt;/span&gt; function uses String.concat to concatenate the strings returned by fizzBuzz. In F#, all basic functions are put in libraries, which as prefixed with their name, such as String and List. They resemble namespaces in C# a lot, and by using the &lt;span style="font-weight:bold;"&gt;open&lt;/span&gt;  keyword you can use all functions in a given library globally, without the prefix.&lt;br /&gt;&lt;br /&gt;There are a lots of more stuff to find out about the language, for example how to integrate it with your current C# project. I'll blog about it if I have time to try it out!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-7874100142026785320?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/7874100142026785320/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=7874100142026785320' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/7874100142026785320'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/7874100142026785320'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/05/fizzbuzz-with-f.html' title='FizzBuzz with F#'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-5175518232367811700</id><published>2008-05-12T08:47:00.000-07:00</published><updated>2008-05-12T09:35:56.425-07:00</updated><title type='text'>What is your favorite language?</title><content type='html'>We all have our favorite language, but for most people it's almost religious to discuss which language is the best. I realized I don't know what language people &lt;span style="font-weight:bold;"&gt;really&lt;/span&gt; prefer, simply because I've never discussed it!&lt;br /&gt;&lt;br /&gt;Having a look at &lt;a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html"&gt;TIOBE Programming Community Index for May 2008&lt;/a&gt; got me thinking. You will see Java in top, followed by C and then (Visual) Basic. I couldn't get this, until I read the following:&lt;br /&gt;&lt;blockquote&gt;Observe that the TIOBE index is not about the best programming language or the language in which most lines of code have been written.&lt;/blockquote&gt;&lt;br /&gt;Of course, Java produces a lots of lines of code, and C has been around for a long long time. Visual Basic I can't say why is rated third, but I guess every newbie programmer develops in VB, and also, you have all those macros! I guess those makes up for a few lines?! :)&lt;br /&gt;&lt;br /&gt;C# is only at 8'th place, but I hope that's because you don't need to write as much code in it as you do with other languages... which probably isn't true because you DO write a lots of code in it. With C# 3.0 you can use synthetic sugar to write less code, but not &lt;span style="font-weight:bold;"&gt;that much&lt;/span&gt; ;) I really think the reason C# is not being that highly ranked is because it hasn't been around for that long. People are still experimenting with it, and those companies that &lt;span style="font-style:italic;"&gt;has&lt;/span&gt; been around for a while can't see the reason for replacing Java with C#. But, in the future, when everyone is &lt;a href="http://www.michaelnygard.com/blog/2008/05/when_should_you_jump_jsr_308_t.html"&gt;jumping off Java&lt;/a&gt;, C# will grow in popularity!&lt;br /&gt;&lt;br /&gt;Anyway, I added a vote to this blog (in the left sidebar), so go ahead and vote on your favorite language. And, if you want, post a comment on this post, arguing for why you think your language is the best!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-5175518232367811700?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/5175518232367811700/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=5175518232367811700' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5175518232367811700'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5175518232367811700'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/05/what-is-your-favorite-language.html' title='What is your favorite language?'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-5407816622422539183</id><published>2008-05-10T06:16:00.000-07:00</published><updated>2008-05-11T12:24:50.290-07:00</updated><title type='text'>C# Lambda Lazy List FizzBuzz :)</title><content type='html'>Ok, so here is my next example of an implementation of the FizzBuzz problem. I'm going to use a combination of C# 3.0's lambda expressions, lazy lists and extension methods!&lt;br /&gt;&lt;br /&gt;First, I create a function returning a lazy list over all numbers starting from 1:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;static IEnumerable&amp;lt;int&amp;gt; AllNumbers()&lt;br /&gt;{&lt;br /&gt;    int i = 1;&lt;br /&gt;    while (true) yield return i++;&lt;br /&gt;}&lt;/pre&gt;Second, I create a function returning a lazy list of strings, being pText every pCount time. I.e. null, null, Fizz, null, null, Fizz.&lt;br /&gt;&lt;pre name="code" class="c#"&gt;static IEnumerable&amp;lt;string&amp;gt; FizzBuzz(int pCount, string pText)&lt;br /&gt;{&lt;br /&gt;    while (true)&lt;br /&gt;    {&lt;br /&gt;        for (int i = 0; i &lt; pCount-1; i++)&lt;br /&gt;        {&lt;br /&gt;            yield return null;&lt;br /&gt;        }&lt;br /&gt;        yield return pText;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Now, I need a way of combining the values two lists, so I create an extension method for this. It takes another list and a combinator function:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;static IEnumerable&amp;lt;T&amp;gt Combine&amp;lt;T, TA, TB&amp;gt;(this IEnumerable&amp;lt;TA&amp;gt pA, IEnumerable&amp;lt;TB&amp;gt pB, Func&amp;lt;TA, TB, T&amp;gt; pCombineFunc)&lt;br /&gt;{&lt;br /&gt;    var i = pA.GetEnumerator();&lt;br /&gt;    var j = pB.GetEnumerator();&lt;br /&gt;    while(i.MoveNext() &amp;&amp; j.MoveNext())&lt;br /&gt;    {&lt;br /&gt;        yield return pCombineFunc(i.Current, j.Current);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;And here is the whole FizzBuzz program, returning a list of strings being 1, 2, Fizz, 4, Buzz...:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;static IEnumerable&amp;lt;string&amp;gt; FizzBuzz()&lt;br /&gt;{&lt;br /&gt;    return AllNumbers().Combine(&lt;br /&gt;        FizzBuzz(3, "Fizz").Combine(FizzBuzz(5, "Buzz"), (x, y) =&gt; x + y),&lt;br /&gt;        (x, y) =&gt; (y != "") ? y : x.ToString());&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;To write the result to the console, I use this main function:&lt;br /&gt;&lt;pre name="code" class="c#"&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;    foreach (string fizzBuzz in FizzBuzz().Take(100))&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine(fizzBuzz);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Thats it! Nice and easy eh? :) Hope you liked it and perhaps makes you look into C# 3.0, if you haven't already? You can always &lt;a href="http://www.microsoft.com/express/"&gt;download Visual Studio 2008 C# Express for free&lt;/a&gt; if you want to try it out!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Edit: Changed generic parameters &amp;lt;T&amp;gt; to html-characters. Changed C# 3.5 to 3.0 (thank u Gustaf :))&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-5407816622422539183?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/5407816622422539183/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=5407816622422539183' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5407816622422539183'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/5407816622422539183'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/05/c-lambda-lazy-list-fizzbuzz.html' title='C# Lambda Lazy List FizzBuzz :)'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-8723493482961338181</id><published>2008-05-08T22:10:00.000-07:00</published><updated>2008-05-08T22:46:47.099-07:00</updated><title type='text'>C# FizzBuzz :)</title><content type='html'>Here is my thoughts and solution to Gustafs &lt;a href="http://coffeedrivendevelopment.blogspot.com/2008/04/ok-so-heres-my-first-real-post-its-spin.html"&gt;Functional FizzBuzz&lt;/a&gt; problem! It's more or less a copy from what I wrote as a &lt;a href="http://gustafnilssonkotte.blogspot.com/2008/04/functional-fizzbuzz.html"&gt;comment in his old blog&lt;/a&gt;, except for some modifications in the code snippet below.&lt;br /&gt;&lt;br /&gt;I believe that in almost all cases, where you should write a solution for a given problem, you have two solutions to choose from. The first one focuses on writing well looking, readable, code (which is always good), the second focuses on writing fast code (which is required in some projects).&lt;br /&gt;&lt;br /&gt;My solution for a bit faster code (I guess), would be this (c# style):&lt;br /&gt;&lt;pre name="code" class="c#"&gt;&lt;br /&gt;for (int i = 1, j = 1, k = 1; i &lt;= 100; i++, j++, k++)&lt;br /&gt;{&lt;br /&gt;    string t = "";&lt;br /&gt;    if (j == 3)&lt;br /&gt;    {&lt;br /&gt;        t = "Fizz";&lt;br /&gt;        j = 0;&lt;br /&gt;    }&lt;br /&gt;    if (k == 5)&lt;br /&gt;    {&lt;br /&gt;        t += "Buzz";&lt;br /&gt;        k = 0;&lt;br /&gt;    }&lt;br /&gt;    if (t == "")&lt;br /&gt;    {&lt;br /&gt;        t = i.ToString();&lt;br /&gt;    }&lt;br /&gt;    Console.WriteLine(t);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The difference is that you don't need to call "x `mod` m == 0" to find out if you should print Fizz or Buzz, instead two extra counters are used to keep track on when to write Fizz and when to write Buzz. I guess you could write this in functional style as well, but my haskell skills are a bit out of date so I wouldn't dare to try that ;)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-8723493482961338181?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/8723493482961338181/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=8723493482961338181' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/8723493482961338181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/8723493482961338181'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/05/c-fizzbuzz.html' title='C# FizzBuzz :)'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-8862651654731710</id><published>2008-05-03T06:17:00.000-07:00</published><updated>2008-05-03T06:18:29.986-07:00</updated><title type='text'>On "Java, Improve or Renew", Java Posse #182</title><content type='html'>&lt;span class="Apple-style-span" style="border-collapse: collapse; color: rgb(51, 51, 51); font-size: 13px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "&gt;So, the "dual post" from me and Christian has not yet been produced (we need to drink coffee together more often!). It's not really so that my fingers are itching - on the contrary; I've been writing on my thesis report like never before. The part I'm writing right now is about Domain Specific Languages, where focus is on writing internal DLS in Java. But right now, I feel like writing something short about something else..&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="border-collapse: collapse; color: rgb(51, 51, 51); font-size: 13px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "&gt;&lt;br /&gt;I listened to on of the Java Posse podcasts yesterday. Its title was "&lt;a href="http://javaposse.com/index.php?post_id=333661" style="font-weight: bold; color: rgb(51, 102, 204); "&gt;Java, Improve or Renew?&lt;/a&gt;". I've actually started to listen to a lot of other podcasts (other than Hanselminutes and .NET Rocks), a complete list with reviews will come in a later post. I like the "Roundup" discussions of Java Posse a lot! There's so much people in the room, it sometimes impossible to identify who's who, but the good thing is that their opinions about the specific topics are so strong!&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="border-collapse: collapse; color: rgb(51, 51, 51); font-size: 13px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "&gt;&lt;br /&gt;So, the "Improve or Renew" podcast was very interesting because to me it seemed like a quick (72 minutes) summary of what challenges Java has right now, to meet the future demands on the language. And not only about the language, but of course also about the bytecode itself.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="border-collapse: collapse; color: rgb(51, 51, 51); font-size: 13px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "&gt;&lt;br /&gt;There's so many factors to put into account for when changing/improving a language! Breaking backward compatibility is very painful when you're the biggest business software language in town, so that should be avoided as long as possible. When listening to the show, it seems like the question of closures in Java 7 really boils down to the implementation of generics in Java. .NET and Java has different implementations of generics and this is the reason that .NET had no problems putting lambdas (anonymous delegates) in - the generics implementation is the whole way down to the IL. It will indeed be very interesting to follow how the Java community deals with this question, but (as someone in the Java Posse panel points out) closures without a proper generics implementation will be a "half-baked" concept that's likely to make people disappointed.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="border-collapse: collapse; color: rgb(51, 51, 51); font-size: 13px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "&gt;&lt;br /&gt;Notice that I'm not trying to say that .NET is better choice than Java in any way! There's a lot more things to a language than small (but important) language details. Certainly no companies (or at least very few) are going to abandon Java in favor of .NET just because of the differences of generics, but it adds one more argument to the discussion of choosing framework (for you who have that choice).&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="border-collapse: collapse; color: rgb(51, 51, 51); font-size: 13px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "&gt;&lt;br /&gt;But honestly, I must admit that &lt;a href="http://blogs.msdn.com/wesdyer/default.aspx" style="font-weight: bold; color: rgb(51, 102, 204); "&gt;Wes Dyer&lt;/a&gt;'s post on monads in C# ("&lt;a href="http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx" style="font-weight: bold; color: rgb(51, 102, 204); "&gt;The Marvel of Monads&lt;/a&gt;") was really inspiring stuff. Monads in Java seems to be a far away, but I guess it's up to the Java community to decide on the "whats, whys and whens" on creating the proper building blocks for enabling them.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-8862651654731710?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/8862651654731710/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=8862651654731710' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/8862651654731710'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/8862651654731710'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/05/on-java-improve-or-renew-java-posse-182.html' title='On &quot;Java, Improve or Renew&quot;, Java Posse #182'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-120765959651930502</id><published>2008-04-30T06:06:00.000-07:00</published><updated>2008-04-30T06:16:10.488-07:00</updated><title type='text'>Functional FizzBuzz</title><content type='html'>&lt;span class="Apple-style-span"  style=" ;font-family:Times;"&gt;&lt;div style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; width: auto; font: normal normal normal 100%/normal Georgia, serif; text-align: left; "&gt;&lt;span class="Apple-style-span"   style="color: rgb(51, 51, 51); font-family:'Trebuchet MS';font-size:13px;"&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;Ok, so here's my first "real" post! It's a spin-off of the (perhaps) famous &lt;a href="http://en.wikipedia.org/wiki/Bizz_buzz"&gt;fizzbuzz/bizzbuzz&lt;/a&gt; test. There seems to be some consensus [&lt;a href="http://www.hanselman.com/blog/YouCantTeachHeightMeasuringProgrammerCompetenceViaFizzBuzz.aspx"&gt;1&lt;/a&gt;,&lt;a href="http://www.codinghorror.com/blog/archives/000781.html"&gt;2&lt;/a&gt;,&lt;a href="http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/"&gt;3&lt;/a&gt;] that this kind of tests identify at least the worst developers during job interviews. When listening to one &lt;a href="http://www.hanselman.com/blog/HanselminutesPodcast53HiringAndInterviewingEngineers.aspx"&gt;episode&lt;/a&gt; of Hanselminutes, I started to wonder how this would work in an interview for a company who would like to hire &lt;span class="Apple-style-span" style="font-style: italic; "&gt;functional&lt;/span&gt; programmers.&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;My plan is to do some more posts on this theme in the future, but at what frequence I don't know yet. And of course I'll put other posts in between the "fizzbuzz" posts as well!&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;Think of this as a version of "&lt;a href="http://www.willamette.edu/~fruehr/haskell/evolution.html"&gt;The evolution of a Haskell programmer&lt;/a&gt;", but perhaps in a more "interviewish" context - that is: Does the style of a "functional fizzbuzz" say something about the person in front of me?&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;So, here's the first version! I call it "Imperative programmer likes Haskell" (but makes the wrong choice). It's written in literate way, so it &lt;span class="Apple-style-span" style="font-style: italic; "&gt;should&lt;/span&gt; work to copy/paste &lt;span class="Apple-style-span" style="font-style: italic; "&gt;the whole post &lt;/span&gt;directly into your text editor, save it (as *.lhs), and then load it into &lt;a href="http://www.haskell.org/ghc/"&gt;ghci&lt;/a&gt; or whatever you choose to run/compile Haskell programs in.&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;First, construct a small helper function for "divides"&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&gt; divides x m = x `mod` m == 0&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;Then do a simple "divide-and-conquer" solution, if we can print &lt;span class="Apple-style-span" style="font-style: italic; "&gt;one&lt;/span&gt; "fizzbuzz", then we have solved the hardest task of this problem. As we see, the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;if-then-else&lt;/span&gt; expression doesn't really fit into the &lt;span class="Apple-style-span" style="font-style: italic; "&gt;do&lt;/span&gt;-notation because of the indentation rules, which is the reason we have to use the block notation. The function looks a bit imperative and it's clearly abusing IO, printing directly to stdout. It would be better to collect the values before printing them, but lets take that in another post or perhaps skip it entirely..&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&gt; printOne x = do&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&gt;  {&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&gt;   if (x `divides` 3 &amp;amp;&amp;amp; x `divides` 5) then print "FizzBuzz" &lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&gt;     else if (x `divides` 3) then print "Fizz" &lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&gt;     else if (x `divides` 5) then print "Buzz" &lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&gt;     else print $ show x&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&gt;  }&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;Now use a nice higher order function (HOF), which certainly no beginner would have found! This function takes a function of type &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;a -&gt; m a&lt;/span&gt;, the list &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;[a]&lt;/span&gt; and performs the given actions on that list, returning no result. The entire type for &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;mapM_&lt;/span&gt; is: &lt;span class="Apple-style-span"  style="color: rgb(0, 0, 0); font-family:-webkit-monospace;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;span class="Apple-style-span"  style="color: rgb(0, 0, 0); font-family:-webkit-monospace;"&gt; Monad m =&gt; (a -&gt; m b) -&gt; [a] -&gt; m ()&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;, where our monad &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;m&lt;/span&gt; in this case is &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;IO a&lt;/span&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&gt; fizzbuzz = mapM_ printOne [1..100]&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;The above function and its relatives are nice examples of "iterating" in Haskell. Of cource, some other languages have HOFs as well, and I think that I most likely will HOFs in other language in some later posts.&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id="smartPaste" contenteditable="true" style="overflow-x: hidden; overflow-y: hidden; "&gt;So, that's all for today folks! Challenge: Try writing some fizzbuzz's yourself in your favourite language and put them as a comment below or post them in your own blog and put a link to it in the comments.&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-120765959651930502?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/120765959651930502/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=120765959651930502' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/120765959651930502'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/120765959651930502'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/04/ok-so-heres-my-first-real-post-its-spin.html' title='Functional FizzBuzz'/><author><name>Gustaf Nilsson Kotte</name><uri>http://www.blogger.com/profile/06265015751619707522</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2340562896607928708.post-7650915592412372277</id><published>2008-04-27T10:25:00.000-07:00</published><updated>2008-04-30T05:42:07.549-07:00</updated><title type='text'>Welcome to Coffee Driven Development!</title><content type='html'>Hello and welcome to this novel blog, created by the developers and bloggers (and previously Chalmers students) &lt;a href="http://christiangenne.blogspot.com"&gt;Christian Genne&lt;/a&gt; and &lt;a href="http://gustafnilssonkotte.blogspot.com"&gt;Gustaf Nilsson&lt;/a&gt;!&lt;br /&gt;&lt;br /&gt;Coffee is indeed a very inspiring drink, and that is the reason for this blog's very existence. Without coffee, what would we do? From meeting over a cup of (yes) coffee, we decided to join together to create this blog. By blogging together we came to the conclusion that we will get more potential readers and more posts. Hopefully that's true :)&lt;br /&gt;&lt;br /&gt;The next post will be a reflection over this meeting, the dialog we had and any potential conclusions we came to. We probably came to more unanswered questions than conclusions, but open questions are always interesting blog topics and maybe addressed in later posts!&lt;br /&gt;&lt;br /&gt;And yes, if you are interesting in joining our blog, just &lt;a href="mailto:christian.genne(at)gmail.com"&gt;drop a mail&lt;/a&gt;!&lt;br /&gt;&lt;br /&gt;/ Team of Coffee Driven Development - Gustaf &amp;amp; Christian&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2340562896607928708-7650915592412372277?l=coffeedrivendevelopment.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://coffeedrivendevelopment.blogspot.com/feeds/7650915592412372277/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2340562896607928708&amp;postID=7650915592412372277' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/7650915592412372277'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2340562896607928708/posts/default/7650915592412372277'/><link rel='alternate' type='text/html' href='http://coffeedrivendevelopment.blogspot.com/2008/04/welcome-to-coffee-driven-development.html' title='Welcome to Coffee Driven Development!'/><author><name>Christian Genne</name><uri>http://www.blogger.com/profile/05183435846100768676</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='18' height='32' src='http://bp2.blogger.com/_F5z3mmdTM8I/R8lEzI2TnAI/AAAAAAAAAAc/Rhe8x9Nk7Uw/S220/070505+183.jpg'/></author><thr:total>0</thr:total></entry></feed>
