markhneedham/blog huggingface dataset:
F#: Inline functions and statically resolved type parameters
Date: 2010-02-10 23:06:14

One thing which I've often wondered when playing around with F# is that when writing the following function the type of the function is inferred to be 'int \-> int \-> int' rather than allowing any values which can be added together:

[source,ocaml]
----

let add x y = x + y
> val add : int -> int -> int
----

It turns out if you use the 'inline' keyword then the compiler does exactly what we want:

[source,ocaml]
----

> let inline add x y = x + y
val inline add :
	^a -> ^b -> ^c
	when ( ^a or  ^b) : (static member ( + ) :  ^a *  ^b ->  ^c)
----

Without the inline modifier type inference forces the function to take a specific type, in this case int. With it the function has a statically resolved type parameter which means that "the type parameter is replaced with an actual type at compile time rather than run time".

In this case it's useful to us because it allows us to implicitly define a member constraint on the two input parameters to 'add'. From the http://msdn.microsoft.com/en-us/library/dd548046(VS.100).aspx[MSDN page]:

____
Statically resolved type parameters are primarily useful in conjunction with member constraints, which are constraints that allow you to specify that a type argument must have a particular member or members in order to be used. There is no way to create this kind of constraint by using a regular generic type parameter.
____

The neat thing about the second definition is that we can add values of any types which support the '+' operator:

[source,ocaml]
----

add "mark" "needham";;
> val it : string = "markneedham"
----

[source,ocaml]
----

> add 1.0 2.0;;
val it : float = 3.0
----

From a quick look at the IL code in Reflector it looks like the 'add' function defined here makes use of the 'http://stuff.mit.edu/afs/athena.mit.edu/software/mono/current/arch/i386_deb40/FSharp-1.9.6.2/lib/FSharp.Core/prim-types.fsi[AdditionDynamic]' function internally to allow it to be this flexible.

One thing which I found quite interesting while reading about http://msdn.microsoft.com/en-us/library/dd548047(VS.100).aspx[inline functions] is that it sounds like it's quite similar to duck typing in that we're saying a function can be passed any value which supports a particular method.

http://www.atrevido.net/blog/2008/08/31/Statically+Typed+Duck+Typing+In+F.aspx[Michael Giagnocavo has a post] where he covers the idea of http://msdn.microsoft.com/en-us/library/dd548046(VS.100).aspx[statically type resolved parameters] in more detail and describes what he refers to as 'statically typed duck typing'.
Shu Ha Ri harmful?
Date: 2010-02-26 23:53:31

I came across a blog post by Rachel Davies where http://agilecoach.typepad.com/agile-coaching/2010/02/shuhari-considered-harmful.html[she wonders whether the Shu-Ha-Ri approach to learning/teaching is actually harmful] and I found Rachel's thoughts around the teaching of principles and practices quite interesting.

____
Quoting Jeff Sutherland:

______
_Only when you have mastered the basic practices are you allowed to improvise. And the last and most important -- Before you have gained discipline, centering, and flexibility, you are a hazard to yourself and others."_
______

I'm uncomfortable with approaches that force students to follow agile practices without questioning. These approaches seem to violate the first value of the Agile Manifesto "Individuals and interactions over processes and tools."
____

What I find interesting about this is that when I first started learning about agile I would read the principles and they would all make sense but I didn't know what to do with that knowledge.

It was only after I'd used the practices frequently and in most cases taking the practices too far that I reached the stage where I could start asking questions that actually had any relevance.

For example Test Driven Development is one practice that I learnt and one of the http://en.wikipedia.org/wiki/Extreme_Programming#Principles[principles] which encourages this approach to coding is the need to get quick feedback on our work.

It would be quite easy to question whether we should test drive everything and you could quite correctly have the opinion that we shouldn't use this approach for absolutely everything.

However, without spending some time following this practice that opinion wouldn't be particularly useful because you would lack the experience that tells you which type of code we should or should not look to test first.

Having decided that TDD was the only approach to writing code I eventually got into situations where it didn't seem to make sense to stick so rigidly to this practice and it was only then that the value of the underlying principle became clear to me.

I don't think I was ever forced not to ask questions but I was just so concentrated on learning how to do the practices properly that I focused on that.

I often find that it's useful to use practices too much until you cause yourself pain.

Martin Fowler has http://martinfowler.com/articles/rubyAtThoughtWorks.html#IsARubyCode-baseHardToUnderstand[a nice graph which shows how people adopt meta programming when coding Ruby] and I think this links quite closely to my  experiences when learning a new practice.

For me at least this seems to be a necessary learning step before I eventually step back and think about the principle that originally led me to following that practice. I can then start using the practice again but in a more effective or intelligent way.

What I've described above is often the way that I learn new things but I'm sure that others' experiences will vary so it'd be interesting to hear about other approaches too!
C#: Overcomplicating with LINQ
Date: 2010-02-21 12:01:22

I recently came across an interesting bit of code which was going through a collection of strings and then only taking the first 'x' number of characters and discarding the rest.

The code looked roughly like this:

[source,csharp]
----

var words = new[] {"hello", "to", "the", "world"};
var newWords = new List<string>();
foreach (string word in words)
{
    if (word.Length > 3)
    {
        newWords.Add(word.Substring(0, 3));
        continue;
    }
    newWords.Add(word);
}
----

For this initial collection of words we would expect 'newWords' to contain ["hel", "to", "the", "wor"]

In a way it's quite annoying that the API for 'Substring' throws an exception if you try and get just the first 3 characters of a string which contains less than 3 characters. If it didn't do that then we would have an easy 'Select' call on the collection.

Instead we have an annoying if statement which stops us from treating the collection as a whole - we do two different things depending on whether or not the string contains more than 3 characters.

In the spirit of the http://www.markhneedham.com/blog/2010/01/20/functional-collectional-parameters-some-thoughts/#comment-30627[transformational mindset] I tried to write some code using functional collection parameters which didn't make use of an if statement.

Following this idea we pretty much have to split the collection into two resulting in this initial attempt:

[source,csharp]
----

var newWords = words
    .Where(w => w.Length > 3)
    .Select(w => w.Substring(0, 3))
    .Union(words.Where(w => w.Length <= 3).Select(w => w));
----

This resulted in a collection containing ["hel", "wor", "to", "the"] which is now in a different order to the original!

To keep the original order I figured that we needed to keep track of the original index position of the words, resulting in this massively overcomplicated version:

[source,csharp]
----

var wordsWithIndex = words.Select((w, index) => new { w, index });

var newWords = wordsWithIndex
               .Where(a => a.w.Length >= 3)
               .Select((a, index) => new {w = a.w.Substring(0, 3), a.index})
               .Union(wordsWithIndex.Where(a => a.w.Length < 3).Select(a => new { a.w, a.index }))
               .OrderBy(a => a.index);
----

We end up with a collection of anonymous types from which we can get the transformed words but it's a far worse solution than any of the others because it takes way longer to understand what's going on.

I couldn't see a good way to make use of functional collection parameters to solve this problem but luckily at this stage http://enginechris.wordpress.com/[Chris Owen] came over and pointed out that we could just do this:

[source,csharp]
----

var newWords = words.Select(w => w.Length > 3 ? w.Substring(0, 3) : w);
----

I'd been trying to avoid doing what is effectively an if statement inside a 'Select' but I think in this case it makes a lot of sense and results in a simple and easy to read solution.
F#: function keyword
Date: 2010-02-07 02:54:13

I've been browsing through Chris Smith's http://www.amazon.com/gp/product/0596153643?ie=UTF8&tag=marneesblo-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596153643[Programming F#] book and in the chapter on pattern matching he describes the 'function' key word which I haven't used before.

It's used in pattern matching expressions when we want to match against one of the parameters passed into the function which contains the pattern match.

For example if we have this somewhat contrived example:

[source,ocaml]
----

let isEven value = match value with
                    | x when (x % 2) = 0 -> true
                    | _ -> false
----

That could be rewritten using the function keyword to the following:

[source,ocaml]
----

let isEven  = function
               | x when (x % 2) = 0 -> true
               | _ -> false
----

It's a relatively straight forward way to simplify code like this although one thing I noticed while looking back through some old code I've written is that if we use this syntax then we need to ensure that the parameter we want to pattern match against is passed as the last parameter to a function.

For example this function which is used to parse the arguments passed to  a script was originally written like this:

[source,ocaml]
----

let GetArgs initialArgs  =
    let rec find args matches =
        match args with
        | hd::_ when hd = "--" -> List.to_array (matches)
        | hd::tl -> find tl (hd::matches)
        | [] -> Array.empty
    find (List.rev (Array.to_list initialArgs) ) []
----

If we want to use 'function' then we'd need to put 'args' implicitly as the second argument passed to the recursive 'find' function:

[source,ocaml]
----

let GetArgs initialArgs  =
    let rec find matches =
        function
        | hd::_ when hd = "--" -> List.to_array (matches)
        | hd::tl -> find (hd::matches) tl
        | [] -> Array.empty
    find [] (List.rev (Array.to_list initialArgs) )
----

I'm not sure that the resulting code is necessarily more intention revealing if the function has more than one argument passed to it. The second version of this function could be very confusing if you didn't know what the 'function' keyword actually did.
Functional C#: Extracting a higher order function with generics
Date: 2010-02-08 23:17:47

While working on some code with http://www.the-arm.com/[Toni] we realised that we'd managed to create two functions that were almost exactly the same except they made different service calls and returned collections of a different type.

The similar functions were like this:

[source,csharp]
----

private IEnumerable<Foo> GetFoos(Guid id)
{
    IEnumerable<Foo> foos = new List<Foo>();
    try
    {
        foos = fooService.GetFoosFor(id);
    }
    catch (Exception e)
    {
        // do some logging of the exception
    }
    return foos;
}
----

[source,csharp]
----

private IEnumerable<Bar> GetBars(Guid id)
{
    IEnumerable<Bar> bars = new List<Bar>();
    try
    {
        bars = barService.GetBarsFor(id);
    }
    catch (Exception e)
    {
        // do some logging of the exception
    }
    return bars;
}
----

We're defining the empty lists so that if the service throws an exception we can make use of an empty list further on in the code. A failure of the service in this context doesn't mean that the application should stop functioning.

My thinking here was that we should be able to pull out the service calls into a function but the annoying thing is that they return different types of collections so I initially thought that we'd be unable to remove the duplication.

Thinking about the problem later on I realised we could just define the return value of the service call in the function to use generics.

We therefore end up with this solution:

[source,csharp]
----

private IEnumerable<Bar> GetBars(Guid id)
{
	return GetValues(() => barService.GetBarsFor(id));
}
----

[source,csharp]
----

private IEnumerable<Foo> GetFoos(Guid id)
{
	return GetValues(() => fooService.GetFoosFor(id));
}
----

[source,csharp]
----

private IEnumerable<T> GetValues<T>(Func<IEnumerable<T>> getValues)
{
    IEnumerable<T> values = new List<T>();
    try
    {
        values = getValues();
    }
    catch (Exception e)
    {
        // do some logging of the exception
    }
    return values;
}
----

I think the code is still quite readable and it's relatively obvious what it's supposed to be doing.
Functional C#: Writing a 'partition' function
Date: 2010-02-01 23:34:02

One of the more interesting higher order functions that I've come across while playing with F# is the partition function which is similar to the filter function except it returns the values which meet the predicate passed in as well as the ones which don't.

I came across an interesting problem recently where we needed to do exactly this and had ended up taking a more imperative for each style approach to solve the problem because this function doesn't exist in C# as far as I know.

In F# the function makes use of a tuple to do this so if we want to create the function in C# then we need to define a tuple object first.

[source,csharp]
----

public class Tuple<TFirst, TSecond>
{
	private readonly TFirst first;
	private readonly TSecond second;

	public Tuple(TFirst first, TSecond second)
	{
		this.first = first;
		this.second = second;
	}

	public TFirst First
	{
		get { return first; }
	}

	public TSecond Second
	{
		get { return second; }
	}
}
----

[source,csharp]
----

public static class IEnumerableExtensions
{
	public static Tuple<IEnumerable<T>, IEnumerable<T>> Partition<T>(this IEnumerable<T> enumerableOf, Func<T, bool> predicate)
	{
		var positives = enumerableOf.Where(predicate);
		var negatives = enumerableOf.Where(e => !predicate(e));
		return new Tuple<IEnumerable<T>, IEnumerable<T>>(positives, negatives);

	}
}
----

I'm not sure of the best way to write this function - at the moment we end up creating two iterators to cover the two different filters that we're running over the collection which seems a bit strange.

In F# 'partition' is on List so the whole collection would be evaluated whereas in this case we're still only evaluating each item as it's needed so maybe there isn't a way to do it without using two iterators.

If we wanted to use this function to get the evens and odds from a collection we could write the following code:

[source,csharp]
----

var evensAndOdds = Enumerable.Range(1, 10).Partition(x => x % 2 == 0);

var evens = evensAndOdds.First;
var odds = evensAndOdds.Second;
----

The other thing that's nice about F# is that we can assign the result of the expression to two separate values in one go and I don't know of a way to do that in C#.

[source,ocaml]
----

let evens, odds = [1..10] |> List.partition (fun x -> x % 2 = 0)
----

We don't need to have the intermediate variable 'evensAndOdds' which doesn't really add much to the code.

I'd be interested in knowing if there's a better way to do this than what I'm trying out.
Coding: Effect sketches and the Mikado method
Date: 2010-02-23 00:29:34

I've http://www.markhneedham.com/blog/2009/11/04/reading-code-unity/[written previously about how useful I find effect sketches] for helping me to understand how an object's methods and fields fit together and while drawing one a couple of weeks ago I noticed that it's actually quite useful for seeing which parts of the code will be the easiest to change.

I was fairly sure one of the object's in our code base was doing too many things due to the fact that it had a lot of dependencies.

However, it wasn't obvious to me from looking at the code which would be the easiest place to start in pulling out some of those responsibilities.

I therefore drew out an effect sketch which looked something like this:

image::{{<siteurl>}}/uploads/2010/02/blog.png[blog.png,700]

From the diagram I could see more clearly that 'MethodC' is using 3 fields which are not used by any of the other methods in the object.

This therefore seemed like the perfect method to pull out since I could do so really easily and get rid of 3 of the object's fields since noone else used them anyway.

This reminded me a lot of the http://danielbrolund.wordpress.com/2009/03/28/start-paying-your-technical-debt-the-mikado-method/[Mikado method] for addressing technical debt which I've read about but haven't used yet.

As I understand it, the goal with the Mikado method is to locate areas of the code base that we can change easily because there are no dependencies on this piece of code.

When using effect sketches the goal is to try and use the sketch to work out how we can group functionality and I think the idea of making initial changes that have a low impact is a good one to follow.

I drew my initial effect sketch on paper but I noticed that drawing it up in http://www.graphviz.org/Documentation.php[graphviz] actually makes it even more obvious which bits of functionality are related.

For example I hadn't realised that 'fieldB' was used by so many methods until I typed this up.

It's quite a neat tool and easy to pick up. This is my 'dot' file for the above sketch:

blog.dot

[source,text]
----

digraph effectgraph {
	size="8,8";
	
	"MethodA" -> "fieldA";
	"MethodA" -> "fieldB";
	"MethodA" -> "fieldC";
	
	"MethodB" -> "fieldB";
	"MethodB" -> "fieldE";
	"MethodB" -> "fieldF";
	
	"MethodC" -> "fieldG";
	"MethodC" -> "fieldH"
	"MethodC" -> "fieldD"
	
	"MethodD" -> "fieldB"
	"MethodD" -> "fieldI"
}
----

And to generate a png I ran the following command from the terminal:

[source,text]
----

dot -Tpng -blog.png blog.dot
----
F#: Passing an argument to a member constraint
Date: 2010-02-15 00:05:17

I've written previously about http://www.markhneedham.com/blog/2009/04/28/f-overloading-functionspattern-matching/[function overloading in F# and my struggles working out how to do it] and last week I came across the concept of http://www.markhneedham.com/blog/2010/02/10/f-inline-functions-and-statically-resolved-type-parameters/[inline functions and statically resolved parameters] as a potential way to solve that problem.

I came across a problem where I thought I would be able to make use of this while playing around with some code parsing Xml today.

I had a 'descendants' function which I wanted to be applicable against 'XDocument' and 'XElement' so I originally just defined the functions separately forgetting that the compiler wouldn't allow me to do so as we would have a duplicate definition of the function:

[source,ocaml]
----

let descendants name (xDocument:XDocument) = xDocument.Descendants name
let descendants name (xElement:XElement) = xElement.Descendants name
----

I wanted to make use of the inline function to define a function which would allow any type which supported the 'Descendants' member:

[source,ocaml]
----

let inline descendants name (xml:^x) =
    (^x : (member Descendants : XName -> seq<XElement>) (xml))
----

I couldn't work out how I could pass the 'name' input parameter to 'Descendants' so I was getting the following error:

[source,text]
----

expected 2 expressions, got 1
----

I http://stackoverflow.com/questions/2260939/f-overloading-functions[posted the problem to StackOverflow] and 'Brian' pointed out the syntax that would allow me to do what I wanted:

[source,ocaml]
----

let inline descendants name (xml:^x) =
	(^x : (member Descendants : XName -> seq<XElement>) (xml,name))
----

Tomas Petricek pointed out that in this case we could just write a function which took in 'XContainer' since both the other two types derive from that anyway:

[source,ocaml]
----

let descendants name (xml:XContainer) = xml.Descendants name
----

In this situation that certainly makes more sense but it's good to know how to write the version using member constraints for any future problems I come across.
F#: Unexpected identifier in implementation file
Date: 2010-02-14 01:03:34

I've been playing around with some F# code this evening and one of the bits of code needs to make a HTTP call and return the result.

I wrote this code and then tried to make use of the 'Async.RunSynchronously' function to execute the call.

The code I had looked roughly like this:

[source,ocaml]
----

namespace Twitter

module RetrieveLinks
    open System.Net
    open System.IO
    open System.Web
    open Microsoft.FSharp.Control

    let AsyncHttp (url:string) = async {
        let request =  HttpWebRequest.Create(url)
        let! response = request.AsyncGetResponse()
        let stream = response.GetResponseStream()
        use reader = new StreamReader(stream  )
        return! reader.AsyncReadToEnd() }

    let getData =
        let request = "http://some.url"
        AsyncHttp <| request

	Async.RunSynchronously getData
----

The problem was I was getting the following error on the last line:

[source,text]
----

Error	3	Unexpected identifier in implementation file
----

I've seen that error before and it often means that you haven't imported a reference correctly and hence the compiler doesn't know what you're trying to refer to.

In this case I was fairly sure all my references were correct and I was still getting the same error when I used the full namespace to 'Async.RunSynchronously' which seemed to suggest I'd done something else wrong.

After comparing this file with another one which was quite similar but didn't throw this error I realised that I'd left of the '=' after the module definition. Putting that in solved the problem.

[source,ocaml]
----

namespace Twitter

module RetrieveLinks =
	// and so on
----

As I understand it if we don't use the '=' then we've created a top level module declaration and if we do use the '=' then we've created a local module declaration.

http://msdn.microsoft.com/en-us/library/dd233221(VS.100).aspx[From MSDN]:

____
You do not have to indent declarations in a top-level module. You do have to indent all declarations in local modules. In a local module declaration, only the declarations that are indented under that module declaration are part of the module.
____

Given this understanding another way to solve my problem would be to remove the indentation of the functions inside the module like so:

[source,ocaml]
----

module RetrieveLinks
open System.Net
open System.IO
open System.Web
open Microsoft.FSharp.Control

// and so on until...

Async.RunSynchronously getData
----

That compiles as expected.

From reading the MSDN page it would suggest that in my first example I'd created a top level module declaration but indenting the code inside that module somehow meant that the 'Async.RunSynchronously' function wasn't recognised.

I don't quite understand why that is so if anyone can enlighten me that would be cool!