A Markdown Based Adventure Engine – My First Playing Around With Elm

My recent little adventures in Elm have definitely been one of the most enjoyable programming experiences.

Elm is a purely functional programming language that compiles to JavaScript. It has static type checking, all values are immutable, there are no null references, and no runtime exceptions. Additionally Elm has a great performance due to the implementation of a virtual DOM. All apps written in Elm are structured by the same simple pattern called The Elm Architecure.
Continue reading →

A new Scala feature for making illegal states unrepresentable

Making illegal states unrepresentable means that we enforce invariants on the code that we write, and choose data types so that states that are invalid won’t show up in our programs. 1

By reducing the number of representable wrong states we also reduce the number of potential bugs in our program by a great deal, as well as the number of tests needed to check for invalid inputs and outputs.

If we can’t create an illegal argument of a given type, we don’t need test cases for this illegal state for any function that takes arguments of that type as inputs.

Continue reading →

12 Things You Should Know About Event Sourcing

Are you aware that storing and updating current state means loosing important data?

Event sourcing is a way to solve this problem. It is the technique of storing state transitions rather than updating the current state itself.

Event sourcing has some more benefits:

  • Complete audit-proof log for free
  • Complete history of every state change ever
  • No more mapping objects to tables
  • Distribution support
  • CQRS (Command Query Responsibility Segregation) support
  • Natural fit for domain-driven design and functional programming
  • Be prepared for unanticipated use cases in the future (for free)

State transitions are an important part of our problem space and should be modelled within our domain — Greg Young

When I first encountered the concept of event sourcing and CQRS and looked at some sample applications, I had the impression that it must be extremely difficult to implement. But later I found out that event sourcing is easier than I first thought, especially when it is expressed with functional programming.

Here are 12 things about event sourcing that should help you to get started today.

Continue reading →

What are Scala Type Classes?

What are Scala type classes, what kind of problem do they solve and how are they implemented?

In a nut shell, type classes provide polymorphism without using subtyping, but in a completely type safe way.

Type classes represent some common functionality that can be applied to values of many different types. Moreover, we don’t have to change existing types in order to extend them with the new functionality.

In this post I will describe 5 simple steps for encoding a type class in Scala in an idiomatic way.

Continue reading →

7 Most Convenient Ways To Create A Future Either Stack

In Scala Future[A] and Either[A, B] are very useful and commonly used types. Very often we want to combine them, but a Future[Either[A, B]] is kind of awkward to handle not only because we don’t want to have to call Await anywhere.

One way to deal with this is to stack the types into a combined data type EitherT defined in Cats that is much easier to handle.

Still it can be quite unwieldy to compose values of this new type with other values of different types.

To get nice composability (e.g. with for comprehensions) we have to wrap other values into the new type by lifting them up inside the monad stack.

Here are the most convenient ways that I found to do that.
Continue reading →

Purity in an impure language with the free monad – by example of a Tic-Tac-Toe backend with CQRS and event sourcing

This post is part of the F# Advent Calendar in English 2016. Please also checkout the other posts or the F# Advent Calendar 2016 eBook.

Pure code intermingled with impure code.

This is not a very good separation of concerns and has many other disadvantages.

Here is an example of how many programs look like:

alt

In Haskell e.g. this would not be possible. But how should we deal with this in an impure programming language that does not enforce side effects to be made explicit, like F# e.g.?

There are a few approaches that will be presented in this post, one of which is the free monad pattern.

We will also examine a proof of concept implementation of a Tic-Tac-Toe backend following the command query responsibility segregation pattern (CQRS) together with event sourcing (ES).

See how you can implement a program in F# that is entirely pure and free from any effects and side effects!
Continue reading →

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 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 →