Phil Trelford's Array
POKE 36879, 255

DDD North 2013

October 13, 2013 07:08 by phil

I’m heading home on the geek train back from a cracking DDD North, this year held in Sunderland and attracting 430 attendees and three well attended F# talks.

I managed to deliver the first pony of the day in F# eye for the C# Guy:

Starting with live samples of immutable classes in C# and then in F# using the class and record syntax. Then instantiating the classes via the F# interactive (REPL) window and from C# which simply requires a reference to the F# library project, it’s all .Net IL underneath.

Then we moved on to unit testing in F# using a selection of libraries from Nuget including NUnit, FsUnit and Unquote. On the unit testing theme Ian Cooper gave a talk on TDD, where did it all go wrong at the same time which was well received.

Leading on from unit testing we took a look at automated acceptance testing with TickSpec, and how this was used in the open source spreadsheet Cellz.

Finally for fun I showed a couple of FunScript samples including the Mario Tutorial (only working in IE at the moment) and a Pacman Sample. FunScript is an open source F# to JavaScript compiler with a Type Provider to TypeScript type definition files which provides typed access to common JavaScript libraries.

If you’re interested in picking up F# I’d recommend trying the F# Koans (where you can pick up the syntax while making a set of tests pass), the online Try F# site, and the F# Tutorial project in Visual Studio.

Hadoop Kickstarter

Next up in the same room Gary Short gave a great introduction to Hadoop and some of the related tools like Pig.

If you’re interested in trying out Hadoop with F# check out the Hadoop Type Provider online on the Try F# site which gives you typed access with intellisense:


WTFP?! Functional Programming and why you should care, with examples in F#

Before lunch Grant Crofton gave a great introduction to functional programming with F# using lots of live code snippets in Visual Studio


Grant also managed to slip some ASCII art into his scripts :)


Lightning talks

Lunch time saw a selection of lightning talks including Ben Nunney on Twillio and Anthony Steele on Feature Folders which emphasised the importance of focusing on the problem domain in your architecture.

The Joy of Wires: an introduction to Netduino

After lunch I popped over to see Iain Angus’s lively talk on Netduino which showed how much fun it can be playing with electronics.

You've learnt the basics of F#: What's next?

The last session of the day for me was Ian Russell’s fun talk which covered a selection of F# Type Providers for typed data access and F# Agents for taming concurrency.

Audience at Ian's talk at DDD North

Ian demonstrated the SQL, CSV, JSON and World Bank type providers all available either bundled with F# or via Nuget. For F# agents he showed a simple chat server that he ran from F# interactive using a local web server.

You can get the samples from Ian’s github repository: DDDNorth F# Presentation

Testing Crap in Web Applications Like ASP.Net MVC

The last pony of the day was spotted in Rob Ashton’s talk/rant which unfortunately I missed as it was on at the same time as Ian’s:


Hanging out at the close with my fellow F#ers, Ian and Grant:



A big thanks to the DDD North team especially Andy Westgarth for hosting a great event!

Categories: .Net | F# | C# | JavaScript
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed


October 6, 2013 10:13 by phil

The Elm programming language has some fun game samples including Mario. Elm’s syntax is quite similar to F#’s so it turned out quite easy to port the Mario sample and use FunScript to compile the F# code to JavaScript:

Mario FunScript

Use the cursor keys to move Mario.


With F# and FunScript you get to use Visual Studio (Windows) or Xamarin Studio (Mac), and get Intellisense over your code and JavaScript libraries via a Type Provider to TypeScript declaration files, which is nice.

The sample uses an F# record for state & function composition for Mario’s movement:

type mario = { x:float; y:float; vx:float; vy:float; dir:string }

let jump (_,y) m = if y > 0 && m.y = 0. then  { m with vy = 5. } else m
let gravity m = if m.y > 0. then { m with vy = m.vy - 0.1 } else m
let physics m = { m with x = m.x + m.vx; y = max 0. (m.y + m.vy) }
let walk (x,_) m = 
    { m with vx = float x 
             dir = if x < 0 then "left" elif x > 0 then "right" else m.dir }

let step dir mario = mario |> physics |> walk dir |> gravity |> jump dir

Alternatively the step function can be composed using the forward compose operator:

let step dir = physics >> walk dir >> gravity >> jump dir

FunScript is a lightweight open source F# to JavaScript compiler. It is still very much in development so if you want to take it out for a spin I’d recommend pulling the source directly from Zach’s Github repository (the current Nuget package is pretty old).


Another good option for F# to JavaScript compilation is WebSharper which is a mature, commercial offering from Intellifactory. I’ve ported the sample to WebSharper too:

The source code again is pretty close, with some minor differences in the way the JavaScript API’s are called. I’m pretty new to WebSharper, so found the slightly older WebSharper 2.4 the easiest version to get started with and adapted the HTML5 Canvas sample to get going.

WebSharper vs FunScript

Anton Tayanovskyy recently posted a WebSharper vs FunScript comparison.

To summarize:

  • FunScript aims to provide an easily extensible F# to JavaScript compiler for building client-side apps. The samples use a lightweight command line based web server built by Tomas Petricek so you don’t even need ASP.Net
  • WebSharper is a commercial enterprise scale web development platform with smooth integration with ASP.Net allowing seamless communication between client and server code

Which gives 2 good choices for creating web apps that target JavaScript with F# :)

For some more complete game implementations try:

  • Pacman (FunScript + HTML5 Canvas)
  • Pool (WebSharper + WebGL)

Categories: F# | JavaScript
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Pacman Kata at Progressive F# Tutorials NYC 2013

September 24, 2013 15:39 by phil

I’m a bit of a Pacman fan so when I came across the Pacman Kata in the Kata Catalogue I was hooked:

Pacman finds himself in a grid filled with monsters. Will he be able to eat all the dots on the board before the monsters eat him?

Back in early 2012 we ran a Pacman Kata at the F#unctional Londoners meetup where you start with a maze and sprites and your task is to write a simple AI. After the event I extended the sample to run on Windows 8, Windows Phone as well as Silverlight and WPF, and somehow got a mention from Channel 9.

A few months back Neil Danson ported the sample to iOS, and over the last few weeks I’ve been playing with the HTML 5 canvas and have created a FunScript version too.

When Rich Minerich suggested a version for the Progressive F# Tutorials in New York I jumped at the chance, and teamed up with Mathias Brandewinder to create a fun session centred around Pacman:


Code samples from the session:

Test-driven Pacman

The session began with some good old unit tests:

let ``pacman should wrap from left to right beyond left extent`` () =
    let p = wrap { x = leftExtent-1; y =0 }
    Assert.AreEqual(rightExtent, p.x)

Followed by automated acceptance tests using TickSpec:

Scenario: Pacman eats dots
  When pacman eats a pac-dot
  Then he scores 10

Scenario: Pacman eats power pellets
  When pacman eats a power pellet
  Then he scores 50

In the sample the scenarios are automated using attribute based step definitions:

type Property = PacDot | PowerPellet

let [<When>] ``pacman eats a pac-dot`` () =
    properties <- PacDot::properties

let [<When>] ``pacman eats a power pellet`` () =
    properties <- PowerPellet::properties

let [<Then>] ``he scores (.*)`` (points:int) =
    let scored =
        properties |> List.sumBy (function
            | PacDot -> 10
            | PowerPellet -> 50
    Assert.AreEqual(points, scored)


Next up we looked at implementing keyboard controls, first with Mario and then Pacman, both in FunScript:

Pacman keys 

The controls can be composed from simple functions:

let left (dx,_) p = if dx = –1 then {p with x=p.x+dx} else p
let right (dx,_) p = if dx = 1 then {p with x=p.x+dx} else p
let up (_,dy) p = if dy = -1 then {p with y=p.y+dy} else p
let down (_,dy) p = if dy = 1 then {p with y=p.y+dy} else p

let step dir pacman = pacman |> left dir |> right dir |> up dir |> down dir


The grand finale was writing your own AI for the Ghosts (or Pacman) using an API designed by Mathias specifically for the session:

// Decision is based on current move, line of sight and possible moves
member this.Decide (current: Move) (lineOfSight: Sight) (choices: Move Set) =
    n <- n + 1
    let inSight (creatures:Creature list) =
        |> List.exists (function PacMan 0 -> true | _ -> false)

    let standard  () =
        let restricted =                 
            |> Set.filter (fun c -> not (c = backwards current))
        if (restricted |> Set.count > 0)
        then randomMove restricted
        else randomMove choices

    let home () =
        let s = lineOfSight
        let xs = [Up,s.Up; Down,s.Down; Left,s.Left; Right,s.Right]
        let x = 
            |> List.tryFind (fun (d,xs) ->
                xs |> List.exists (snd >> inSight)
        match x with
        | Some (d,_) -> d
        | None -> standard ()

    if (n / 500) % 2 = 0 then home ()
    else standard ()

The simple AI above switches the ghosts between homing in on Pacman and random moves every 500 frame updates (roughly every 8 seconds).

At the end we played off Mathias’s payoff function based Pacman AI against my simple Ghosts on the big screen which ended in a tie :)

Blue ghosts

In the more competitive Pacman session ran by Paulmichael Blasucci and Rich Minerich the final was between Jack Fox and Tyler Smith.

More fun

And there’s more fun to come, Rich Minerich will be running a Pacman competition at the Progressive F# Tutorials in London this year:

ProgFsharp London 2013

and there’s a Pacman Kata scheduled for November at the F#unctional Londoners meetup.

Categories: F# | WPF | Silverlight | .Net | Mono | JavaScript
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed