In this post we will see how to use F# scripting to download the daily Bing wallpaper on Windows or Linux.
It is basically a simple application of the F# JSON Type Provider.
Complete source code from this post.
In this post we will see how to parse a Git log using F# and FParsec.
FParsec is a parser combinator library for F#. The library provides many simple parser functions that can be combined to create quite complex and powerful parsers.
For an introduction on how this works please refer to Functional Monadic Parsers ported to C# which explains some basic concepts and shows how a parser combinator library is built from scratch. Another good starting point is the FParsec tutorial or this post by Mathias Brandewinder.
In this post, however, we will focus on the usage rather than on how it works.
Complete Gist for this post.
It is integrated in this post and you can play it right here in the browser.
This sample was also published on Fable’s official sample site where you can find more information on the implementation details.
Therefore it can be extremely difficult to solve, even for computers.
A very promising approach to solving complex problems such as SameGame is the Nested Monte Carlo Search (NMCS). It is a very simple variation from the family of Monte Carlo Search algorithms, especially suited for single player games.
In this post we will see a generic implementation of the NMCS in Java that can easily be adapted to different problem domains.
But before we come to that, let’s look at how the NMCS works.
Continue reading →
I don’t get it. Even in a statically typed language like C# you can change the domain model and your application will still compile as if nothing happened. In this post we will address this and see how F# can help with such pitfalls of C# enumerations.
The problem with enumerations is simply that when we add new cases especially in a large code base, it can be really hard to find and update all dependent source code which might lead to errors at runtime.
The compiler will not help to find all the places that have to be updated. A text-based search isn’t reliable.
Here is how things can go wrong.
Exceptions are bad.
Not only do we have to remember to catch them everywhere, they also provide a second implicit exit strategy for functions, similar to the
However, there is an alternative more explicit approach.
In this post we will go through an example of how to implement decent functional error handling in F# without using NULL or exceptions.
F# type providers are just awesome because they help to write very efficient and reliable code.
In this post I will show this by implementing a simple, but real-world-like scenario with some F# type providers.
Type providers provide the types, properties and methods to get access to external data sources of various kinds without having to write a lot of boiler-plate code. This makes coding very efficient.
Additionally they offer static types that represent external data and that the compiler will check at compile time. This makes coding very reliable.
So let’s look at the scenario that we are going to implement…
Continue reading →
Often we encounter algorithms with a certain structure that consist of individual steps that may vary for specific implementations.
To keep things clean and in order to reduce duplicate code we should refactor common parts out.
The object-oriented solution to this is the Template Method design pattern.
There are two great articles by Scott Wlaschin on how functional programming can be used for the domain design of real-world applications by implementing a Tic-Tac-Toe game. He demonstrates how business rules can be modeled with types, how data hiding, encapsulation, logging and capability-based security can be achieved with functional programming and more.
I liked that the data-centric domain model was concise, totally clear and very close to the natural notion of the game. Eventually it couldn’t meet high security standards since there were ways for malicious users of the API to manipulate the data.
The second function-centric implementation introduced the concept of capability-based security. The design smells of the previous version could be resolved. But I argue that the API of the second version is not as intuitive as the first one anymore. Technically it is also quite simple. However, the recursive structure doesn’t come totally natural to me. Also an indicator that the second version is more complex is that logging becomes trickier.
Continue reading →
I have been playing around with FParsec a little bit lately, inspired by the chapter 8 “Functional Parsers” of the book Programming in Haskell by Graham Hutton. FParsec is an F# parser combinator library for building quite powerful parsers by combining primitive parsers and functions provided by the FParsec library.
If you haven’t been exposed to the concept of functional monadic parsers then this can be a very different experience. I am still totally fascinated by the power and the simplicity of this concept. Here is a brief introduction in C#.
As an exercise and to learn the usage of FParsec I have been looking for code that could be refactored to FParsec.