Author Archives: purpleblob

Tuples, tuples and more tuples

Tuples are used a lot in F#. Let’s start with a definition

A tuple is a grouping of unnamed but ordered values, possibly of different type

This was take from Tuples (F#) and I’d highly recommend reading this. I will undoubtedly recreate some of the information from this document here, but hopefully add something of use.

Declaring tuples

So we’ve got a definition (above) for what a tuple is, now let’s have a look at some

let tupleInts = (1, 2, 3, 4)
let tupleStrings = ("one", "two", "three")
let tupleValues = (a, b, c, d)
let tupleMixed = ("one", 2, c)
let tupleExpressions = (a + 1, b + 2, c + 3)

The above should be pretty self explanatory.

If we take the first tuple, name tupleInts, and run this in the F# interactive window we’ll see that this translates to val tupleInts : int * int * int * int = (1, 2, 3, 4). The * denoting a tuple. In this case you can see that each item/element in the tuple is of type int.

Getting values from a tuple

Using let bindings

let (a, b) = (1, 2)

Using let bindings but ignore one or more elements (i.e. using the _ wildcard character)

let (a, b, _) = (1, 2, 3)

// or

let a, b, _ = (1, 2, 3)

Using pattern matching

let tuple = (1, 2, 3)

let pattern t =
    match t with
    | (a, b, c) -> printfn "%d" a

pattern tuple

We can use the fst and snd functions which get the first or second elements from the tuple, i.e.

let a = fst (1, 2)
let b = snd (1, 2)

Obviously a will have the value 1 and bReturning multiple values

We can obviously pass multiple values into a function using a tuple, but we can also return a tuple and hence return multiple values as per

let inc a b = (a + 1, b + 1)

where the return is a tuple of the values (a + 1, b + 1)

Cross language

In C# (.NET 4.0) we have the Tuple generic class which we can create in one of two ways

Tuple<int, int> tuple = mew Tuple<int, int>(1, 2);

// or using the helper method

var tuple = Tuple.Create(1, 2);

F# uses the same Tuple class internally (we can confirm this by checking the IL using ILSpy or the likes) to represent it’s tuples. However, as seen, it’s more of a language feature in F#. Hence we have the syntactic sugar to allow us to create the tuple just using (item1, item2).

Obviously if we can create a Tuple in C# and the underlying representation of a tuple in F# is the same, we can pass data to and from each language using this mechanism. So for example if we had this rather contrived C# code

public static class Data
{
   public static Tuple<int, string> Get()
   {
      return Tuple.Create(123, "One Two Three");
   }

   public static void Set(Tuple<int, string> tuple)
   {
      Console.WriteLine(tuple.Item1 + " - " + tuple.Item2);
   }
}

and in F# we can interact with these methods as follows

let (num, value) = CSharpLib.Data.Get()
CSharpLib.Data.Set(321, "Three Two One");

Event and event handling in F#

Publishing events

Let’s look at the subject of event handling by starting with some code

type Application() =
    let state = new Event<_>()
    
    member this.State = state.Publish
    member this.Run() =
        state.Trigger(this, "Starting")
        state.Trigger(this, "Running")
        state.Trigger(this, "Finished")

So we’ve declared an Application object which has a private state value, of type Event<_>. Basically this is declaring a value as an Event, the <_> leaves it to the compiler to infer the usage, ofcourse you can explicitly state the type if you prefer.

An Event is an implementation of IEvent, which is itself derived from an IDelegateEvent and IObservable.

The IDelegateEvent has a special use when you mark properties with the attribute [<CLIEvent>]. The properties are now compatible with other languages which use CLI compatible events.

The IObservable allows an event to be composable using Reactive Extensions (Rx) or using F#’s subset of Rx – this means we can filter, merge, split and more on events.

Going back to the code sample, we declare a member property using this.State = state.Publish which basically binds to the Publish property which itself allows us to use the Add method to add event handlers. Had we wished to make this property CLI compliant we would write

[<CLIEvent>]
member this.State = state.Publish

The next piece of code in our example is a Run function and this simply triggers events in a way analogous to state changing during the function.

Let’s now look at some code for handling these “state” change events…

Subscribing to events

Assuming we’ve created an instance of the application object, as follows

let app = new Application()

we can now subscribe to events on the object by adding an event handler

app.State.Add(fun (sender, obj) -> printfn "%s" obj)

in this example we use the Add function to subscribe to the State event and in this example we supply an anonymous function which is called each time a trigger is fired on the Application object’s State event.

Now the Add function doesn’t have an equivalent remove, but as an alternative we could use the IDelegateEvent’s AddHandler and RemoveHandler as per the following

let handler = new Handler<_>(fun s o -> printfn "%s" (snd o))
app.State.AddHandler(handler)

// ...

app.State.RemoveHandler(handler)

We can also use the more functional style Event module methods to both add and filter (for example) to only handle certain messages, as per

app.State 
   |> Event.filter(fun (sender, obj) -> obj = "Running")
   |> Event.add(fun (sender, obj) -> printfn "%s" obj)

In this example we’re filtering the events to only handle the case when the message is “Running” and the add function simply subscribes to the resultant events and outputs the message.

Observable

We can also use the IObservable subscription based model for events using

app.State 
   |> Observable.map(fun (sender, obj) -> obj)
   |> Observable.filter(fun obj -> obj = "Running")
   |> Observable.add(fun obj -> printfn "%s" obj)

In this sample code we map the events to extract the message string (using the map function), then we filter it and finally we add our event handler.

And here’s an example using the subscribe function

app.State 
   |> Observable.map(fun (sender, obj) -> obj)
   |> Observable.filter(fun obj -> obj = "Running")
   |> Observable.subscribe(fun obj -> printfn "%s" obj)
   |> ignore

Units of Measure

F# has the concept of units of measure. In essence we can declare a unit of measure on a numerical type which associates the numeric with a specific “measurement type” of data. For example, we might define a value as one of the imperial unit of measure, such as lb. Such as

As you can see, declare a unit of measure we use the [<Measure>] attribute as follows

[<Measure>]
type lb

We can now use this unit on a type as follows

let weightInPounds = 100<lb>

The inferred type here is val weightInPounds : int = 100, so as you can see the unit of measure is tied to the type (in this case an int).

If we wish to add or subtract to/from the weightInPounds we might be tempted to write

let newWeight = weightInPounds - 2

But we’ll get the following error The type ‘int’ does not match the type ‘int‘. This is because the 2 is simply an int and we’d need convert to the same unit of measurements, as per

let newWeight = weightInPounds - 2<lb>

However using multiplication or division will work without the need for the unit of measure added, so the following is perfectly fine

let newWeight = weightInPounds * 2

Deriving units of measurement from existing units of measurement

We can also derived new units of measurement from existing ones. So assuming we have the following

[<Measure>]
type lb

[<Measure>]
type inch

We might create a psi (pounds per square inch) as follows

[<Measure>]
type psi = lb inch^2

We could also apply multiple units of measurement as part of the let binding, for example

let p = 12<lb inch^2>

Removing a unit of measure

In some cases we might want to remove the unit of measure from a value, for example maybe we have a method that takes an int. We will get a compile time error stating “The expression was expected to have type in but here has type int“. We can simply cast

The code below is trying to set the value i (which we’ve setup to be an int) with a unit of measure value (where weight is shown as an int).

let weight = 12<kg>

let i : int = int weight

Writing to the console

To output a value with a unit of measure, using printn, we don’t use the following

let weight = 12<kg>

printfn "%A" weight

Tail Recursion in F#

The Stack

In most languages, when a function is called, the function arguments and the return address of the calling function (possibly including other things like the frame pointer – which can be turned off in the likes of VC++) are pushed onto the stack. When the function call ends, these are popped from the stack and the function returns to the calling method.

The stack is finite, and per thread is by default 1MB in size (see Thread Stack Size). So obviously it’s possible to get to point where we get a StackOverflow exception. This is especially a problem with recursive functions.

Another issue with having deep stacks in .NET is when the garbage collector kicks in as it will have to traverse the entire stack each time garbage collection is required.

Tail Recursion/Tail Call

C# has no built in tail call mechanism, unlike F#. A tail call allows the .NET CLR to drop the current stack frame before executing the target function.

Let’s look at a really simple recursive function, which simply increments a value and outputs the result.

let rec increment i =
    if i < 100000 then 
        printfn "%d" i
        increment (i + 1)
        printfn "next"

The above will be terminated by a StackOverflowException very quickly. But if we remove the last printfn line to give

let rec increment i =
    if i < 100000 then 
        printfn "%d" i
        increment (i + 1)

we will find the function continues until completion.

So basically a tail call is where the recursive call is the last (or tail) function call. This doesn’t strictly mean the recursive call is the last line of the function, it’s that it’s the last called within the function. For example

let rec increment i =
    if i < 100000 then 
        printfn "%d" i
        increment (i + 1)
    else
        printfn "next"

shows that the recursive call is the last function call and only when the recursive calls are completed (and stack unwound) does the last line get executed. But beware, the recursive call should be the last call executed but if we alter the code slightly, to have it return an int we might have something like the following code

let rec increment i =
    if i < 100000 then 
        printfn "%d" i
        i + increment (i + 1)
    else
        printfn "%d" i
        0

This is contrived example, to force an int return (so don’t worry about what it actually does). Now the line i + increment (i + 1) is the last executed as the code goes recursive and it appears that increment (i + 1) is still the last line executed, however the inclusion of the i + code has mean’t this code is no longer tail recursive.

Further Reading

Tail call
Tail calls in F#

MongoDB notes

This post is meant as a catch all for those bits of information which do not need a full post (ofcourse this doesn’t mean I won’t create a full post at a later date should the need arise).

Atomicity

Whilst MongoDB guarantees atomicity of a write operation as a single document level there is no such guarantee on multiple documents.

So for example if one is creating a relational style model with related data in more than one document (i.e. not an embedded relational model) then writing to the multiple documents is NOT atomic.

Indexes

Be default an index is created for the _id of a document. To create our own we use the ensureIndex method on a collection.

The MongoDB documentation states that

  • Each index requires at least 8KB od data space
  • Whilst query performance on the indexed data is increased the write operation will see a negative performance impact – therefore indexes can be expensive if there’s a high write to read ratio on documents

Overriding Default functionality

As mongodb uses JavaScript we can use JavaScript code to override some default functionality.

Let’s say for example, we want to stop users accidentally dropping a database. We can override the dropDatabase function thus

DB.prototype.dropDatabase = function() {
   print("dropDatabase disabled");
}

db.dropDatabase = DB.prototype.dropDatabase;

Loading scripts from the shell

Typing in code, such as the above override of the dropDatabase function every time, would end up being time consuming. Being that this is JavaScript it may come as no surprise to find out that the mongo shell can load script files.

Using load(‘myscript.js’);

Loading scripts at startup

In the above example, we’ve created a script which we might need to load every time we start mongo. So it’d be better if we could just load the script file automatically at startup.

So to load scripts at startup on a Windows machine, place the script file into c:\users\\.mongorc.js

Note: It’s also possible that we might want to ignore scripts at startup when debugging, in such a case we start mongo using

mongo –norc

More to come…

Mutable values and closures

The real title of this post should be The mutable variable ‘count’ is used in an invalid way. Mutable variables may not be captured by closures. Consider eliminating this use of mutation or using a heap-allocated mutable reference cell via ‘ref’ and ‘!’ but I thought it was a little long for a title.

Setting the scene

I was writing a simple C# extension method for the System.String class which has the following method signature

public static void Split(this string s, char[] seperator, StringSplitOptions options, Action<string, int, int> action)

The idea being that instead of creating an array of strings for each delimited string, I would instead pass the original string to an Action delegate with the start of a delimited field and the length of the field. This would help save on potentially wasteful creation of memory when handling large amounts of data.

I decided it’d be cool to write the unit tests for this method using F# (to help increase my knowledge of F#) – review Unit testing in F# to see how I set up the tests etc.

So the intention was to create a simple unit test, which just checked the number of times the Action delegate was called, which should be the same number of times as the number of delimited words within the string. So here’s a sample of what I would have written in C#

[Fact]
public void ExpectTheSameNumberOfCallsToTheActionDelegateAsDelimitedWords()
{
   string test = "The, quick, brown, fox";

   int count = 0;
   test.Split(new []{','}, StringSplitOptions.None, (s, i, j) => count++);

   Assert.Equal(4, count);
}

So I implemented the following F# text, very much along the lines of the above

[<Fact>]
let ``Expect the same number of calls to the action delegate as delimited words`` () =
   let mutable count = 0
   "The, quick, brown, fox".Split([|','|], StringSplitOptions.None, 
            fun s a b -> count <- count + 1)
   count |> should equal 4

at this point Visual Studio shows a squiggly red line under the code fun s a b -> count <- count + 1 with the message The mutable variable ‘x’ is used in an invalid way. Mutable variables may not be captured by closures. Consider eliminating this use of mutation or using a heap-allocated mutable reference cell via ‘ref’ and ‘!’. What’s going on ?

Searching for answers

Credit where credit is due, I searched around and found the web and found the following StackOverflow question “The mutable variable ‘i’ is used in an invalid way.?” which describes a solution to this problem.

Basically you cannot use mutable values inside a closure and thus we need to change to a ref type. So the following code now works

[<Fact>]
let ``Expect the same number of calls to the action delegate as delimited words`` () =
   let count = ref 0
   "The, quick, brown, fox".Split([|','|], StringSplitOptions.None, 
                fun s a b -> count := !count + 1)
   !count |> should equal 4

The changes here are that the mutable value count is now a ref type, the code to increment the count changes to use the ref assignment operator := and we also use the reference cell dereferencing operator !. The last line also uses the same. Alternatively we could have written fun s a b -> count := count.Value + 1 and the last line as count.Value |> should equal 4.

So why can’t we use the mutable type within a closure, after all it works fine in C#. The StackOverflow question (listed above) has a telling comment. Mutable values are stored on the stack not the heap. Therefore when the anonymous function exits, values stored on the stack are discarded during the stack unwind.

So how can C# handle this code ?

In C# we can do this because the Common Language Runtime (CLR) automatically places variables on the heap if they’re used as part of a closure as detailed in the post Capturing Mutables in F#.

References

The following are some references I located either whilst trying to solve this problem or as part of my research into the reasons for this after fixing the code.

The mutable variable ‘i’ is used in an invalid way.?
Mutable variables cannot be captured by closures” is giving me a bad day
Capturing Mutables in F#
The Truth About Value Types

Beware an exception in a ReactiveCommand

The Reactive UI class ReactiveCommand can be used to implement a view model’s commands as per the following:

Synchronize = ReactiveCommand.Create(_ => true, DoSynchronize);

This is excellent and works like RelayCommand and DelegateCommand from other MVVM frameworks, in that the first argument of the Create method handles the CanExecute and the second is the Action to be executed when ICommand.Execute is called.

One problem this usage has, is in the case of an exception within the command’s action. As Reactive UI uses Reactive Extensions it also handles errors in the same way.

When an error occurs in Reactive Extensions the default behaviour after an OnError is that subscribers are unsubscibed, in other words no further calls are made to your subscribed Action.

In the case of ReactiveUI’s ReactiveCommand this means if an exception occurs during the Action the command essentially disconnects itself and further commands are ignored. So a simpler way of solving this is to use ReactiveAsyncCommand.

Synchronize new ReactiveAsyncCommand(null);
Synchronize.RegisterAsyncAction(_ => DoSynchronize());

This way a new IObservable is created each time a command is executed, thus an exception will simply stop the current IObservable and the next time the command is called a new one is created.

Caliburn Micro’s message bus (event aggregator)

Most MVVM frameworks come with some form of message bus. A means to send messages from one class to any classes wishing to listen for such messages. Ofcourse in a MVVM world those classes might be view models also.

In Caliburn Micro this message bus implementation is known as the event aggregator which is defined by the IEventAgreggator interface.

A class wishing to subscribe to events simple get passed an IEventAggregator as calls the subscribe method as follows

public MyViewModel(IEventAggregator eventAggregator)
{
   eventAggregator.Subscribe(this);
}

In this example we’re using the constructor and in this case, I’m using the Ninject bootstrapper which we looked at in a previous post, to inject the IEventAggregator into the constructor.

Obviously we need to add some code to actually handle any events as this solely subscribes to the aggregator, so for the sake of argument we’ll assume we have an event class which looks like the following

public class MyMessageEvent
{
   public MyMessageEvent(string message)
   {
      Message = message;
   }

   public string Message { get; provate set; }
}

And now on our view model we implement IHandle

public class MyViewModel : PropertyChangedBase, IHandle<MyMessageEvent>
{
   public void Handle(MyMessageEvent message)
   {
      // do something with the event
   }
}

Now we need something to publish events…

If we create another view model and again pass the IEventAggregator into the constructor as per

public class MyViewModel : PropertyChangedBase
{
   private IEventAggregator eventAggregator;

   public MyViewModel(IEventAggregator eventAggregator)
   {
      this.eventAggregator = eventAggregator;
   }

   public void PublishMessage()
   {
      event.Publish(new MyMessageEvent("Hello"));
   }
}

List basics in F#

In a previous post I touched on F# lists, now let’s look a little further into the F# lists.

Note: Also check out Lists (F#) for a far more comprehensive view of F# lists

The MSDN documentation states that F# lists are implemented as singly linked lists.

Creating Lists

First off we can create a list using square brackets and semi colon delimiters, for example

let list = ["One"; "Two"; "Three"]

This creates a string list using type inference, i.e.

val list : string list = [“One”; “Two”; “Three”]

Had we wanted we could also create a list with a type hint mirroring this line (note the string list type hint)

let list : string list = ["One"; "Two"; "Three"]

We can also define this list without the semi-colon delimiter, instead using newlines as per

let list = [
   "One"
   "Two"
   "Three"]

We can declare an empty list as follows

let list = []

We can also declare lists using the range operator, for example

let list = ['a' .. 'z']

Whilst we can define a list of data as above, we can also generate the list in the following way

let list = [for i in 'A' .. 'Z' -> i]

Operators

The double colon :: operator takes a left hand argument as a single item and the right hand side as a list and creates a new list which is made up of the left hand argument and then right hand list appended to it. So, for example

let original = [1; 2; 3]
let list = 0 :: original

will result in list being [0; 1; 2; 3]

We can also concatenate lists using the @ operator, for example

let right = [1; 2; 3]
let left = [3; 2; 1]

let combined = left @ right

This results in a new list which looks like [3; 2; 1; 1; 2; 3]

Operator overloading in F#

F# allows us to override existing operators and even create our own. There are two types of operators, prefix and infix.

Prefix operators are those which take one operand. Infix operators take two arguments.Unlike C# both operands in an infix operator must be of the same type.

Along with overloading existing operators we can create custom operators using a single or multiple characters from the following !$%&+-,/<=>?@^|~

For example, let’s define a global operator !^ which will calculate the square root of a floating point number

let inline (!^) (a: float) = Math.Sqrt(a)

and in usage

let sqrt = !^ 36.0
printfn "Sqrt %f" sqrt

An infix operator is declared taking two arguments, so here’s an example of a value to the power n, which we’ll use the operator ++ which can be defined as

let inline (++) (x: float) (y: float) = Math.Pow(x, y)

and in usage

let a = 10.0 ++ 2.0

When declaring operators on a type (as opposed to globally) we have the following

type MyClass2(x : int) =
    member this.value = x
    static member (~+) (m : MyClass2) = 
        MyClass2(m.value + 1)
    static member (+) (m : MyClass2, v : int) = 
        MyClass2(m.value + v)

// usage 

let m = new MyClass2(1)
let n = m + 5