I spent the last cou­ple of nights putting together a sim­ple Mark­down to PDF for­mat­ter using Tomas Pet­ricek’s FSharp.Formatting project and the PdfSharp-MigraDoc library.

To use this library, you can either grab the source from the GitHub repos­i­tory or get it from Nuget using the fol­low­ing command:

NuGet package

F# Usage

To use the library from F#, you can use the Mark­down type defined in the FSharp.Markdown name­space (from the FSharp.Formating library), once you’ve opened the FSharp.Markdown.Pdf name­space you’ll have access to two sta­tic exten­sion methods:

  • Trans­form­Pdf – accepts a string as input and out­puts the result­ing PDF to a local file path or to a spec­i­fied Stream.
  • WritePdf – accepts a Mark­down­Doc­u­ment gen­er­ated by the Markdown.Parse method, and out­puts the result­ing PDF to a local file path or to a spec­i­fied Stream.

C# Usage

You can also use this library from C#, how­ever, the sta­tic exten­sion meth­ods men­tioned above is not acces­si­ble in C# because type exten­sions defined in F# are com­piled dif­fer­ently to stan­dard exten­sion meth­ods you find in C# (with far more pos­si­bil­i­ties I might add, such as exten­sion prop­er­ties, and sta­tic meth­ods, for instance!) and is not rec­og­nized by the C# compiler..

You can still access the same func­tion­al­i­ties using a Mark­down­Pdf type, with two sta­tic methods:

  • Trans­form – equiv­a­lent to Markdown.TransformPdf.
  • Write – equiv­a­lent to Markdown.WritePdf.

 

You can see an exam­ple out­put here with the cor­re­spond­ing Mark­down here.

Share

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