Phillip Trelford's Array

POKE 36879,255

BDD with ``TickSpec`` talk

The podcast for “BDD with TickSpec and Concurrency with Agents” from the F#unctional Londoners Meetup is now available, many thanks to Skills Matter:

Podcast content:

  • a quick look at F# in MonoDevelop
  • BDD with TickSpec with examples in Visual Studio using NUnit, MbUnit & Silverlight
  • Tomas Petricek on Concurrency with Agents including Chat and Twitter samples

This talk coincided with the November 2010 stable release of TickSpec, which supports both .Net 2.0+ and Silverlight 4. Plus examples for all major .Net testing frameworks.

All TickSpec examples presented are on CodePlex:

Thanks to everyone who made it a really fun evening!


Picture taken at the Slaughtered Lamb pub courtesy of Ankur Gurha

Metadata: data about data

Compilers for statically typed languages like C# and F# maintain data about types from programs. This type data is used at design time to provide IntelliSense and during compilation to generate new types and bind to existing types. We can think about this type data in the abstract as data about data, or Metadata. In statically typed languages the typed Metadata is typically imported and generated from typed programs.


What if a compiler could be augmented with metadata from other sources, say a database or an XML DTD or a DSL?


Then we could map to data easily inside our programs without resorting to either late binding or a code generation step!!!

Examples of accessing attributes of an XML element

1: Late-binding with string literals (F#)

let person = XElement.Parse("<Person Name=\"Bob\"/>")
let name = person.Attribute(XName.Get "Name").Value


2: Late-binding with dynamic lookup (F#)

// Define dynamic lookup operator
let (?) (el:XElement) (name:string) = el.Attribute(XName.Get name).Value
// Lookup Name attribute
let name = person?Name


3: Code generation using XSD.exe (C#)

string xml = "<Person Name=\"Bob\"/>";
var s = new XmlSerializer(typeof(Person));
var person = s.Deserialize(new StringReader(xml)) as Person;
var name = person.Name;


4: Hypothetical code binding (F#)

let person = XmlProvider.LoadXml("<Person Name=\"Bob\"/>")
let name = person.Name


The first 1st and 2nd methods using late-binding in effect use string-literals so lack type safety. The 3rd method required a code generation step that generated a whopping 62 lines of C# code to map 1 XML element with a single attribute.

The 4th (hypothetical) method provides all the immediacy benefits of a dynamic language like Python with the compile time safety and ease of use of a statically typed language.

PDC 2010: The Future of F#: Data & Services at Your Finger Tips – Don Syme:

Programming today exhibits a voracious appetite for information, and one of the most important trends in languages today is to make access to data and services fluent and seamless. Come and see the latest from the F# team, and learn how we are extending F# to embed the analytical programmer instantly in a world of typed data and services, whether they be web, enterprise, client or local.

From the slides that are now up on the PDC site, it looks like F# 3.0 is about to deliver!

Game of Life

Last week I had the pleasure of attending Software Craftsmanship 2010 at the magnificent Bletchley Park mansion on a gloriously sunny autumn’s day.


One of the great sessions I attended was Michael Hunger’s:

Using the Game Of Life to code, discuss language, paradigms and approaches

Michael used a video of Jon Conway himself to explain the rules; which he followed up by showing a very interesting implementation using APL. Afterwards we all paired up to try implementations in various languages. After about half an hour my pair came up with the following F# implementation.

Initialise a board as a 2D Array:

let width, height = 12,12
let board = Array2D.init width height (fun x y -> 0)

Computing the next generation of life requires finding the neighbours of a specified cell:

let computeNeighbours x y (board:int[,]) =
    let getValue (x,y) =
        let x = (x + width) % width
        let y = (y + height) % height
        -1,0;      1,0;
        -1,1 ;0,1;1,1;  
    |> (fun (dx,dy) -> x+dx,y+dy)
    |> getValue
    |> Seq.sum

To compute the next generation the current board is mapped to a new one, computing life  by matching the result of the compute neighbours function to the rules of life:

let doGeneration board =
    |> Array2D.mapi (fun x y n ->
        match n, computeNeighbours x y board with
        | _, 3 -> 1
        | n, 2 -> n
        | _, _ -> 0

The board is shown to both the console and Visual Studio output window:

let show (board:int[,]) =
    for y = 0 to height-1 do    
        for x = 0 to width-1 do
            let v = if board.[x,y] = 0 then '0' else '1'
            Console.Write v
            Debug.Write v
        Console.WriteLine ()
        Debug.WriteLine ""

Finally the board is set up and a number of generations are run:

do  [7,7;8,7;9,7;9,6;8,5]        
    |> List.iter (fun (x,y) -> board.[x,y] <- 1)
do  let mutable current = board
    for i = 1 to 10 do
        show current
        current <- doGeneration current

A visually prettier implementations was achieved using JavaScript and HTML during the exercise. So just for fun I tried running the F# code inside WebSharper (which converts F# code directly to JavaScript). A few minor changes required:

  • The 2D Array for the board was replaced with a Jagged Array
  • The output was converted to HTML
  • The loop changed to an Asynchronous Workflow

You can see the WebSharper version running here:

Coincidentally Adam Granicz of Intellifactory, who was very recently awarded an F# MVP, will be giving a talk about his WebSharper product this Tuesday October 12th; along with Can Erten on Backtracking, for the Functional Londoner’s Meetup Group at Skills Matter!

GameOfLife.fs (1.36 kb)

GameOfLifeWebSharper.fs (2.55 kb)