Phillip Trelford's Array

POKE 36879,255

A Fistful of Dollars

Just over a week ago I took the Eurostar over to Paris for NCrafts, a conference bringing together over 300 software craftsmen and craftswomen:

The event was held in a crypt and featured a good number of F# sessions:

Mathias Brandewinder gave an excellent closing talk on The T in TDD : tests, types, tales.

 NCrafts 2015 - May 2015

In this live coding session, Mathias took the multi-currency money example from Kent Beck’s seminal Test-Driven Development by Example book. First implementing a dollars class in C# driven by a unit test for quick feedback and then contrasting it with a similar implementation in F# using the REPL for immediate feedback.

Unit Test

The system needs to be able to multiply a price in dollars by a number of shares, so that 5 USD * 2 = 10 USD:

public class Tests
{
   [Test]
   public void five_dollars_times_two_should_equal_ten_dollars()
   {
      // arrange
      var five = new Dollars(5);
      // act
      var result = five.Times(2);
      // assert
      Assert.AreEqual(new Dollars(10), result);
   }
}

C# Dollars

Based on the test an immutable dollars class can be implemented:

public class Dollars
{
   private readonly decimal _amount;

   public Dollars(decimal value)
   {
      _amount = value;
   }

   public decimal Amount
   {
      get { return _amount; }  
   }

   public Dollars Times(decimal multiplier)
   {
      return new Dollars(this._amount * multiplier);
   }
}

The code now compiles, but the test fails!

C# Equality

The test fails because in C# class types use reference equality, so we must override Equals:

public class Dollars
{
   private readonly decimal _amount;

   public Dollars(decimal value)
   {
      _amount = value;
   }

   public decimal Amount
   {
      get { return _amount; }  
   }

   public Dollars Times(decimal multiplier)
   {
      return new Dollars(this._amount * multiplier);
   }

   public override bool Equals(object obj)
   {
      var that = obj as Dollars;
      return
         that != null
         ? this.Amount == that.Amount
         : false;
   }
}

Note: at this point FXCop will also recommend that we implement GetHashCode as we’ve implemented Equals.

F# Dollars

In F#, the simplest thing that could possibly work is a measure type which gives compile time type safety:

[<Measure>] type usd

5.0M<usd> * 2.0M = 10.0M<usd>

We can also test it immediately in F# Interactive as above, or alternatively write a unit test as below:

let [<Test>] ``5 USD * 2 = 10 USD`` () =
   Assert.AreEqual(10M<usd>, 5M<usd> * 2M)

Note: F# units of measure are erased at compile time meaning there’s no runtime performance penalty.

F# Money

For a report we’d probably want to encode money dynamically with a currency component. Below I’ve chosen an F# record type:

type Money = { Amount:decimal; Currency:string } with
   member this.Times(multiplier) = { this with Amount = this.Amount * multiplier }

let USD amount = { Amount=amount; Currency="USD" }

USD 10M = (USD 5M).Times(2M)

This succeeds immediately as F# implements equality (and GetHashCode) by default for us on record types.

Unquote

As an aside, I find assertions over numerical types are more natural using the Unquote library which lets you assert equality using the equals operator, i.e.

let [<Test>] ``5 USD * 2 = 10 USD`` () =
   test <@ (USD 5M).Times(2M) = USD 10M @>

Summary

When writing code we may seek quick feedback on our first implementations. In C# we’d typically write reflection based unit tests to get early feedback, in F# we may use F# interactive first for immediate feedback and later promote useful tests to reflection based tests that run as part of our continuous build and may help find regressions.

Also in this scenario implementing types in F# required a lot less boilerplate than the equivalent C# code.

The only 3 apps you’ll ever write…

At last week’s F# eXchange Robert Pickering gave a talk on Expression Oriented Programming with F#, with one of the slides condensing the only apps you’ll ever write down to 3:

OnlyThreeApps

In the same week fsharpWorks conducted an F# Survey, the results of which are well worth a look.

One of the questions was ‘What kind of learning material would you like to see more of?’ with a popular answer being ‘More material with short "cookbook" style information’.

So lets take a look at 2 out of 3 of the only apps you’ll ever write :)

Command line

An easy place to get started with F# is to use a popular IDE, for Windows there’s the free Visual Studio 2013 Community edition, and for Mac and Linux there’s Xamarin Studio and MonoDevelop.

To create a new console app in Visual Studio hit File –> New Project and select the F# Console Application project:

image

For the ubiquitous hello world app write:

printfn "Hello World"

Then run with Debug –> Start without Debugging (Ctrl+F5), which runs the app and waits for you to press a key.

To make things a little more exciting we could display all the files in the working directory:

let files = System.IO.Directory.GetFiles(".")
for file in files do printfn "%s" file

GUI App

An easy option in F# for creating a GUI app is to use the Windows Forms library.

To do this simply add a reference to you’re existing console application. In Visual Studio right click the project’s references folder and select the System.Windows.Forms.dll reference.

Then right click the project and select Properties to change the application type to a Windows Application:

image

Now insert the following code to display a form:

open System.Windows.Forms

[<System.STAThread>]
do let form = new Form()
   Application.Run(form)

You should see an empty window on the screen.

Let’s make things a little more interesting with a grid with some data, carrying on the file information theme:

open System.IO
open System.Windows.Forms

[<System.STAThread>]
do let form = new Form()
   let grid = new DataGridView()
   grid.Dock <- DockStyle.Fill   
   form.Controls.Add(grid)
   form.Load.Add(fun _ ->
      let files = Directory.GetFiles(".")
      grid.DataSource <- [|for file in files -> FileInfo(file)|]   
   )
   Application.Run(form)

When you run it you should see something like this:

image

That’s all folks! Just a few lines of code and we’ve got a grid up.

Summary

Hopefully you can see in this F# introduction creating a console app or GUI app is a relatively simple task.

If you’d like the complete set, then check out Tomas Petricek’s tutorial on MSDN: Creating Windows Services in F# or Mike Hadlow’s recent self-hosted web service sample: A Simple noWin F# example.

F# Exchange 2015

This Friday saw the first ever F# eXchange, a one-day 2 track conference dedicated to all things F#, hosted at Skills Matter in London and attracting developers from across Europe.

There was a strong focus on open source projects throughout the day including MBrace (data scripting for the cloud), Fake (a DSL for build tasks), Paket (a dependency manager for .Net), the F# Power Tools and FunScript (an F# to JS compiler). In fact all the presenters used the open source project FsReveal to generate their slides!

Keynote

Tomas Petricek opened proceedings with a keynote on The Big F# and Open Source Love Story:

Slow development

One of Tomas’s observations, on slow development for open source projects resonated with many, where successful projects often start as just a simple script that fulfils a specific need and slowly gather momentum over time.

As an example, in Steffen Forkmann’s presentation he talked about how Fake had started as a simple F# script and over the years seen more and more contributors and downloads, with the addition of high quality documentation having a huge impact:

Talks

All the talks were recorded, and all the videos are already online!

Speakers

The videos:

Steffen also took advantage of his talk to make a special announcement about Paket:


Panel

The day ended with some pizza, drinks and a panel organized by prolific F# contributor, Don Syme:

Panel Discussion

Each panel member pitched why they thought F# was good in their core domain area from cloud, games, design, data science, scripting through to web.

There were some interesting discussions, and some mentions of the recent fsharpWorks led F# Survey.

2016

The date for next year’s F# eXchange 2016, the 16th April, is already in the calendar, hope to see you there, and please take advantage of the early bird ticket offer, only 85GBP up until the 16th June!