Tahir Hassan's Blog

My Technical Notes

Tuesday, 19 August 2014

The "Null" monad in F#

In F#, we can use computation expressions to implement monads. Here is a definition of null' which is our monad:

Given a C# class definition of Student below, which contains an instance of Course which in turn has a property CourseId: string:

Here is how we would safely extract the CourseId from a given student:

Ignoring Visual Studio Packages within SVN Directory.

Nuget advices that the svn:ignore pattern for the packages directory should be:

That pattern is supposed to stop any file/folders with names which don't end in .config, but unfortunately allows names which are five or less characters in length. It also allows any file name where the sixth last character is c, or the fifth last character is o or ... the last character is g.

To get around this, we first need to outlaw any names which are six or less characters in length, and we need to outlaw any name which does not end specifically in config. The way we do this is by ensuring that the seventh last character is not a dot or the sixth last character is not a c or the fifth last character is not a o or ... or the last character is not a g.

Here is how it looks:

I have written a short F# script which generates this for a given file extension:

Calling allowExtensionOnly "config" will produce the above list.

Wednesday, 13 August 2014

ASP.NET: Getting File System Path and Resolving Virtual Paths

To get the directory of the base directory, we can call:

This is equivalent to:

To resolve a virtual path to an http path that the client can use, we do:

Monday, 11 August 2014

SQL Server: Getting First Word of a String

Below if a function which returns us the first word of a string (i.e. part of the string before a space character):

And we use it like so:

F#: Pattern matching on the Type of Input

Pattern matching based on the type of the input is readily done easily using a syntax called "Active Patterns". First we define our active pattern As:

Thereafter, to use it is results in very natural code:

Here is some code to test the above code:

I first posted this solution on StackOverflow.com.

F#: Converting Rows Back to a DataTable

Because the DataGridView does not display an array of DataRows, but can deal with a DataTable, the easiest course of action is to convert our array of DataRows into a DataTable and then pass it into the DataGridView. Below is code which does this:

F#: Showing Data in a Grid

Note that if you pass in a List or a Seq, then it won't work, instead pass in an Array.