Why CompilationRepresentationAttribute is your friend

In this short post I show why CompilationRepresentationAttribute is your friend if you like the "union/struct plus module" way of organising code as seen in core language features like Option<'T> and the Option module.

Continue reading »

Optimising Civ 5 cities using genetic algorithms

I recently came across a paper from 2005 about using genetic algorithms to optimise cities in FreeCiv, a free game similar to Sid Meier's Civilization. Genetic algorithms are an interesting approach to complex problems. Their brute force, trial and error approach means that they are often less effective than other algorithms but for some scenarios they can be surprisingly effective.

The paper spurred me to publish this post as over the course of 2015 I sporadically worked on a similar side project which used genetic algorithms to assign citizens in Civilization 5. The 2005 paper does not include any actual algorithms and there does not appear to be any follow up, so in this post I will discuss the algorithm I arrived at and its results.

Continue reading »

Constructing lenses using quotations

Earlier this month I wrote a post describing lenses in F#. Lenses are a nice tool for reducing the amount of boilerplate code required when working with record types, most notably when performing updates. However, the lenses themselves contain a fair amount of boilerplate, with most taking the form:

static member Lens_ = 
    (fun source -> source.View),
    (fun view source -> { source with View = view; })

I was intrigued, then, when I saw Dominic Graefen's post about dynamic lenses using quotations and reflection. His post outlines a way to create a type of lens that requires essentially no boilerplate code. Dominic's approach provides direct update access to record properties without the need for Lens<'TSource, 'TView> lens properties:

let original = { View = "Original Value"; }
let updated = Lens.With <@ original.View @> "New Value"

In this post I will describe how the same approach of using quotations and reflection can be used to create lens properties.

Continue reading »

Stateful Akka.NET actors with F#

Akka.NET is the .Net implementation of Java's Akka framework, which allows developers to build "highly concurrent, distributed, and resilient message-driven applications" via an actor model. One of the many nice features of Akka.NET is that it comes with an F# API. However, there are some scenarios which the F# API does not specifically cater for, and one of those is stafeul actors.

In this post I will describe how to implement a simple stateful Akka.NET actor in a functional style without mutable variables.

Continue reading »

Lenses in F#

I recently came across the concept of lenses in functional programming - a useful tool that can significantly reduce the amount of boilerplate code required when working with record types. I first noticed them in suave's source code and, intrigued, did some further research which lead me to this paper by Albert Steckermeier which describes the concept in detail (using Haskell).

As my background is primarily with OO languages I find that I quite often stumble across concepts in functional programming that more experienced practitioners would take for granted. There did not seem to a huge amount of information on lenses out there, so I thought I would write a short post about lenses in F#.

Continue reading »