Phillip Trelford's Array

POKE 36879,255

GameCraft NYC

gamecraftSkills Matter are excited to host the first ever New York GameCraft event on Friday 20th September at the DUMBO Loft! Following the phenomenal success and excitement around Dublin GameCraft, the event is now going global with a event also planned in London! GameCraft is a games jam event designed around building the gaming community. We aim to create events which allow game-makers to meet, share ideas, have fun, compete for prizes and most importantly make games. The events will be facilitated by Games experts (and enthusiasts!) Andrea Magnorsky and Phil Trelford.

 

This is a free event on the same week at the same venue as the Progressive F# Tutorials NYC, so why not come and join us for both. F# + MonoGame makes a pretty awesome combination, as does Lua + Love or Python + PyGame. Take a peek at the excellent GameCraft resources page for some great links to game engines and tools.

Check out the recent article on GameCraft in the Irish Times. I popped over to Dublin for the first event back in February 2012 and it was really well organized and great fun. I managed to knock up a simple game in under 6 hours. The London event on Saturday August 10th will be 12 hours which may be enough to create a releasable game. Currently my hope is to take a simple game concept to completion and release it on an app store on the same day!

Hope to see you there.

Turing drawings

A little while back I stumbled upon a fun generative art project by Maxime Chevalier called Turing drawings that produces 2D procedural art based on randomly generated Turing machines. The project’s code is in JavaScript using a HTML canvas which you can try online, just hit the random button to generate all sorts of weird and wonderful pieces. The project has inspired amongst other things a port to asm.js and a version with genetic recombination of URLs.

For fun I’ve created an F# script that generates Turing drawings that can be run online below via the embedded Cloud Tsunami IDE:


The Tsunami IDE also runs on the desktop and can be embedded into applications like Excel.

If you’re interested in learning more about generative art and trying similar ideas then why not pop down to Skills Matter on Thursday August 15th for a hands on coding session.

F# in the Enterprise

Last night I popped down to Southampton to talk about F# in the Enterprise at the Developer South Coast meetup started by John McLoughlin. The group has grown quite a bit since last time I was down in 2010, and the hall was packed with about 50 members. It’s a really friendly group with a lot of interesting activities going on including hack weekends covering everything from electronics all the way through to games programming.


John contacted me after interest in a deep dive session on F# from the group.

Testimonials

At the start I covered a selection of testimonials captured on the F# Foundation site which gives some idea of where people are applying F# and the kind of benefits they’re seeing. .Net Rocks is another good source featuring interviews with F# developers talking about their experiences on projects, most recently Yan Cui from GameSys and Kit Eason on F# in Insurance.

order of magnitude increase in productivity, GameSys

performance is 10× better than the C++ that it replaces, Aviva

Take a look for yourself: http://fsharp.org/testimonials

Testing

The F# community has a strong testing focus with plenty of posts and libraries on the subject. As F# is a .Net language it works with all the regular .Net testing frameworks including NUnit, xUnit and MsTest:

open NUnit.Framework

let [<Test>] ``2 + 2 should equal 4`` () =
    Assert.AreEqual(4, 2 + 2)

Or using the popular FsUnit library you can make your assertions fluent:

[<Test>]
let ``2 + 2 should equal 4``() =    
    2 + 2 |> should equal 4

Stephen Swensen’s Unquote library takes a novel approach:

let [<Test>] ``2 + 2 = 4`` () =
    test <@ 2 + 2 = 5 @>

If a test fails unquote can help you uncover what went wrong:

Test 'Tests.2 + 2 = 4' failed: 

2 + 2 = 5
4 = 5
false

FsCheck a library based on QuickCheck is another F# library worth a look. FsCheck lets you fuzz test your functions which can help find those bugs that are harder to uncover with hand written unit tests. FsCheck can also be called from C#:

FsCheck

Types

Dependency injection (DI) is commonly employed in enterprise C# code. DI can make code more testable by injecting dependencies as interfaces to a class’s constructor:

public class HolidayService
{
    private readonly IFlightBooking _flights;
    private readonly IVillaBooking _villas;

    public HolidayService(IFlightBooking flights, IVillaBooking villas)
    {
        _flights = flights;
        _villas = villas;
    }

    public bool CanBook(DateTime date)
    {
        return _flights.Available(date) && _villas.Available(date);
    }
}

This can be expressed in F# as a class type:

type HolidayService(flights:IFlightBooking,villas:IVillaBooking) =
    /// Returns can book status
    member this.CanBook(date:DateTime) =       
       flights.Available(date) && _villas.Available(date)

The F# code generates the same IL as the C# code and works with all the major .Net dependency injection frameworks. I’ve personally used both Castle Windsor and more recently AutoFac.

If you’re using interfaces or abstract classes to inject dependencies you can provide mock instances when you test. This can be useful if for example you have code that interacts with a database or uses the current time.

F# 3 works seamlessly with all the major .Net mocking libraries like Rhino Mocks and Moq. If you’re looking for something that takes advantage of F#’s type inference features you might want to try out my imaginatively named Foq library which has a similar API to Moq:

let [<Test>] ``on no flights available you can't book`` () =
    let flights = 
        Mock<IFlightBooking>
            .Method(fun x -> <@ x.Available @>).Returns(false)
    let service = HolidayService(flights, mock())
    Assert.False(service.CanBook(any()))

That said it’s pretty easy to create an instance of an interface using F# built-in object expressions, so for many tasks a library is not needed:

{ new IDisposable with member __.Dispose() = printf "Disposed" }

And if you have interfaces with a single method you may just as well pass a function:

type HolidayService(hasFlight:DateTime->bool, hasVilla:DateTime->bool) =
    /// Returns can book status
    member this.CanBook(date:DateTime) = hasFlight(date) && hasVilla(date)

Type Providers

Type Providers are a feature unique to F# introduced in F# 3, that enable easy typed access to diverse data like databases, web services and even programming languages. In the presentation I showed Tomas Petricek’s World Bank sample running in Zach Bray’s FunScript. FunScript gives typed access to JavaScript libraries via a Type Provider to TypeScript and compiles F# code to JavaScript. The 80 line sample uses jQuery and HighCharts to show data pulled from the World Bank:

FunScript

For more example’s check out Don’s 12 Type Providers in pictures post.

Twitter

Thanks for all the great feedback on Twitter (here's just a few):

Tutorials

If you’re interested in learning F# I’d highly recommend the F# Koans and the online tutorials on the Try F# site:

image

Also check out the Progressive F# Tutorials in New York this September and London in late October:

Progressive FSharp Tutorials