by Mathias
16. May 2010 08:14
I have been working with trees quite a bit lately, because I am coding something which involves probability trees: based on the state of the system, there is a number of things which can happen, each with a certain probability.
I ended up writing a simple generic Node class, which can contain anything, and can have multiple children, along these lines:
public class Node<T>
{
public Node()
{
this.Children = new List<Node<T>>();
}
public T Content
{
get;
set;
}
public List<Node<T>> Children
{
get;
private set;
}
public bool IsLeaf
{
get
{
return (this.Children.Count() == 0);
}
}
}
Pretty quickly, I realized I would need to get the list of all nodes under a certain node, as well as the list of its leaves (a leaf being a node that has no children, i.e. an endpoint of the tree). This is a job tailor-made for recursion: if a node is a leaf, return it, otherwise, search further in all his children.
More...
36c22eef-a36d-4c49-b6c8-43d4637abf79|0|.0
by Mathias
9. April 2010 12:04
Learning new things is difficult, but the hardest thing to do is to learn new ways to do things you have always done a certain way. Case in point: the Strategy Pattern. It is the first design pattern I was introduced to, and back then, it was an eureka moment for me.
I learnt it the “classic” way: when a class is performing an operation, but could go about it a few different ways, rather than building a switch statement in the class to handle each particular case, extract an interface for the operation, and create interchangeable concrete implementations, which can be plugged in the “Context” class at run time.

Source: http://www.dofactory.com/Patterns/Patterns.aspx
For some obscure reason, I went to the Wikipedia page dedicated to the Strategy pattern recently, and was very surprised to see that the first C# example proposed didn’t use polymorphism at all. The second example is the old-school interface/concrete implementation version, but the first illustration uses the Func<> delegate. Here is a quick example I wrote using that approach. Rather than an interface, the Strategy can be any function that takes in a string as first argument, and returns a string as a result.
public class Context
{
public Func<string, string> Strategy
{
get;
set;
}
public string SayHello(string name)
{
return this.Strategy(name);
}
}
More...
3f6f2cb9-9f99-4da5-ba0d-8c140a71860f|0|.0
by Mathias
23. January 2010 00:21
One of the immediate benefits I saw in digging into F# is that it gave me a much better understanding of LINQ and lambdas in C#. Until recently, my usage of LINQ was largely limited to returning IEnumerable instead of List<T> and writing simpler queries, but I have avoided the more “esoteric” features. I realize that now that F# is becoming familiar to my brain, whenever I see a statement in C# which contains a foreach:
foreach (var item in items)
{
// do something with item.
}
… I ask myself if this could be re-written in a more functional way. Sometimes it works, sometimes not. Just like classic OO Design Patterns, functional programming has its own patterns, and I find that having a larger toolkit of patterns in the back of my mind helps criticizing my own code and think about alternatives and possible improvements.
I encountered one such case a few days ago, with the following snippet:
public bool IsValid()
{
foreach (var rule in this.rules)
{
if (!rule.IsSatisfied())
{
return false;
}
}
return true;
}
There is nothing really wrong with this code. However, seeing the foreach statement, and an if statement with a return and no else branch made me wonder how I would have done this in F# – and my immediate thought was “I’d use a Fold”.
More...
dee8f3b2-3671-46b1-907b-bd1542725321|1|1.0
by Mathias
1. December 2009 11:33
I have been using test-driven development since I read Kent Beck’s book on the topic. I loved the book, I tried it, and adopted it, simply because it makes my life easier. It helps me think through what I am trying to achieve, and I like the safety net of having a test suite which tells me exactly what I have broken. It also fits well with the type of code I write, which is usually math-oriented, with nice, tight domain objects.
So when I decided recently to write a C# implementation of the Simplex algorithm, a classic method to resolve linear programming optimization problems, I expected a walk in the park.
(Side note:I am aware that re-implementing the Simplex is pointless, I am doing this as an exercise/experiment)
Turns out, I was mistaken. I have been struggling with this project pretty much from the beginning, and unit testing hasn’t really helped so far. Unfortunately, I didn’t reach a point where I fully understand what it is that is not flowing, but I decided I would share some of the problems I encountered. Maybe brighter minds than me can help me see what I am doing wrong!More...
1d6a56a4-3f67-4303-ac1a-5af4876e313f|0|.0
by Mathias
15. October 2009 18:03
I recently finished reading the Art of Unit Testing, by Roy Osherove (Manning); here are a few thoughts on the book.
Who to give it too
This is an excellent book for the C# developer with solid foundations in object-oriented design, who has already some exposure to writing unit tests. If you have worked on a decent-scale project, and found yourself thinking “hmmm, I am sure there is a smarter way to write these tests”, you should definitely get that book. Note that while it will be extremely useful to the test-driven development practitioner, this is NOT a book on TDD.More...
c7c01ab4-0462-41f3-8542-5dab693b6341|0|.0