Now that Visual Studio 2010 Beta 1 is out, it is finally a good time to take a look at one of the (in my opinion) most interesting new features in the new release - the F# language. As all the regular hubFS readers know, F# existed for quite a long time now as Microsoft Research project, but is now becoming a real Microsoft product. Interestingly, F# is still available as a plugin for Visual Studio 2008, so if you want to try it you don't have to install the whole new beta of 2010.
There are already many resources for learning F# including my functional programming overview, which is a Manning Greenpaper for the book Functional Programming for the Real World that I'm writing with Jon Skeet and my four-part F# introduction. There are also some useful links on the official F# web site including some talk recordings. However, I haven't yet seen any good F# webcast focusing mainly on showing F# source code, starting from simple functional concepts to the real-world features like asynchronous workflows and object-oriented programming in F#, so I decided to create one.
(The whole webcast series is available in my other blog and I'm cross-posting the links and brief descriptions of each of the parts here)
The F# development process
The webcast starts by experimenting with F# and by introducing some of the essential functional concepts and then follows the usual F# development process. It starts by creataing an empty F# script (New File... - F# Script File in Visual Studio), which is the easiest way to start coding in F#. We'll just create an empty script, start writing the code and run it interactively to see how it works. This way, we'll develop a very simple F# snippet for downloading content of a RSS feed and for searching for feeds that contain the specified keyword.
Once we have a simple solution to the problem, we can start improving it and refactoring it in various ways. I believe that refactoring is a very important part of the usual F# development process. In particular, we'll see how to wrap the code into asynchronous workflows to make it run asynchronously (rather than using blocking calls) and also potentially in parallel (rather than downloading and processing all the feeds in sequence).
Finally, in the last part of the series, you'll see one more refactoring. We'll wrap the existing code into a simple object oriented library and we'll also finally turn it from a simple F# script file into a standard .NET DLL library that can be used from C#. As you'll see in the webcast, this is again a relatively simple refactoring that includes only a couple of minor changes in the source code. Once we'll compile the library, we'll also look how to use it from C# and in particular, from a simple ASP.NET web application.
Introducing F# webcast
The links to the individual parts of the webcast are available here. Each of the articles also contains link for downloading the video (in case you wanted to view it offline) and also full source code that I wrote during the presentation:
- Part I. - Introducing functional concepts
The first part introduces functional programming principles such as immutability, recursion and functions that take other functions as parameter (higher order functions). This can all be demonstrated in C# 3.0, so we start with C# and then look how the same concepts look in F#. Finally, the first part also shows functions for working with lists in F#.
- Part II. - Using standard .NET libraries
The second part demonstrates how we can use standard .NET libraries. It uses classes from System.Net and System.Xml to download content of a web page (RSS feed), load it into XML document and process it to find only posts that contain some specified keyword.
- Part III. - Downloading web pages asynchronously
The third part shows how to make the code from the part II. better. It introduces F# asynchronous workflows that can be used for writing code that doesn't block a thread when waiting for the completion of some I/O request. This part also shows how to modify the code to download and process multiple feeds in parallel.
- Part IV. - Developing standard .NET libraries
In the fourth part, we look how to encapsulate the functionality written in F# into classes. We'll create a project and we'll wrap the code we wrote into a .NET class. We'll also look how to compile the project into DLL and how to use it from a simple C# web application.