Phillip Trelford's Array

POKE 36879,255

Concurrency for Cats

F# asynchronous programming has been instrumental in taming concurrency while building a Silverlight 4 Trading Application, written with both C# and F#.

To learn more, PLEASE VOTE for my MIX11 open session proposal:

Simplifying Silverlight Concurrency with F# Asynchronous Programming

Phillip Trelford

In Silverlight communication is asynchronous to avoid blocking the UI thread of the browser. Writing asynchronous code in C# or VB.Net can be complex and error prone. F# is a powerful new programming language available in Visual Studio 2010 and compatible with Silverlight. F# has a number of built-in features which greatly help simplify asynchronous programming. With F# Asynchronous Workflows communication code can be written in a more natural and concise synchronous style then executed asynchronously with the compiler doing the hard work. F# Agents provide Erlang style message parsing, which make it easy to write reactive applications that receive inputs from various sources in parallel. This talk will include plenty of live code examples, and draw on recent experience of building a Silverlight based Trading Application.

UPDATE! The results are now in at And I will be attending MIX 2011, but unfortunately not speaking, thanks all the same for everyone who voted.

If you are still interested in the talk I have proposed it for DDD Scotland where again it can be voted for (or not).

Functional Style BDD Steps

    Could a BDD framework be implemented simply as a fold over a sequence of lines in a document?
    Or in the words of Apple:

This changes everything. Again


Or in the words of Gojko Azdic:

Like anything else in F#, the primary use of this feature is to send it to 10 competitors whose brains you want to melt.

Let inception commence.


As defined on Wikipedia the Fold (higher-order function):

In functional programming, fold, also known variously as reduce, accumulate, compress or inject, is a family of higher-order functions that iterate an arbitrary function over a data structure in some order and build up a return value.

Or more succinctly defined via the F# List.fold signature on MSDN

  • ('State -> 'T -> 'State) -> 'State -> 'T list –> 'State

Fold high-order function implementation examples in F# and C#:


let rec fold f acc = function

    | [] -> acc

    | x :: xs -> fold f (f acc x) xs

public static TAcc Fold<TItem,TAcc>

    (Func<TAcc, TItem, TAcc> f,

     TAcc acc,

     IEnumerable<TItem> items)


    foreach (var item in items)

        acc = f(acc, item);

    return acc;




Thusly a BDD framework can be thought of simply as a fold that iterates over a document passing the lines or steps to an arbitrary matching function.

Signature:  fold: (‘context –> step –> ‘context) –> step list –> ‘context

With the accumulator value threaded between calls to the matching function representing the context, which can be explicitly scoped to the scenario under test!

Pattern Match

Given the following Feature document “Addition.txt”:


And an immutable calculator type:

type Calculator = { Values : int list } with
    member this.Push n = {Values=n::this.Values}
    member this.Add() = {Values=[List.sum this.Values]}
    member this.Top = List.head this.Values


When folding over the lines in each scenario of the feature:

let feature = parse "Addition.txt"
|> Seq.iter (fun scenario ->
    |> List.fold performStep (Calculator())            
    |> ignore


And the following matching function is defined:

let performStep (calc:Calculator) (step,line:LineSource) =
    match step with
    | Given "I have entered (.*) into the calculator" [Int n] ->
        calc.Push n                        
    | When "I press add" [] -> 
        calc.Add ()
    | Then "the result should be (.*) on the screen" [Int n] ->
    | _ -> sprintf "Unmatched line %d" line.Number |> invalidOp


Then the feature test succeeds!



Parsing of the feature file is achieved using TickSpec’s parser module. TickSpec is an Open Source BDD framework written in F#. Underneath TickSpec’s parser is also implemented as a fold (well technically actually a scan) over a Gherkin feature document using regular expressions and Active Patterns.

To support the pattern matching function above the following Active Patterns are required:

module Patterns =
    open System.Text.RegularExpressions
    let Regex input pattern =
        let r = Regex.Match(input,pattern)
        if r.Success then 
           Some [for i = 1 to r.Groups.Count-1 do yield r.Groups.[i].Value]
        else None
    let (|Given|_|) (pattern:string) (step) =
        match step with
        | GivenStep input -> Regex input pattern        
        | WhenStep _ | ThenStep _ -> None
    let (|When|_|) (pattern:string) (step) =
        match step with
        | WhenStep input -> Regex input pattern        
        | GivenStep _ | ThenStep _ -> None    
    let (|Then|_|) (pattern:string) (step) =
        match step with
        | ThenStep input -> Regex input pattern        
        | GivenStep _ | WhenStep _ -> None
    let (|Int|) s = System.Int32.Parse(s)


All the code to TickSpec including the parser and this sample (in Examples/Functional) are available at:



Finally the tests scenarios can be easily run within an NUnit test runner using the TestCaseSource attribute to parameterize a test method:

type AdditionFixture () =
    member this.TestScenario (scenario:ScenarioSource) =
        scenario.Steps |> Seq.fold performStep (Calculator.Create())
        |> ignore  
    member this.Scenarios =       
        let feature = parse "Addition.txt"        
        |> Seq.filter (fun scenario ->
            scenario.Tags |> Seq.exists ((=) "ignore") |> not


Launch the NUnit Test Runner and the test passes:



Cucumber style BDD Step Definitions with F#

This article describes how to define and execute BDD Step Definitions using F# in a similar, perhaps more concise, manner to using Ruby with Cucumber.

Behaviour Driven Development (BDD)

BDD is a relatively new Agile software development approach (circa 2003) that focuses on communication; encouraging collaboration between developers, QA and business participants; to help bridge the Business-IT alignment gap. Through discussion with stakeholders, scenarios in which the software will actually be used are written to build up a clear understanding of the desired behaviour.

Scenarios are specified in plain-text in a structured form composed of steps:

  • Given some context
  • When some event occurs
  • Then I expect some outcome

Developers and Testers use these scenarios as acceptance criteria to drive development. A feature is considered done when the tests pass for all of its scenarios.

Using a BDD framework, scenario specifications can be automated as acceptance tests, thereby creating living documentation that is in sync with the code. These days there is a plethora of Open Source BDD frameworks covering just about every programming language and platform.


Cucumber is probably the best known BDD framework at the moment. It executes plain-text files written in a business-readable domain-specific language (known as Gherkin) as automated tests. Each file describes a feature and its scenarios. During execution steps of a scenario are matched against Step Definitions composed of a regular expression and a function to execute. Cucumber is written in Ruby, and has benefited from Ruby’s light syntax and expressiveness to minimize the ceremony while defining step definitions:

Given /a customer buys a black jumper/ do

.Net BDD Frameworks

There are a number of Open Source BDD frameworks for.Net that can parse Gherkin based text specification files including:


Typically .Net BDD frameworks take a reflection based approach, marking functions with attributes containing the regular expression to match against:

[Given(@"a customer buys a black jumper")]
public void GivenACustomerBuysABlackJumper()


StorEvil supports Reflection-based pattern matching representing spaces in C# method names as underscores:

public void Given_a_customer_buys_a_black_jumper()   


While TickSpec supports F# back-tick method names that can contain white space.

let [<Given>] ``a customer buys a black jumper`` () = ()


Note: A reflection based approach requires a compile and run cycle to test a feature. For iterative development in larger applications, Ruby developers may have the advantage, with Ruby being a dynamic language no compilation step is required. Matters can be improved for .Net developers by running individual tests in isolation using tools like Test Driven .Net or ReSharper, however this still does not mitigate the build step.


F# is a powerful .Net language that ships with Visual Studio 2010. Although F# is a statically typed language, using type inference, developers can still benefit from a light and expressive syntax. F# can also execute scripts interactively. While as a first-class .Net programming language F# code interoperates easily with C# and VB.Net.

Using the TickSpec BDD framework (also written in F#) to parse the feature and scenarios; it is possible to specify step definitions concisely in a similar way to Cucumber for Ruby, i.e. without reflection:

Given "a customer buys a black jumper" <| fun []> ()


This approach allows step definitions to be executed in F# interactive

F# Interactive

Fragments of F# code can be executed as scripts using an F# interactive session which can be run from:

F# Interactive makes it easier for developers to take a more explorative approach to coding.



Lets take the example from the front page of the Cucumber website and convert the code portions to F#:

Feature text file:


Ruby step definition:
Calculator Steps 

F# step definition:

Given "I have entered (.*) into the calculator" <| fun [Int n] ->         
    calculator <- Calculator()
    calculator.Push n  

Ruby Calculator class:


F# Calculator class:

type Calculator () =
    let mutable values = []
    member this.Push(n) = values <- n :: values

Ruby Console output:


F# Interactive output:



How it works


Given, When and Then are defined as F# functions with 2 parameters:

  1. s: a regular expression pattern string
  2. f: a handler function that takes a list of the string values matched

/// Registers specified pattern's handler function
let Given s f = RegisterStep givens s f
/// Registers specified pattern's handler function
let When s f = RegisterStep whens s f
/// Registers specified pattern's handler function
let Then s f = RegisterStep thens s f


Function RegisterStep records the pattern and handler in a dictionary:

let private givens, whens, thens = 
    Dictionary<_,_>(), Dictionary<_,_>(), Dictionary<_,_>()

let private RegisterStep 
        (handler:string list -> unit) = 
    stepDefinitions.[pattern] <- handler


Feature text files are parsed using the TickSpec parser and scenario steps are tried against the registered pattern and function handlers:

/// Executes specified feature lines writing output to console
let Execute (lines:string[]) =  
    let feature = TickSpec.FeatureParser.parseFeature lines
    Console.WriteLine feature.Name
    |> Seq.iter (fun scenario -> 
        Console.WriteLine scenario.Name
        scenario.Steps |> Seq.iter tryStep


Inside the step definitions Active patterns can be used to parse strings:

let (|Int|) = function s -> System.Int32.Parse(s)


Allowing easy type conversion of parameters:

Then "the result should be (.*) on the screen" <| fun [Int n] ->


All the above code is available in the Interactive project example of TickSpec on CodePlex.



Businesses are reporting real business value from adopting BDD; in Gojko Adzic’s forthcoming Specification by Example book he examines over 50 case studies of this style of technique. BDD is gaining real traction in the development community with plenty of talks at conferences and user groups on the subject, along with blog articles and books now available.

Hopefully this article has helped demonstrate the applicability of F# for developing BDD Step Definitions on the .Net platform. From experience, I believe that this can be a good way to gain value from introducing F# to an organization.

Regardless of your language preference, given that your business specifications could outlive your current platform and programming language, I’d recommend choosing a framework that is based on the well supported Gherkin business language, so that you are not locked in to a specific technology in the future.



BDD References:

F# Interactive References: