After a long Easter hol­i­day filled with late night cod­ing ses­sions I find myself wide awake at 2am… good job I’ve still got my plu­ral­sight sub­scrip­tion and a quick look at the Algo­rithms and Data Struc­tures course again at least gave me some­thing to do to relax the mind with some back-to-school style imple­men­ta­tion of com­mon sort­ing algo­rithms in F#:

Whilst not the most per­for­mant imple­men­ta­tions I’m sure, I hope at least it goes to show how eas­ily and con­cisely these sim­ple algo­rithms can be expressed in F#! Now back to that sleep thing…

Share

This is a F# imple­men­ta­tion of the fast algo­rithm to count the num­ber of inver­sions in an array, out­lined in the Algo­rithms : Design and Analy­sis Part 1 course on Cours­era.

Share

Here’s a sim­ple F# imple­men­ta­tion of the merge sort algo­rithm (using muta­ble arrays) out­lined in the Algo­rithms : Design and Analy­sis Part 1 course on Cours­era.

So refresh­ing to be writ­ing sim­ple sort­ing algo­rithms years after uni­ver­sity, still fun! Open-mouthed smile

Share

With the offi­cial release of .Net 4.5 and Visual Stu­dio 2012, I sus­pect many .Net devel­op­ers will be rush­ing to rewrite their data access or net­work lay­ers (amongst many many other things!) to take advan­tage of the new async-await (see the excel­lent 101 exam­ples here) lan­guage fea­ture in C#, which means you’ll likely be work­ing with the Task and Task<T> type an awful lot.

If you have F# code that needs to interop with C# that returns or awaits some task types then you’ve prob­a­bly already come across the Async.StartAsTask<T> and Async.AwaitTask<T> meth­ods for con­vert­ing between F#’s Async<T> and Task<T> types. Curi­ously, there are no equiv­a­lent meth­ods on the Async class for con­vert­ing between Async<unit> and Task types.

So, to fill in the gaps our­selves, here are two sim­ple func­tions to do just that:

 

Enjoy!

Share

In Erlang, we have the Super­vi­sor behav­iour which makes it very easy to pro­vide the means to mon­i­tor and restart a whole net­work of work­ers and other super­vi­sors based on some con­fig­ured strategy.

The Mail­box­Proces­sor (aka agents) in F# doesn’t come with the same higher-level abstrac­tions (such as Erlang’s gen_server, gen_fsm behav­iours) by default, but it’s very easy to mimic some of their capa­bil­i­ties by build­ing on top of the stan­dard F# agents.

Luca Bolognese’s light­weight LAgent frame­work is a per­fect exam­ple and even pro­vides inter­est­ing pos­si­bil­i­ties such as hot swap­ping of code (although still some way off of what’s pos­si­ble in Erlang in this regard).

Sim­i­larly, the fol­low­ing snip­pet shows how you can start an agent with super­vi­sion so that any escaped excep­tions (which will crash the agent) are trapped and restarts the agent:

Share