January 21, 2021
Hot Topics:

Getting Functional with F#

  • By Jani Järvinen
  • Send Email »
  • More Articles »

A (Parallel) Database Example

The previous listing gave you a hint that the whole .NET Framework versions 2.0 and later are accessible from F#. Now, assume that you would like to build a simple database application to read data from an SQL Server database. As you are surely aware, using ADO.NET is usually the best solution to do this, and so you would use the connection, command, and reader (for instance) classes from the System.Data.SqlClient namespace.

Here is an example of how to read data from the old, yet familiar Northwind database:

let connStr = "Data Source=.;Initial Catalog=Northwind;\
               Integrated Security=True;"
let conn = new System.Data.SqlClient.SqlConnection(connStr)
let sql = "SELECT [employeeid], [firstname], [lastname]\
           FROM [employees]"
let cmd = new System.Data.SqlClient.SqlCommand(sql, conn)
let reader = cmd.ExecuteReader()
printfn "Name = %s %s" (reader.GetString(1)) (reader.GetString(2))

Again, F# provides a quite straightforward way of accessing data with ADO.NET. As you can see, it is easy to convert C# code to F#, because the classes and methods calls you need to make stay the same.

Of course, things get more interesting for F# if you add some calculations into the mix. For instance, you might need to calculate the price of items with sales tax included. To retrieve the data from the database, you could use, for example, a LINQ query to get two arrays of values: one for the prices and another for the tax percentages for the given prices:

let prices = [100.0; 200.0; 50.0]
let taxPercents = [5.0; 4.0; 7.0]

With these two arrays, it would be simple to define a function called "calculateTax" that would calculate the correct price, and then run a List.map2 ("map2" is the method version that takes two parameters instead of just one) on these two lists:

let calculateTax price tax = price * (1.0 + (tax/100.0))
let results = List.map2 calculateTax prices taxPercents
printfn "results = %A" results

This would return a new array, "results," with values [105.0; 208.0; 53.5]. Of course, all this works nicely, but wouldn't it be great if you could somehow make this calculation use multiple threads to take better use of modern multi-core processors? With F#, this is almost as easy as 123!

F# supports built-in parallelism, and the "async" keyword. If you wanted to calculate the taxes in parallel, you would embed the List.map2 method call into an asynchronous context:

let results =
   Async.Run (Async.Parallel [
   async { return List.map2 calculateTax prices taxPercents } ])

That is all you need! Of course, with only three elements in the array, it would actually take longer to calculate the taxes in parallel because of threading overhead, but if you would have three hundred thousand items instead, the differences would start to show.

Installing F# into Visual Studio

If you attended Microsoft's PDC 2008 (Professional Developer Conference) in Los Angeles in late October and early November, or viewed the many F# sessions online, you might have noticed that Microsoft plans to make F# a fully supported Visual Studio language. This means that all users who use Visual Studio (presumably the next 2010 version), would get support for F#, just as they get support for C# and Visual Basic .NET today.

However, at this writing, F# is currently in the Community Technology Preview (CTP) stage, the latest version being from September, 2008 (see Figure 1). The CTP requires Visual Studio 2008 to operate, but as with other .NET languages, you also have the option to use the command-line compiler, fsc.exe. The current download package for F# is about 14 megabytes in size.

Figure 1: The September CTP of F# requires Visual Studio 2008.

Page 2 of 4

This article was originally published on November 14, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date