Phillip Trelford's Array

POKE 36879,255

Blog 101

This is a meta post to coincide with posting over 100 articles over 4 years, going back over some the more popular along with some of my own personal favourites. I started blogging regularly back in 2007 on the Applied Games Group blog, here’s a selection from there: 

Then in 2008 I started this blog with the motivation of keeping a record of some of the things I’ve learned while improving my technical writing skills with a view to working on a book. This week in collaboration with Tomas Petricek I signed a book deal with Manning.

Many of the posts are written during my daily commute from Cambridgeshire down to London, which takes just over an hour. The ideas for posts come at different times, more often when I’m away from my computer taking a walk or a cycle or having a bath. Once an idea has solidified in my mind, I usually start by writing a working title, an opening sentence, a brief outline and then fill in the gaps.

Tools/Tech:

Most popular articles

More often than not it’s the articles I least expect to be popular that are. Typically early views come from Twitter, and then from link sites like Chris Alcock’s Morning Brew and Alvin Ashcraft’s Morning Dew. My biggest traffic spikes have come from CodeProject.

My picks

A selection of interesting technology and some minor rants.

Mini Games

My interest in programming started with video games, and I still enjoy writing mini games. I typically write them in Silverlight so that I can embed them in the blog.

Open Source Projects

Introductions to some of my open source projects on CodePlex.

Thanks!

Thanks for all the feedback, it really helps, don't be shy :)

Progressive F# Tutorials 2012 NYC – Day 2

On day 1 of the Progressive F# Tutorials in NYC on the beginner’s track we started with the F# Koans to introduce F#’s syntax and language features, and then finished up with F# Katas to put some of that learning into practice.

On day 2 the idea was to look at how to structure F# programs through a tutorial on Domain-specific languages (DSLs) in F#, and finish up the day with a fun programming contest.

ProgressiveFSharpTutorialsNYC2012

DSLs

Here are the slides, they’re a little sparse as most of the session was a hands-on tutorial with live code examples:


DSL: UML Sequence Diagrams

This sample shows how easy it is to create an internal DSL in F#:

type Object(name) = member o.Name = name
type Action = Action of string * Object * Object

let fred = Object("Fred")
let bob = Object("Bob")
let hank = Object("Hank")
let renee = Object("Renee")

let (--) (source:Object) (message:string) = 
    (source,message)
let (-->) (source:Object,action:string) (target:Object) = 
    Action(action,source,target)

let actions =
    [
        fred --  "order food" --> bob
        bob  -- "serve wine"  -->  fred
        hank -- "pickup"      --> bob
        bob  -- "serve food"  --> fred
        fred -- "pay"         --> renee
    ]

The sample above uses a single line to define an class for specifying object’s and their associated names, and another line using a discriminated union to define actions between objects. Custom operators are used to allow the actions to be defined in a way that is similar to drawing in a UML Sequence diagram

DSL: Retail Domain

Next up we created a simple domain for Retail Point-of-Sale (POS) application:

type Code = string
type Name = string
type Price = decimal
type Product = Product of Code * Name * Price
type Quantity = decimal
type LineNo = int
type LineItem = 
  | SaleLineItem of LineNo * Product * Quantity
  | TenderLineItem of Tender * Price
  | CancelLineItem of LineNo

Then used it to build a simple command line based Retail application that could lookup items from a barcode scanner and show a total on checkout.

There are some exercises to work through in the attached code.

DSL: Automated Acceptance Testing with TickSpec

Before the break I gave a brief introduction to TickSpec an F# library for automating acceptance tests. TickSpec allows you to wire up English language test specifications to your code in a variety of ways including regular expressions.

DSL: Fluent API for Unit Testing

Tomas led a session on building a fluent API using the example of unit testing:

[<Test>]
let ``Sum of 1 to 5 is 15``() =
  [ 1 .. 5 ] |> sum 
  |> should equal 15

In this session Tomas managed to introduce a new custom operator (+/-) which lets you concisely declare range checks:

value should equal 7.0 (+/-) 0.000001

DSL: Spreadsheet

Finally we looked at how to create an external DSL using a Spreadsheet as an example. If you’re interested in learning more I’d recommend looking through these articles:

Mystery Coding Contest

The mystery coding contest was an ant colony simulator where you had to implement an AI for ants to beat a rival colony, which involved attacking the enemy, searching for food, leaving pheromone trails.

Read more over on Rich Minerich’s blog

Progressive F# Tutorials 2011

Last week’s 2-track Progressive F# Tutorials conference seems to have gone pretty well:

Possibly the best Skills Matter event I have attended

progfsharp11

You can see pictures from the event over on the Skills Matter Flickr stream:

Don Syme, Phil Trelford, Mark SeemanChris MarinosKit Eason, Martin Trojer, Don SymeTomas Petricek

Skills Matter also have Videos Podcasts of all the sessions including Don Syme’s keynote:

This was followed by an excellent Tennis Kata in the Programming with the Stars session performed by Mark Needham and Mark Seeman, first in C# and then F#.

wimbledon

Don posted this solution on F# Snippets which is pretty close the 2 Mark's vesion:

 1: /// The two players
 2: type Player = A | B
 3: 
 4: /// The point score in for a player in a game
 5: type PlayerPoints = Zero | Fifteen | Thirty | Forty 
 6: 
 7: /// The score of a game
 8: type Score = 
 9:     | Points of PlayerPoints * PlayerPoints 
10:     | Advantage of Player 
11:     | Deuce 
12:     | Game of Player
13: 
14: /// Compute the next score in a game 
15: let nextPointScore a = 
16:     match a with 
17:     | Zero -> Fifteen
18:     | Fifteen -> Thirty
19:     | Thirty -> Forty
20:     | Forty -> failwith "what??"
21: 
22: /// Check if we've reached deuce
23: let normalize score = 
24:     match score with 
25:     | Points(Forty,Forty) -> Deuce
26:     | _ -> score
27: 
28: /// Score a point in a game
29: let scorePoint score point =
30:     match score, point with 
31:     | Advantage player1, player2 when  player1 = player2 -> Game player1
32:     | Advantage player1, player2 -> Deuce
33:     | Deuce, player -> Advantage player
34:     | Points(Forty, _), A -> Game A
35:     | Points(_, Forty), B -> Game B
36:     | Points(a, b), A -> normalize (Points (nextPointScore a, b))
37:     | Points(a, b), B -> normalize (Points (a, nextPointScore b))
38:     | Game _ , _ -> (* printfn "the game is over!"; *) score
39: 
40: /// Score a whole game, where a game is a sequence of points
41: let scoreGame (points: seq<Player>) = 
42:     Seq.scan scorePoint (Points(Zero,Zero)) points
union case Player.A: Player
union case Player.B: Player
type PlayerPoints =
  | Zero
  | Fifteen
  | Thirty
  | Forty

Full name: Snippet.PlayerPoints

  type: PlayerPoints
  implements: System.IEquatable<PlayerPoints>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<PlayerPoints>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable


The point score in for a player in a game
union case PlayerPoints.Zero: PlayerPoints
union case PlayerPoints.Fifteen: PlayerPoints
union case PlayerPoints.Thirty: PlayerPoints
union case PlayerPoints.Forty: PlayerPoints
type Score =
  | Points of PlayerPoints * PlayerPoints
  | Advantage of Player
  | Deuce
  | Game of Player

Full name: Snippet.Score

  type: Score
  implements: System.IEquatable<Score>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Score>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable


The score of a game
union case Score.Points: PlayerPoints * PlayerPoints -> Score
union case Score.Advantage: Player -> Score
type Player =
  | A
  | B

Full name: Snippet.Player

  type: Player
  implements: System.IEquatable<Player>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Player>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable


The two players
union case Score.Deuce: Score
union case Score.Game: Player -> Score
val nextPointScore : PlayerPoints -> PlayerPoints

Full name: Snippet.nextPointScore

Compute the next score in a game
val a : PlayerPoints

  type: PlayerPoints
  implements: System.IEquatable<PlayerPoints>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<PlayerPoints>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val failwith : string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val normalize : Score -> Score

Full name: Snippet.normalize

Check if we've reached deuce
val score : Score

  type: Score
  implements: System.IEquatable<Score>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Score>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val scorePoint : Score -> Player -> Score

Full name: Snippet.scorePoint

Score a point in a game
val point : Player

  type: Player
  implements: System.IEquatable<Player>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Player>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val player1 : Player

  type: Player
  implements: System.IEquatable<Player>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Player>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val player2 : Player

  type: Player
  implements: System.IEquatable<Player>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Player>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val player : Player

  type: Player
  implements: System.IEquatable<Player>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Player>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val b : PlayerPoints

  type: PlayerPoints
  implements: System.IEquatable<PlayerPoints>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<PlayerPoints>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val scoreGame : seq<Player> -> seq<Score>

Full name: Snippet.scoreGame

Score a whole game, where the game is represented as a sequence of points
val points : seq<Player>

  type: seq<Player>
  inherits: System.Collections.IEnumerable
Multiple items
val seq : seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------

type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>

  type: seq<'T>
  inherits: System.Collections.IEnumerable
module Seq

from Microsoft.FSharp.Collections
val scan : ('State -> 'T -> 'State) -> 'State -> seq<'T> -> seq<'State>

Full name: Microsoft.FSharp.Collections.Seq.scan
val game1 : seq<Player>

Full name: Snippet.game1

  type: seq<Player>
  inherits: System.Collections.IEnumerable


A sample game - A wins every point
val game2 : seq<Player>

Full name: Snippet.game2

  type: seq<Player>
  inherits: System.Collections.IEnumerable


A sample game - A and B swap points indefinitely
val game3 : seq<Player>

Full name: Snippet.game3

  type: seq<Player>
  inherits: System.Collections.IEnumerable


A sample game - A and B trade points but A wins more points than B
val truncate : int -> seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.truncate
val toList : seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList
val randomGame : int -> seq<Player>

Full name: Snippet.randomGame

Generate a random game
val i : int

  type: int
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<int>
  implements: System.IEquatable<int>
  inherits: System.ValueType
val rnd : System.Random
namespace System
type Random =
  class
    new : unit -> System.Random
    new : int -> System.Random
    member Next : unit -> int
    member Next : int -> int
    member Next : int * int -> int
    member NextBytes : System.Byte [] -> unit
    member NextDouble : unit -> float
  end

Full name: System.Random
System.Random.NextDouble() : float
val i : int32

  type: int32
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<int>
  implements: System.IEquatable<int>
  inherits: System.ValueType
val nth : int -> seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.nth
val printfn : Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn

Discriminated unions were used to describe the tennis domain and pattern matching to transition between game score states, e.g. deuce to advantage. Colin Bull came up with a Tennis Monad: http://fssnip.net/8Q.

After lunch the tutorials broke into 2 tracks:

  1. Chris Marinos introduced the F# programming language via his F# Koans
  2. Meanwhile Tomas Petricek showcased Data Access in F#, Today & Tomorrow

 

If you were in the Data Access session you may well have created your own Point of sale application using the Tesco API:

TescoAPI

By the end of the session we’d created our own custom F# 3 Type Provider for .INI files:

IniFileTypeProvider

You can download the source code here.

On the second day in the morning Robert Pickering ran a hands-on session on Building Applications in F# using a WPF Twitter client as the sample. Meanwhile Tomas Petricek and Simon Cousins presented on Asynchronous Programming in F#.

In the afternoon George Stavroulakis & Gian Ntzik ran a very well received session on F# in the cloud, the slides and code for which are available on the M-Brace site.

Unfortunately I couldn’t make their session as I was presenting on TDD & BDD with F#:

The first part of the talk introduced the REPL (F# Interactive) using the code from the Functional Cells: A Spreadsheet in F# article over on the DeveloperFusion site. The tasks for this session are available at: http://trelford.com/progfsharp.zip

Thanks again to all the speakers, attendees, sponsors and Skills Matter for putting on the event.

Please support next year’s Progressive F# Tutorials:

progfsharp2012