Consumed: Handling commands (F#)

As I wrote earlier, I’ve been working on porting a node.js web application to an F# console application. It’s an application I wrote to learn node.js but still use today to keep track of all the things I consume. The application is able to consume an item, to remove a consumed item and to query all consumed items. In the previous post, I parsed command line arguments into typed commands and queries. Today, I’ll look at handling the two commands. ...

May 17, 2015 · 7 min · Jef Claes

Finding unused code (F#)

Coming from C#, I’m used to the compiler warning me about unused variables. Relying on the compiler to help me with checked exceptions in F#, I noticed that unused values (and functions) would go unnoticed. Having accidentally read earlier that Haskell has a compiler flag to check for unused bindings, I looked for the F# equivalent but failed to find it, until Scott Wlaschin pointed me in the right direction. By using the –warnon:1182 flag, the compiler will warn you about unused bindings. ...

April 26, 2015 · 2 min · Jef Claes

Consumed: Parsing command line arguments (F#)

Last year, I set out to write my first node.js application; a small web application for keeping lists of everything I consume. I had something working pretty quickly, deployed it to Heroku and still find myself using it today. Since there’s very little use for having it running on a server, and because I wanted something to toy with getting better at F#, I decided to port it to an F# console application. ...

April 19, 2015 · 4 min · Jef Claes

Checked errors in F#

In the land of C#, exceptions are king. By definition exceptions help us deal with “unexpected or exceptional situations that arise while a program is running”. In that regard, we’re often optimistic, overoptimistic. Most code bases treat errors as exceptional while they’re often commonplace. We are so confident about the likelyhood of things going wrong, we don’t even feel the need to communicate to consumers what might go wrong. If a consumer of a method wants to know what exceptions might be thrown, he needs to resort to reading the documentation (or source) and hope it’s up-to-date. ...

March 29, 2015 · 4 min · Jef Claes

Scaling promotion codes

In our system a backoffice user can issue a promotion code for users to redeem. Redeeming a promotion code, a user receives a discount on his next purchase or a free gift. A promotion code is only active for a limited amount of time, can only be redeemed a limited amount of times and can only be redeemed once per user. public bool TryRedeem(UserId userId) { if (HasAlreadyBeenRedeemedByUser(userId)) return false; if (NoLongerActive()) return false; if (Depleted()) return false; Apply(new PromotionCodeRedeemed(userId.Value)); return true; } In code these requirements translated into a promotion code aggregate which would guard three invariants. ...

March 15, 2015 · 3 min · Jef Claes

Domain Language: The Playthrough Bonus

Since online gambling has been regulated in Belgium, basically each eligible license holder has complemented their land based operations with an online counterpart. Being such a small country, everyone wants to secure their market share as soon as possible. The big players have been pouring tons of money in to marketing and advertising, it’s everywhere: radio, television, (online) newspapers, bus stops, billboards, sport events, airplane vouchers - you name it. While regulations for land based casinos are very strict and almost overprotective, regulations for online play are much more permissive. This makes that online casinos can be rather aggressive acquiring new customers. ...

February 23, 2015 · 5 min · Jef Claes

Averages are not good enough (F#)

Let’s (no pun intended) look at a set of response times of a web service. let responseTimes = [ 18.0; 21.0; 41.0; 42.0; 48.0; 50.0; 55.0; 90.0; ] People like having a single number to summarize a piece of data. The average is the most popular candidate. The average is calculated by dividing the sum of the input elements by the number of input elements. let average input = let length = input |> Seq.length match length with | 0 -> raise <| new ArgumentException("Input sequence is empty") | _ -> (input |> Seq.sum) / float length // Average = 45.625 // (18 + 21 + 41 + 42 + 48 + 50 + 55 + 90) / 8 The average is a measure of centre which is fragile to outliers; one or two odd irregular values might skew the outcome. The median on the other hand is always representative of the centre, not just when the data distribution is symmetric. The median is determined by sorting the input elements and picking the one in the middle. ...

January 18, 2015 · 4 min · Jef Claes

Spinning the wheel: clustering and near misses

The previous post showed a simple model casinos use to manipulate the odds. Instead of relying on the physical wheel for randomness, they rely on a virtual list of indexes that maps to the physical wheel. Using that same model, it’s easy to fiddle with the virtual indexes so that they map to misses right next to the winning pocket, creating “near misses”. “Near misses” make players feel less like losing, since you “almost won”. Casinos use this technique to get the next spin out of you. ...

December 14, 2014 · 2 min · Jef Claes

Spinning the wheel: manipulating the odds

The previous post defined a basic set of data structures and functions to spin a wheel of fortune in F#. There was very little mystery to that implementation though. The physical wheel had four pockets and spinning the wheel would land you a win one out of four spins. As a casino, it’s impossible to come up with an interesting payout using this model. To juice up the pot, casinos started adding more pockets to the wheel of fortune. This meant that the odds were lower, but the possible gain was higher. More pockets also allowed casinos to play with alternative payouts, such as multiple smaller pots instead of one big one. ...

December 11, 2014 · 2 min · Jef Claes

Spinning the wheel

In this post, I’ll define a basic set of data structures and functions to spin a wheel of fortune. In the next post, I’ll show you the simple model casinos use to build a bigger, more attractive pot, without touching the physical wheel and without losing money. Finally, I’ll show you how casinos tweak that model to bend the odds and create near misses. Let’s say we have a physical wheel with four pockets, which are labeled either miss or win. ...

December 9, 2014 · 2 min · Jef Claes