Phillip Trelford's Array

POKE 36879,255

The Rise of F#

Back in September 2008, Scott Hanselman ran a piece entitled the Rise of F#. The following year, in October 2009, Somesegar announced the release of F# in VS 2010. April 2010 saw F# 2.0 ship with VS2010. 6 months on F# goes open source under the Apache 2.0 license. Last month, F# entered the TIOBE top 20 for the first time, the first functional language to do so.

According to TIOBE the recent rise in popularity comes as no surprise:

Apart from being a nicely designed language, F# is available in the latest version of Microsoft's Visual Studio (2010).

A year before the August 2010 edition of the ThoughtWorks Technology Radar commented on the rise in interest of functional languages F#, Scala and Clojure:

Two characteristics of functional languages in particular are driving this interest, immutability with its implications for parallelism and functions as first class objects. While the introduction of closures to C# brings some of the latter capability, functional languages are almost synonymous with immutability.

Clearly functional programming has much to offer and interest in F# is rising rapidly. But before we get ahead of ourselves it should be noted that in the TIOBE index C# received a rating of 6.042%, while F# received a mere 0.604%. It therefore seems unlikely F# is going to overtake C# anytime soon, or for that matter Scala overtake Java. However,rather than thinking of F# as a competitor to C#, in many scenarios it is probably more appropriate to think of it is complimentary.

Don Syme remarks in the article Putting the ‘Fun’ into ‘Functional’, on F# 2.0:

F# is complementary to .NET stalwarts C# and Visual Basic .NET, which remain integral to the majority of .NET projects. But C# and Visual Basic programmers can use F# components in their existing applications, enabling developers to maintain their current investments and integrate F# on a component-by-component basis.

Sitting on the near horizon is F# 3.0 and the “magic” of Type Providers:

type Netflix = ODataService<"http://odata.netflix.com">

type SQL = SQLConnection<"Server='.\SQLEXPRESS'... ">

type EmeaSite = SharePointSide<"http://myemea/">

 

Jo Palmer describes F# Type Providers:

Type Providers are a unique mechanism for applying the benefits of strong static typing to external, dynamic data sources. For example, Type Providers enable IDE support for “Intellisense”-style completion on dynamic data sources while exploring their APIs. When the code is compiled, programmers can then enjoy static typing against these data sources with a variety of options for code generation.

F# already provides the kind of terseness experienced with dynamic languages while at the same time allowing the type safety and performance of a statically typed language. With F# 3.0 this capability is extended to accessing data and services.

Interested? Try F# interactively in your browser now.

Back to the now, the TIOBE ratings are based on skilled engineers world-wide, courses, third party vendors and results from popular search engines. Lets take a look at where some of those numbers come from.


Skilled Engineers

According to IT Jobs Watch the trend in demand for F# developer in the UK is up:fsharp-permanent-demand-trend

There is also plenty of anecdotal evidence of the use of F# in the Enterprise from case studies and user group membership.

Selection of F# case studies:

Selection of F# user groups:

At time of writing both the London and New York F# user groups had over 300 members!


Courses

 

In the UK, there are a selection of F# workshops now available through Skills Matter:

F# is also starting to be part of university curriculums around the globe:

With a good selection of books available in print:

Beginning F# Programming FSharp Book Professional F# 2.0 Real World Functional Programming F# for Technical Computing Expert F# 2.0

 

And books online:


Third Party Vendors

MSDN has a good article on F# Tools & Resources by Terrence Dorsey, which details some of the F# Open Source projects, including:

  • Fake – a build automation system
  • TickSpec – a BDD framework
  • FsCheck – a randomized testing framework

There are also a number of commercial products targeting F#:


Search

Google returns 3,720,000 results for “F# programming”.

If that’s a bit daunting a good place for the latest articles is the MSDN F# Developer Center.


Finally, something silly

Scott Hanselman ended his Rise of F# article with “something silly”, a 2D Tron-Clone game written in 182 lines of code, which you can now play online if you have Silverlight installed.

A recent post on the F# Snippets site gave us a Mine Sweeper in 99 lines of code.

FineSweeper

Update 23rd September 2011:

F# is the programming language of choice for discriminating hackers!

What makes a good step definition

Following on from last week’s What makes a good feature file article, this week it’s time to tackle what makes a good step definition. To automatically test that an application’s behaviour matches that defined by the scenarios in a feature file, a mapping is required. Step definitions provide that mapping from the lines in a feature file to the code that implements the feature.

Starting with a Given:

Given I have entered 0.1134 into the calculator

Implementations

Ruby step definition for use with Cucumber::

Given /I have entered (.*) into the calculator/ do |n|
  # Some Ruby code here
end

The code above specifies a regular expression used to match lines in the feature file and an anonymous function to execute.

C# attributed step definition compatible with both SpecFlow and TickSpec:

[Given(@"I have entered (.*) into the calculator")]
public void GivenIHaveEnteredNIntoTheCalculator(int n)
{
  // C# code here
}

Again a regular expression is specified, however this time the function is public and reflection is used for invocation.

F# attributed step definition compatible with TickSpec:

let [<Given>] ``I have entered (.*) into the calculator``(n:int) =
  // F# code here
}

F# allows white space and special charecters in method names enclosed with backticks. This removes the need for both an attribute and a function name.

TickSpec also supports anonymous functions as Ruby with Cucumber, i.e.:

Given "I have entered (.*) into the calculator" (fun [Int n] –>
  // F# code here
)

From a terseness point of view it looks like Ruby and F# the advantage for writing step definitions. But that’s only part of the story…

Scope 

By default in frameworks like Cucumber and SpecFlow step definitions are defined globally, so that they can be shared or reused. Global functions are usually fine for small programs, but for larger programs we usually group them into classes or modules for maintainability.

With SpecFlow and TickSpec it is possible to constrain the scope of a step definition or set of step definitions via a StepScope attribute:

[StepScope(Feature="Addition")]
public class AdditionSteps
{
  [Given(@"I have entered (.*) into the calculator")]
  public void GivenIHaveEnteredNIntoTheCalculator(int n)
  {
    // C# code here
  }
}

Here the step definition methods in the AdditionSteps class are only matched against the Addition feature. This allows the wording in the feature file to evolve without affecting other feature files, and it allows the actions in the methods to vary too. It also means that it is easy to find the step definitions that map to a feature file in one place.

Using a class level step definition scope is quite similar conceptually to using a View Model within the MVVM pattern, where the View Model is responsible for mapping from view to model. It adds a layer of indirection over binding directly to the model, but allows the application to evolve more easily over time.

Note: TickSpec can apply scope to both Step bindings and Event bindings like BeforeScenario and AfterScenario. There is currently was an open bug on this in SpecFlow (since fixed).

Fluent interfaces

So far the step definition examples have included a “code here” comment. This is where we arrange, act and assert. In the Given steps the preconditions are arranged. The When steps execute the actions. Finally the Then steps assert the outcome. This is almost identical to the steps you’d take in TDD for a unit test, except the parts are spread across multiple methods.

Note: The state for a scenario can also be initialized in a method marked with BeforeScenario attribute

For the arrange and act steps, DSL approaches like fluent interfaces can be useful for making step definitions more readable and maintainable.

In the arrange steps using the builder pattern is useful to build up the initial state:

public void constructPizza()
{
  pizzaBuilder.createNewPizzaProduct();
  pizzaBuilder.buildDough();
  pizzaBuilder.buildSauce();
  pizzaBuilder.buildTopping();
}

In the act steps a fluent assertion API is useful, for example the FsUnit F# library:

1 |> should equal 1
anArray |> should haveLength 4
anObj |> should not (be Null)

Summary

Using fluent interfaces inside the step definition functions can make them more readable and maintainable. For larger projects constraining the scope of step definitions helps make them more maintainable. Finally step definitions can sometimes be written more concisely in languages like Ruby and F#.

Functional Programming with F#

This month F# became the first functional programming language to hit the top 20 of the TIOBE Programming Community Index:

The recent rise in popularity of F# comes as no surprise. Apart from being a nicely designed language, F# is available in the latest version of Microsoft's Visual Studio (2010).

Functional programming is by no means a new concept in fact it has its roots in the Lambda Calculus developed in the 1930s. The first popular functional flavoured language Lisp was developed in the 1950s and ML, a language that F# owes much of its syntax to, was developed  in the 1970s.

Some claim that functional programming concepts appear unfamiliar, in fact they are all around us, in Javascript, C#’s LINQ and even spreadsheets.. If you’ve ever written formulas in a spreadsheet then you’re already familiar with some of the principles of functional programming. The key building blocks of a spreadsheet are cells containing functions and values.

Over on Developer Fusion there’s a tutorial that demonstrates some key functional concepts while actually building a spreadsheet application in a couple of hundred lines of F#:

The functioning spreadsheet

If you already have some F# or functional programming then why not give it a try, otherwise you might want to read on.

Functional Programming Basics

The following concepts are considered specific to Functional Programming:

  • Pure functions
  • First class and higher order functions
  • Pattern matching

Pure Functions

Pure functions are functions that are free from side effects. That is pure functions always return the same result when called with the same parameters. Formulas in a spreadsheet hold this property.

First class and higher order functions

Functions are said to be first-class when they can be treated as first class objects. So like objects, functions can be passed as arguments to other functions, returned as values, assigned to values or stored in data structures.

Higher order functions are functions that take functions as their arguments. For example the LINQ Select method is an extension method that takes the function selector as a parameter:

public static IEnumerable<TSource>

    Select<TSource, TResult>(

        this IEnumerable<TSource> source, Func<TSource, TResult> selector)


Higher order functions are often used with anonymous functions as arguments. In the following line of code an anonymous function is passed to the Select function:

var squares = (new[] { 1, 2, 3 }).Select(n => n * n);


Support for higher-order and anonymous functions has been available in C# since 3.0 and will be available in C++ in the C++0x standard. They are also an integral part of JavaScript.

Pattern Matching

Many C based languages including C# supports basic pattern matching in the form of switch statements, matching against integral value and string types. In F#, and other ML based languages, a match statement is used instead:

C#

F#

switch (day) {

case 0: return "Sunday";

case 1: return "Monday";

case 2: return "Tuesday";

case 3: return "Wednesday";

case 4: return "Thursday";

case 5: return "Friday";

case 6: return "Saturday";

default:

 throw new

ArgumentException("day");           

}

 

match day with

| 0 -> "Sunday"

| 1 -> "Monday"

| 2 -> "Tuesday"

| 3 -> "Wednesday"

| 4 -> "Thursday"

| 5 -> "Friday"

| 6 -> "Saturday"

| _ –>

invalidArg "day" "Invalid day"

 

 

Functional programming languages allow pattern matching on types too. The following C# and F# code snippets are equivalent:

C#

F#

abstract class Shape { }

class Line : Shape {

 public Point Pt1;

 public Point Pt2;

}

class Square : Shape {

 public Point Pt;

 public float Size;

}

void Draw(Graphics g,Pen pen,Shape shape)

{

 if (shape is Line)

 {

  var line = (Line)shape;

  g.DrawLin(pen,line.Pt1,line.Pt2);

}

 else if (shape is Square)

{

  var sq = (Square)shape;

  g.DrawRectangle(pen,

   sq.Pt.X, sq.Pt.Y,

   sq.Size, sq.Size);

 }

}

type Shape =

| Line of Point * Point

| Square of Point * float

 

 

 

 

 

 

let draw (g:Graphics,pen:Pen,shape)=

  match shape with

  | Line(pt1,pt2) ->

     g.DrawLine(pen,pt1,pt2)

  | Square(pt,size) ->

     g.DrawRectangle(pen,

      pt.X,pt.Y,size,size)

 

 

In the code above the shape type is defined using an F# discriminated union, also known as algebraic data types in other functional languages like Haskell and Miranda. You can think of the type Shape as defining an empty abstract base class, and Line and Square as classes that inherit from the base class, just like the C# code to the left.

Note: The ‘*’ character in “Line of Point * Point” means “and”, i.e. a Line of Point and Point.

See Tomas Petricek and I implement the code above in the following video:

 

Starting F#

If you have Visual Studio 2010 then you already have F# installed, just start a New Project:

image

If you’re on Windows and don’t already have Visual Studio, you can also download the Visual Studio 2010 Shell for free and then install F#.

If you’re on Linux or Mac then you can use F# with Mono with MonoDevelop or Emacs. Or you can even edit F# code in your browser at http://tryfsharp.org.

image

F# Interactive

You can try out your F# code immediately in Visual Studio using the F# interactive window. Just highlight the code and presss ALT+ENTER

image

F# Basics

F# is a statically typed functional programming language on .Net that aims to minimize the ceremony of coding. F# functions are first class so they don’t need to be defined inside a class and type inference means that most of the time the compiler will infer the types of objects for you.

Values

The let keyword is used to define values with type inference (like the var keyword in C#).

Values in F# are by default immutable (constant):

let alwaysTrue = true


Values must be marked explicitly as mutable if you want to be able to change them:

let mutable variable = 1


Functions

The following function uses a conditional expression, with its type is inferred as a function that takes a bool and returns a bool:

let Not value = if true then false else true


Pattern Matching

The Not function can also be written as a match expression:

let Not value =

    match value with

    | true -> false

    | false> true


This is the shorthand equivalent using the function keyword:

let Not = function true -> false | false -> true


Pattern matching can be also be used to match against tabular values:

let And p q =

    match p, q with

    | true,  true   -> true

    | true,  false  -> false

    | false, true   -> false

    | false, false  -> false


One of the nice things about F# pattern matching is the added type safety. If a case is missing then the compiler gives a warning.

Discriminated Unions

A discriminated union is a type whose values can be any one of a set of types. You can think of it being like an enum in C# where each item has a specified type.

Option Types

The Option type is a simple example of a discriminated union, and it’s one that’s built into F#:

type Option<'TValue> = Some of 'TValue | None


The Option type is used when an actual value might not exist in which case it is specified as None, otherwise it is Some value. In F# the Option type is preferred over the use of null, and this helps remove a whole class of bugs.

Simple Parsing

Parsing a Spreadsheet formula based on whether it starts with an equals (‘=’) sign:

let parseFormula (s:string) =

    if s.StartsWith "=" then Some(s.Substring(1))

    else None


Parsing a decimal string value:

let parseNumber s =

    match Decimal.TryParse(s) with

    | true, n -> Some n

    | false, _ –> None


For a primitive spreadsheet we could define cell values as being either formulas or numbers:

type value = Formula of string | Number of decimal

Now we can match a text string with the parsers for formulas and numbers:

let parse s =

    match parseFormula s, parseNumber s with

    | Some formula, None -> Formula formula

    | None, Some number  -> Number number

    | _, _ -> invalidOp "Unknown value type"


Active Patterns

Active Patterns are defined inside banana brackets, i.e. ‘(|’, ‘|)’. An Active Pattern is a function that can be used actively inside a pattern matching expression.

The following Active Pattern returns whether a number is Odd or Even:

let (|Odd|Even|) n =

    if i % 2 = 1 then Odd else Even


The Active Pattern can then be used in a match expression:

match 1 with

| Odd -> "Odd"

| Even -> "Even"


A partial Active Pattern goes one step further and tries to match a specified value returning an Option of either Some value if successful, or None otherwise.

The following FormulaText partial Active Pattern can use the same body as the parseFormula function we defined earlier:

let (|FormulaText|_|) (s:string) =

   if s.StartsWith "=" then Some(s.Substring(1))

   else None


As the following NumericValue can use the same body as the earlier parseNumber function:

let (|NumberValue|_|) (s:string) =

    match Decimal.TryParse(s) with

    | true, n -> Some(n)

    | false, _ –> None


Now the Active Patterns may be used to simplify the earlier parse function:

let parse s =

    match s with

    | FormulaText text -> Formula text

    | NumberValue n -> Number n

    | _ -> invalidOp "Unknown value type"

 

What Next?

Time to put your new F# and functional programming skills to work and try creating your own Spreadsheet via the tutorial on Developer Fusion: