Up your game by stacking Applicatives in Scala

Monads are very useful in many different situations.

But they get a little unwieldy when we have different Monads nested inside each other.

In these cases Monad Transformers come to the rescue. They allow us to compose different Monads into one that shares the functionalities of all of them.

But sometimes we want to combine the behavior of Applicatives in the same way. This is especially useful when we have to combine independent tasks.

In this post we will see how to do this in Scala with the use of the Cats library.

But let’s first look at an example of Monad Transformers.

Continue reading →

Hands on Monoids in Scala – Applying categories to birdwatching

What do Monoids in Scala have to do with birdwatching?

Before I come to that I would like to mention that since I’m coming from F# and recently started with Scala, being able to use type classes in my code is new to me.

I find this really exciting which I’d like to share.

So this post is maybe not a complete cohesive tutorial an Monoids but I hope that it is a fun little teaser for looking into things like functional programming, Scala, Cats, Haskell, type classes or maybe even category theory.

Continue reading →

How to implement a full-text search on HTML documents with Microsoft SQL Server

This scenario is quite common:

We want to do a linguistic, full-text search on text data, stored in a database.

The only problem: the text data is HTML formatted.

Therefore a target text like <strong>f</strong>oo e.g. will not be matched by the pattern foo.

So how can we implement an “HTML insensitive” search with the help of the Microsoft SQL Server?

While searching for a solution I could not find a complete guide or working sample of how to do this even though it’s quite easy.

The trick is to create a full-text index using an HTML filter.

Here is a quick summary.

Continue reading →

How to parse a Git log with FParsec

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.

Continue reading →

SameGame with F# and Fable: Functional programming meets JavaScript

Fable is a compiler for F# to JavaScript.

It brings all the good parts of functional-first programming in F# to JavaScript development.

Even without a lot of knowledge of Node.js and the JavaScript ecosystem, it is fairly easy to get started with and use functional-first programming for client-side browser applications. Fable can also be used for client-server, Node, mobile or desktop applications.

After reading the very nice introductory tutorial Getting started with Fable and Webpack I was ready to make an implementation of SameGame.

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.

Continue reading →

A generic implementation of a Nested Monte Carlo Search for single player games

A 15\times 15 initial SameGame board has an upper bound of 1.97\times 10^{182} reachable board positions.

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 →

How F# can help with the pitfalls of C# enumerations

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.

What’s the problem?

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.

Continue reading →

Functional error handling in F# by example

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 goto statement.

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.

We will do this by extending the application from the last post and make it even more reliable and robust.

Continue reading →

Writing efficient and reliable code with F# Type Providers

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 →