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

Don Pinto of Couch­Base gave a webi­nar today on some com­mon use cases of Couch­Base, with the record­ing and slides below:

Share

A cou­ple of weeks ear­lier, Ama­zon announced sup­port for Local Sec­ondary Indexes (LSI) for DynamoDB. You can now per­form fast, effi­cient queries against DynamoDB tables using attrib­utes that are not part of the exist­ing Hash and Range key model with­out resort­ing to the use of scans.

As a result to the new fea­ture the DynamoDB query API has also gone through some changes, as did the AWSSDK for .Net. From ver­sion 1.5.18.0 onwards, there’s a new top level name­space Amazon.DynamoDBv2 which con­tains a mir­ror set of types to those under the orig­i­nal Amazon.DynamoDB name­space, albeit with minor changes to sup­port the new LSI feature.

Query syn­tax change

Due to the changes in the under­ly­ing AWSSDK, I have decided to make some changes to the query syn­tax sup­ported by DynamoDB.SQL too – namely, to remove the need for the spe­cial key­words @HashKey and @RangeKey and instead allow you to use the attrib­utes names for your hash and range keys.

For exam­ple, given a table like the one out­lined in the DynamoDB docs:

image

To write a query to find all sub­jects start­ing with “a” in the “S3” forum, you would pre­vi­ously write:

SELECT * FROM Thread WHERE @HashKey = \”S3\” AND @RangeKey BEGINS WITH \”a\”

In the new ver­sion of DynamoDB.SQL, you would write the fol­low­ing instead:

SELECT * FROM Thread WHERE Forum­Name = \”S3\” AND Sub­ject BEGINS WITH \”a\”

This syn­tax change only applies to the exten­sion meth­ods for the Ama­zon­Dy­namoD­B­Client and DynamoD­B­Con­text types under the new Amazon.DynamoDBv2 name­space. The exten­sion meth­ods them­selves are only avail­able under a new name­space DynamoDbV2.SQL.Execution in the DynamoDb.SQL.dll.

The syn­tax for scans on the other hand, has remained the same in both the new and the old API.

Local Sec­ondary Index support

You can spec­ify that a query should use a Local Sec­ondary Index (LSI) by using the Index option in the WITH clause.

For exam­ple, given a Thread table and an index Last­PostIn­dex, as out­lined in the DynamoDB docs:

image

To find all the posts in the “S3” forum since the 1st May 2013, you can write the query as following:

SELECT * FROM Thread WHERE Forum­Name = \”S3\” AND Last­Post­Date­Time >= \”2013–05-01\”

WITH (Index(LastPostIndex, true))

The WITH clause is where you spec­ify optional query para­me­ters, such as NoCon­sis­ten­tRead, and Page­Size. (please refer to the Get­ting Started guide on avail­able query parameters).

The Index option allows you to spec­ify the name of the index, in this case that’s “Last­PostIn­dex”, and a boolean flag to spec­ify whether or not all attrib­utes should be returned.

For the above query, because we’re ask­ing for all attrib­utes to be sent back with *, and that attrib­utes such as Replies are not pro­jected into the index, they will be fetched (auto­mat­i­cally per­formed by DynamoDB) from the main table at addi­tional con­sumed capac­ity units.

 

On the other hand, if you want only the pro­jected attrib­utes back from the index, we can tweak the query slightly:

SELECT * FROM Thread WHERE Forum­Name = \”S3\” AND Last­Post­Date­Time >= \”2013–05-01\”

WITH (Index(LastPostIndex, false))

In which case, only Forum­Name, Last­Post­Date­Time and Sub­ject will be returned by the query.

 

Finally, if you are inter­ested in a spe­cific set of attrib­utes, you can also spec­ify them in the SELECT clause:

SELECT Forum­Name, Sub­ject FROM Thread

WHERE Forum­Name = \”S3\” AND Last­Post­Date­Time >= \“2013–05-01\”

WITH     (Index(LastPostIndex, false))

 

Some ref­er­ence links:

AWS announces Local Sec­ondary Index sup­port for DynamoDB

DynamoDB docs on Local Sec­ondary Indexes

DynamoDB docs on Query

Query­ing with an Index attribute in DynamoDB.SQL

Get­ting started with DynamoDB.SQL

Share

.Net 4 intro­duced the Lazy<T> type which allows you to cre­ate an object that can be lazily ini­tial­ized so that you can delay the cre­ation of large objects, for instance.

How­ever, if your ini­tial­iza­tion logic has the poten­tial to except at run­time (e.g. time out excep­tions read­ing from some exter­nal data source) then you should pay close atten­tion to which con­struc­tor you use to cre­ate a new instance of the Lazy<T> type. Depend­ing on the selected LazyThread­Safe­t­y­Mode, excep­tions in the ini­tial­iza­tion code might be cached and rethrown on all sub­se­quent attempts to fetch the lazily ini­tial­ized value. Whilst this ensures that threads will always get the same result, hence remov­ing ambi­gu­ity, it does mean that you’ve got only one shot at ini­tial­iz­ing that value…

 

LazyThread­Safe­t­y­Mode

In cases where you need to be able to tol­er­ate occa­sional ini­tial­iza­tion errors (e.g. read­ing a large object from S3 can fail from time to time for a num­ber of rea­sons) and be able to try again at a sec­ond attempt, the rule of thumb is to instan­ti­ate the Lazy<T> type by set­ting LazyThread­Safe­t­y­Mode to Pub­li­ca­tionOnly. In Pub­li­ca­tionOnly thread safety mode, mul­ti­ple threads can invoke the ini­tial­iza­tion logic but the first thread to com­plete the ini­tial­iza­tion suc­cess­fully sets the value of the Lazy<T> instance.

For exam­ple, the fol­low­ing only works under the Pub­li­ca­tionOnly mode:

 

F#

F# pro­vides a slightly nicer syn­tax for defin­ing a lazy com­pu­ta­tion:

image

the Control.Lazy<T> type is an abbre­vi­a­tion of the BCL Lazy<T> type with a Force exten­sion method which under the hood just calls Lazy<T>.Value.

Pre­sum­ably the above trans­lates roughly to the fol­low­ing C# code:

var x = 10;

var result = new Lazy<int>(() => x + 10);

and the thread safety mode using the Lazy(Func<T>) con­struc­tor is LazyThreadSafetyMode.ExecutionAndPublication which caches and rethrows any excep­tions caught in the ini­tial­iza­tion. E.g.:

image

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