Rash thoughts about .NET, C#, F# and Dynamics NAV.


"Every solution will only lead to new problems."

Sunday, 22. January 2012


F# Legacy: Test implants

Filed under: F# — Steffen Forkmann at 11:51 Uhr

Legacy code is a problem in all languages, even F#. Zwinkerndes Smiley

The super awesome Ilker Cetinkaya published an excellent article about “test implants”. I really recommend to read his post before you read this one. It’s a really well written story called “The quest of the test”.

I destilled his nice little story to this small F# code:

We have a function a that is part of a third-party API and a function b which is part of our own legacy code base. The problem is: we want to check that b has called a. without changing b’s signature. Ilker suggested to use a “test implant”. Let’s try to do something similar with our small F# code. Since we are in the land of functional programming our idea is to implant another function into b. So let’s rename b into b’ and add the implant to the parameter list of b’.

Of course we restore b’s behaviour by utilizing b’:

As you can see, we didn’t change the signature nor the behaviour of b. This is a typical refactoring in F#. The program runs as before. Now we want to write our test:

I hope you can see the similarity between this and Ilkers solution. The only difference is that we don’t use new constructors here. I really like this since it brings you one step further to nice functional design. What do you think?

Tags: , , ,

Monday, 16. January 2012


Building FAKE scripts with Jenkins

Filed under: F#,FAKE - F# Make — Steffen Forkmann at 16:11 Uhr

Jenkins is an extendable open source continuous integration server. In this blog post I show you how you can build FAKE’s Calculator sample with Jenkins. If you are familiar with Jenkins or Hudson then this should be straight forward.

Install Jenkins

Go to http://jenkins-ci.org/ and download/install Jenkins. In addition install the following plugins:

Create a new Task

Create a new “free-style” task in Jenkins:

image

Use git as the Source Control Management tool and configure the repo as following:

image

Configure the build step to run FAKE from the command line:

image

And finally configure the post build actions:

image

Running the build

If everything is setup correctly, then you should be able to start the build from the project page:

image

The output should look similar to this:

image

Tags: , , ,

Wednesday, 19. October 2011


Articles about FSharpx

Filed under: F# — Steffen Forkmann at 14:49 Uhr

My last posts described some special monads in F#. The code is now part of a new project called FSharpx. FSharpx is a library for the .NET platform implementing general functional constructs on top of the F# core library. The idea is that we start merging all these nice little F# open source projects we have and put them under one umbrella.

The project is in an early phase, but it’s really starting to accumulate so much interesting stuff and the number developers who are contributing is rising.

Unfortunately the documentation is not that good at the moment and therefore I want to present a collection of articles which cover some of the related topics:

I also recommend you to checkout the test projects. You will find there a lot of interesting samples.

If you find more articles about FSharpx or related topics, then feel free to contact me. I will include them here and in the documentation of the project.

Tags: ,

Wednesday, 17. August 2011


Some special monads in F# – Part 5 of n – Application: Poker – Your chance to get rich!

Filed under: F#,Informatik — Steffen Forkmann at 8:52 Uhr

In the last part of this blog series I showed how we can utilize the DistributionMonad in order to solve the famous Monty Hall problem. This time I want to show you how we can use it to solve some basic Texas hold’em poker questions.

We start by defining a model for poker in F#:

As you can see we model cards as tuples of a rank and a suit. The complete deck is just a combination of all ranks with all suits.

Now we need some additional helpers for the DistributionMonad, which will allow us to draw cards from the deck. You can find further explanations about these helpers in the excellent paper by Martin Erwig and Steve Kollmansberger called "Functional Pearls: Probabilistic functional programming in Haskell". Like the DistributionMonad itself, these helpers are already in the FSharp.Monad project and you get the bits from nuget.org.

Now we are ready to perform our first query. Let’s see how we can compute the probability of drawing the Ace of Clubs and the Ace of Spades:

But we can easily go a step further:

If you keep this model in mind you might come up with a poker odds calculator on a smart phone. This might be your chance to get rich. 😉

Tags: ,

Tuesday, 16. August 2011


Some special monads in F# – Part 4 of n – Application: The Monty Hall problem

Filed under: F#,Informatik — Steffen Forkmann at 10:34 Uhr

In the last part of this blog series I showed a DistributionMonad, which allows to perform queries to probability scenarios. Today we will use this monad in order to solve the famous Monty Hall problem. This article is based on the excellent paper by Martin Erwig and Steve Kollmansberger called "Functional Pearls: Probabilistic functional programming in Haskell".

“Suppose you’re on a game show, and you’re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?”

[problem definition from Wikipedia]

Let’s start by modeling the first choice using the DistributionMonad:

As we can see the solution is 1/3 as we expected and if we don’t switch, we stay with these odds. But if we switch we might improve the probability for a car. Remember the host will always remove a Goat after your first pick. Let’s analyze the second pick if we switch:

Car behind First choice Temp. Outcome Host removes Switching to Outcome
Door 1 Door 1 Car Door 2 Door 3 Goat
Door 1 Door 1 Car Door 3 Door 2 Goat
Door 1 Door 2 Goat Door 3 Door 1 Car
Door 1 Door 3 Goat Door 2 Door 1 Car

If the Car is behind door 2 or door 3 the situation is symmetrical and therefor we can conclude, the following:

And if we run this, we get this nice solution:

Next time I will show how we can use the DistributionMonad to solve some basic poker scenarios.

Tags: ,

Monday, 15. August 2011


Some special monads in F# – Part 3 of n – DistributionMonad

Filed under: F#,Informatik — Steffen Forkmann at 18:41 Uhr

In part I of this blog series I showed a simple InfinityMonad, which allows to treat special calculations as infinity and in the second part I showed the UndoMonad, which defines an environment which allows to undo and redo state changes.

This and the following posts are based on a famous paper by Martin Erwig and Steve Kollmansberger called "Functional Pearls: Probabilistic functional programming in Haskell".

Let’s start by looking at a small scenario:

This simple query calculates the probability of the event, that an dice roll gives a value greater than 3 and an independent coin flip gives “Heads”. In order to do this the DistributionMonad enumerates all possibilities and calculates the joint probability using the following formula:

Probability

If we want the nice syntactic sugar we can easily define a computation expression builder:

In order to allow easier access to our monad, we define some helper functions and basic distributions for fair coins and dices:

In the next part of this blog series I will show how we can utilize the DistributionMonad in order to solve the famous Monty Hall problem.

Tags: ,

Some special monads in F# – Part 2 of n – UndoMonad

Filed under: F#,Informatik — Steffen Forkmann at 16:42 Uhr

In part I of this blog series I showed a simple InfinityMonad, which allows to treat special calculations as infinity. This time I want to demonstrate a ported version of the UndoMonad (see the Haskell version). This monad defines an environment which allows you to undo and redo state changes. You probably know this behavior from your favorite text editor.

Let’s start by using this monad in a small sample:

The definition of this monad is easy if we use the StateMonad, which is already implemented in the FSharp.Monad project (you get the bits from nuget.org). We only need to define the kind of state which should be passed through the monad.

Now we need some helpers, which allow to access and manipulate our history.

In the next part I’ll show a DistributionMonad, which allows to perform queries to probability scenarios.

Tags: ,

Some special monads in F# – Part 1 of n – InfinityMonad

Filed under: F#,Informatik — Steffen Forkmann at 15:04 Uhr

At the moment I am collecting some samples for the FSharp.Monad project (you get the bits from nuget.org) and I think I should describe some of these monads here, since they are not that common.

Today I will start with a very small monad which encapsulates infinity as a special value. I found the the idea to this specific monad at http://visualizationtools.net. Let’s start with the basic definition. Like every monad we need a container type and the two functions return and bind. Return will be used to get values into the monad and bind allows us to chain functions inside the monad.

The next thing is to create a computation expression builder which allows us to use the nice syntactic sugar in F#.

Now we can start using the monad. As a small sample we define a safe division function which treats all “division by zero” cases as infinity.

And we can use this division inside computation expressions.

Remark: You can easily define this InfinityMonad in terms of the common MaybeMonad. You can see this in the UnitTests of the FSharp.Monad project.

Next time I will show an UndoMonad which defines an environment which allows to undo and redo state changes.

Tags: ,

Friday, 25. February 2011


Machine.Fakes 0.1.0.0 released – Built with "FAKE – F# Make" 1.50.1.0

Filed under: F#,FAKE - F# Make,Tools — Steffen Forkmann at 14:09 Uhr

My friend Björn Rochel (@BjoernRochel) built a really cool generic model for using fakes and automocking on top of Machine.Specifications (or MSpec) called Machine.Fakes (or mfakes).

Today he released version 0.1.0.0 (get it from nuget.org) and I want to talk a bit about the build process. Björn and myself thought a tool called “Machine.Fakes” should of course be built with a tool called “FAKE – F# Make”. In order to do so I had to fix some stuff in Fake which resulted in the new Fake version 1.50.1.0.

Here are some things I fixed:

  • New task DeleteDirs allows to delete multiple directories.
  • New parameter for NuGet task which allows to specify dependencies.
  • Bundled with docu.exe compiled against .Net 4.0.
  • Fixed docu calls to run with full filenames.
  • Added targetplatform, target and log switches for ILMerge task.
  • Added Git.Information.getLastTag() which gets the last git tag by calling git describe.
  • Added Git.Information.getCurrentHash() which gets the last current sha1.
Machine.Fakes build setup

The build script for Machine.Fakes performs the following steps:

  1. It retrieves the version no. via GitHubs REST API
  2. It cleans all directories from old stuff
  3. It compiles the app and test projects
  4. It uses MSpec to test the application
  5. It merges StructureMap and StructureMap.AutoMocking into the app by using ILMerge
  6. It generates the XML documentation using the .NET 4.0 version of docu.exe
  7. It zips the app and the documentation files
  8. It builds and deploys a Nuget package with just Machine.Fakes
  9. It builds and deploys bundled Nuget packages in the following flavors:
    1. Machine.Fakes.FakeItEasy
    2. Machine.Fakes.RhinoMocks
    3. Machine.Fakes.Moq

Now you can start using this awesome project by calling:

install-package Machine.Fakes.{Flavor}

Tags: , , ,

Friday, 18. February 2011


FAKE 1.46.0.0 with MSpec and NuGet support released

Filed under: F#,FAKE - F# Make — Steffen Forkmann at 8:49 Uhr

After a very interesting talk by Alexander Groß at the ADC2011 about MSpec I started to play around with it. I really like the concepts and especially the Selenium support.

Today I released a new “Fake – F# Make” version with initial support for machine.specifications (MSpec) and fixed some NuGet problems incl. support for automatic push to the http://nuget.org/ feed.

MSpec sample

Using MSpec in Fake is pretty straight forward and nearly the same like the NUnit or xUnit task:

NuGet sample

The following sample is taken from the NaturalSpec build and creates a NuGet package. It takes the AccessKey from the build params and pushes the new package to nuget.org. There are still some missing features but you can use them by manually editing the .nuspec file.

The corresponding .nuspec file looks like this:

Important links
Tags: , ,