Oct 27, 2009

F# – sharp what?


I’m really happy with C# as an expressive, powerful and readable language (after recently being somewhat unfaithful with VB.NET). For a customer project doing heavy mathematics I had a look on the suitability of F# for concurrent table- and matrix-oriented math.

(logo from whattofix.com; not found one from Microsoft!?)

Overview

On the Microsoft F# Developer Center I got a first overview (e.g. F# will be part of Visual Studio 2010).

F# is a functional, compiled but object-oriented language. An interactive console allows for initial coding:

image

There is no autocompletion, syntax highlighting or intellisense. Errors are quite understandable however.

Or you can create a new Visual Studio project:

image

There is useful little video on channel9 that gives a first idea of the language. Since Cobol/Lisp I have not felt so “strange” with a new language …

Downloads

Installation

The installation for VS2008 took some minutes:

image

Learning

In the samples there is a solution called Samples101 that you can build and run:

image

Concurrency

On the development center site there a book chapter about asynchronous and concurrent programming (Apress). Unfortunately this book is based on an older version. I found some slides from Matthew Podwysocki.

The technique I am interested in is called “asynchronous workflows” (a powerful set of techniques for structuring asynchronous programs in a natural way).

// mleder.blogspot.com : my.net

// my first f# concurrent program

 

#light

 

// import or open namespaces

open System.IO

open Microsoft.FSharp.Control.CommonExtensions

 

// define array of key/value pairs

let people = ["Markus", "F# is cool";

               "Jonas", "eff what?";

               "Aaron", "(doing something different)";

               "Elias", "bla bla bla"]

 

// define a function to be executed asynchonously                      

let myAsync(name:string, comment:string) =

    async {

        System.Console.WriteLine("Waiting {0} ... {1} / {2}", System.Threading.Thread.CurrentThread.ManagedThreadId, name, comment)

        System.Threading.Thread.Sleep 1000       

        System.Console.WriteLine("Waited! {0}", name)

    }

 

// change the number of threads in the pool

// let ok = System.Threading.ThreadPool.SetMinThreads(10,10)

 

// start function on thread pool threads

for nm,url in people do

    Async.Start(myAsync(nm,url))

 

// wait for termination (using the "pass-forward" or "pipe-forward" operator to chain calls)

System.Console.ReadKey() |> ignore

Which seems to run on 2 pool threads (instead of 2 per CPU which would mean 4) !?:

image

Uncommenting the SetMinThread line above gives a different behavior

image

F# Summary

  • a language you have to get used to
  • steep learning curve (for a C# guy like me)
  • not widely used yet. Check maintainability and people skills.
  • suited for certain problem domains (like mathematics, analytics, simulation, …); to be preferred to other CLR languages
  • building on existing .NET framework assets
  • well integrated into Visual Studio (even if my often used “go to definition”/F12 does not work)
  • concurrent and asynchronous operation is an integrated part of the language

Oct 14, 2009

ASP.NET MVC 2 Resources

I’ve been asked about MVC tutorials, blogs, how to learn. Here are some useful links to get started for all of you:

ASP.NET MVC

Have fun!