Another plug for F#…

The majority of .NET people I work with are C# developers. There are some VB guys I know and even a few Java people.

I also know a few F# folks from my time when I went on a SkillsMatter course last year and although I don’t use the language on a day-to-day basis, I still dip into it now and then, particularly if I find myself trying to write a component which acts in a functional sense e.g. takes in some data and returns a result.

I’ve been playing a little with F#3.0 lately and have been really, really impressed with the data access and query elements that they’ve added to the language. The two main things that I’ve noticed are Query Expressions and Type Providers .

Query Expressions

Query Expressions bring LINQ-type query syntax directly in F#. To be fair, F# already had the extension method style syntax for pipelining data (which I happen to prefer) with the |> operator, but you can now write LINQ queries directly e.g. given a type customer you could previously write a query as follows:

let results = customers
                  |> Seq.filter (fun customer -> customer.Age < 21)
                  |> (fun customer -> customer.Surname)
                  |> Seq.distinct

But in F#3.0 you can write it like so: –

let results  = query { for customer in customers do
                       where (customer.Age < 21)
                       select customer.Surname
                       distinct }

Personally I don’t mind the pipeline style – in some ways I’m more used to that.

The real difference between F# and C#’s query operators is the sheer volume of them that are directly supported in F# compared to C#. In addition to the normal where, select and distinct etc., there are are also ones such as all, average, first, skip and take etc. etc. all directly in the language itself – so you essentially get a much richer set of query operators directly supported in the F# language as opposed to just in the framework as a set of method calls.

Type Providers

Type Providers are a great way of consuming data from distant sources without having to manually create (or code-gen) types to handle the data coming in. Think about how you handle data from a web service, database or CSV file – typically at best you’ll use a code gen to create your proxy types, or at worst you create some POCOs by hand and manually do the mapping.

Type Providers give us a way to, at code-time (not compile time!), point e.g. a web service to a URI and automatically give us a strongly-typed dataset e.g. in the example below, I’m connecting to the Netflix OData endpoint: –

type odata = ODataService<"">

I can now get my context from this type and go right ahead and start interrogating the model. There’s no need for an “add service reference” etc. which code-gens – this is all happening at edit-time, immediately after I finished typing the line above.


And Type Providers are extensible so you can create your own ones as you see fit; there are now type providers for Entity Framework models, L2S, CSV etc. etc – nice.


F#3.0 brings some very nice data access points directly into the language. Again, to me F# is growing very quickly, and very nicely, to the point where it starts to make C# look a little like it’s standing still. Of course, the two languages target very different domains – C# is a general purpose OO language (which in recent years has brought in some functional constructs), and it’s always going to target a larger audience than F#. As a result, it has different aims and goals as a language than F#.

Nonetheless, there are times where I wish C# had the type inference from F#, or it’s pattern matching capabilities, or its support for helping you write functionally pure code. If you’re at all interested in broadening your views on programming, I would strongly recommend you spend a bit of time looking at F#, if for no other reason than you might improve the way you write your C#. It’ll also give you a better understanding of what a compiler does – you’ll start to realise just what the C# compiler does (or doesn’t do Smile) when you hit build.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s