Friday, September 16, 2011

F# Type Providers - Querying StackOverflow

If you haven't yet heard, the F# 3.0 preview bits were released to the wild a couple of days ago. Congrats to the F# team for this huge accomplishment!

One of the most anticipated features of F# 3.0 is Type Providers. Type Providers make F# the best available option for working in an information-rich world. Even though the preview has only been available for a few days, there is already quite a bit of content covering Type Providers. Here are a few examples:

http://msdn.microsoft.com/en-us/library/hh156509(v=VS.110).aspx
http://blogs.msdn.com/b/fsharpteam/archive/2011/09/14/f-3-0-developer-preview-now-available.aspx
http://fsharp3sample.codeplex.com/

Here's a simple example of querying the StackOverflow OData feed using a Type Provider:

#r "FSharp.Data.TypeProviders"
#r "System.Data.Services.Client"
open Microsoft.FSharp.Data.TypeProviders
[<Generate>] // Only needed for the Developer Preview (this will be dropped before the official release of F# 3.0)
type stackoverflow =
ODataService<ServiceUri = @"http://data.stackexchange.com/stackoverflow/atom">
let dataContext = stackoverflow.GetDataContext()
query {
for post in dataContext.Posts do
where (post.Tags.Contains "fsharp")
count
} |> printfn "There are %i posts marked with the fsharp tag on SO"

How Do I Get It?

To start playing with F# 3.0, install the VS2011 dev preview and follow any of the step-by-step instructions provided at http://msdn.microsoft.com/en-us/library/hh156509(v=VS.110).aspx.

No comments:

Post a Comment