Modeling historical dates

As a part of a side project I continually fail to actually start I would need the ability to sort events based on a date. Sounds simple, right? And so it would be if all of the dates were CE (or AD if you prefer) or if .Net's DateTime type supported BCE dates. Unfortunately these dates could be BCE or CE and System.DateTime's minimum value is 1st Jan 1 CE. Further complicating the matter is the fact that these dates come from historical sources which can be extremely vague (e.g. "before January 17 BCE").

In this post I will look at modeling historical dates using F#. I'll look at creating a model that captures the vagueness of historical dates and a method for sorting them which is further complicated by the surprisingly chequered history of leap years.

Continue reading »

WebAPI OData GraphDelta

In the course of a recent project I ran into a problem with the standard Delta<T> included in Microsoft WebAPI's OData functionality: it treats complex properties as atomic values meaning that instead of applying the delta to the existing value of the complex property it will create a new, blank value, apply the delta to that and then set the complex property to the result.

I asked about this behaviour on StackOverflow and it turns out that it's a known issue. This post briefly outlines how I implemented an alternative to Delta<T> that handled changes to complex types the same way as changes are handled for entities. The post will feature minimal code but source code is available on GitHub.

Continue reading »

Using DBSCAN for fun and (mostly) profit in Elite:Dangerous

Elite:Dangerous is the reboot of the classic 1984 space trading, combat and exploration game Elite and it has been out for a few months now. After some initial aimless wandering I, like a lot of people, settled on a career trading rare goods. These goods, as their name suggests, are rare and available in limited quantities and can bring in huge profits if sold sufficiently far away from their system of origin.

In order to maximise profits it is desirable to circulate as much as possible between systems that supply rare goods, buying and selling at each stop. One popular strategy is to follow a circular route, periodically selling and buying. Another, and the one I favour, is to ferry cargo between clusters of systems that supply rare goods - zipping around one cluster buying goods and then travelling to another to sell before repeating the process.

The Elite:Dangerous community has done a good job of exploring the game's trade network and documenting what rare goods are available where, but it can be hard to visualise the systems relative to each other, even using the in-game galaxy map. So I thought it would be interesting to see if I could take that community generated data and group the systems together into clusters programmatically.

This post details how I used an immutable DBSCAN implementation to identify rare goods clusters in Elite:Dangerous.

Continue reading »

Futures in F#

My first exposure to the concept of futures (or promises) was probably JavaScript's Q API and AngularJS' $q service, but it was only after reading Twitter's "Your Server as a Function" paper that their usefulness really sank in. The closest thing that .Net has to the Scala futures illustrated in the paper are Task<T> and F#'s Async<'T>. Although these types are useful they do not provide all of the functionality of Scala's Future[T], nor are they as elegant.

In this post I will describe an approach to creating something akin to Scala's Future[T] in F#.

Continue reading »

Testing Web API OData controllers

Microsoft's Entity Framework is an occasionally handy ORM framework that allows you to query database entities using LINQ and to easily add, update and delete entities via a variety of helper classes.

Recently I wrote a set of WebAPI controllers to expose an Entity Framework model as OData endpoints. Exposing an Entity Framework model as OData using WebAPI is incredibly easy. However, when it came to testing my controllers I found that mocking Entity Framework is tricky, due to a combination of erratic use of interfaces and the apparent complexity of IQueryable<T>.

Luckily my controllers only used a few parts of the EF API, so I decided to write some lightweight wrappers to make testing easier. This had the bonus of also removing dependencies on EF from my controllers.

Continue reading »