In the previous post, I displayed my fledgling understanding of F# by writing a script which can parse the CSV set of results of the English Premier League to generate the league table. The script does this primarily by using a mutable BCL Dictionary
. F# is immutable by default, and whilst that is itself not immutable, you have to go out of your way to enable it. I’ll try to save repeating Scott Wlashin.
There are some improvements that can be made to the script. I’ll highlight them here and then link to the full script as a gist.
First a note on pattern matching. In the previous post I mentioned that I thought I could use pattern matching in a particular place, and obviously I can:
Rather than if/then/else. Here, the “” is equal to the default
in a C# switch statement, if it’s not a Home or Away (win), then it _must be a draw.
Making things immutable
To start making things immutable, we can update the updateTeam
function from the previous post, and pass in a Map<string, LeagueRow>
:
The code is almost the same as the previous version, except that we no longer use the <-
operator to update the mutable dictionary. What’s going on instead is that F# creates a new instance of the LeagueRow
, with updated values, and adds that to the Map
, by key, which has the side-effect of creating a new instance of the whole Map
, with the league row identified by the key replaced with the updated version.
The updateHomeWin
function becomes:
This again replaces the BCL Dictionary with the Map, and simply passes the league map through each updateTeam
call, and then returns the updated league object.
processMatchResult
is also updated to pass in a Map
, and calling the fold with it and a default map is straightforward:
This makes the script much more ‘the way of things’ in F#, which is to say it’s using an immutable data structure.