Assertfail

Choose your own adventure in F#

06 Jan 2019

When you start out on F# your first thought might be:

let square x = x * x

let sumOfSquares n =
   [1..n]
   |> List.map square
   |> List.sum

hey, that sort of looks like:

public static class Utils
{
   public static int SumOfSquares(int n)
   {
      return Enumerable.Range(1, n)
         .Select(i => i * i)
         .Sum();
   }
}

Examples from F# for fun and profit: Sum of squares.

At a first glance, F# looks sort like slightly different style of C#, where instead of using extension methods you use static methods. Turns out that that’s not all there is to F#.

Let’s start from the basics

There is also an excellent source of knowledge about functional programming called F# for fun and profit.

Overlap of C# and F# coding

There are some points to being happy in F#. For instance there is an intersection of styles for F# and C# where both languages are nice to use:

Prefer F# over C# in the case when …

Some constructs that are easy to use in F# need more work or make for weird C#.

Prefer C# over F# in the case when …

Some constructs that are easy to use in C# need more work or make for weird F#

There is also a certain mainstream appeal of c style languages, why you might choose to code in C# despite being fluent in F#. I’m not fluent enough in VB.net to know when to use that language.

So you have choosen F#?

Let’s say that you go start with a server or service (and worry about client or web code later).

Web framework overview

There is a smörgåsbord of web frameworks to choose from.

The question about web framework can also be phrased as, what kind of style do you like? Roughly there are five categories.

Function composition based : Suave, Giraffe

My personal favorite. I like being able to compose API out of function building blocks. It’s both abstract and low level at the same time. Might not be appropriate for larger teams, but fits well with microservices.

Giraffe is bundled with Newtonsoft.Json for serialization. Suave uses DataContractJsonSerializer for serialization. Both of the libraries make it very easy to plug in your own choice of Json framework.

Personally I prefer to plug in Fleece with FSharp.Data.

Bring your own abstractions: IApplicationBuilder

Another one of my favorites. You can create your own abstractions on top of Map, MapWhen and Run. You can also choose to be more to the metal in order to avoid abstractions. This can be excellent in order to teach programmers about how the details of web services (or when there is little need of abstractions).

Webmachine based : Freya

Focus here is on being true to the HTTP spec. The webmachine diagram is used by people to teach themselves about HTTP.

The Freya tutorial points you in the direction of Chiron (from the same github organization).

CE as builder[s] : Frank, Saturn

Frank is a kind of small library that is probably good enough for many use cases. Saturn strives to be a batteries included framework.

Object oriented : ASP.NET MVC

Even though you need some attributes to configure endpoints, you can get a lot of mileage from TaskBuilder.fs and the fact that you need less code to take in dependency injection parameters. F# shows that it can be a nice object oriented language.

The default for asp.net mvc is to use Newtonsoft.Json. It’s not hard to plug in another serializer.

What about testing your code?

Main branches

There are two main paths of F# testing (that I’ve seen). One is the *Unit style testing frameworks, the other is composable testing frameworks.

*Unit

If you go down this path you should check out FsUnit that provides some extensions to make testing in F# more enjoyable.

XUnit will feel very familiar due to the popularity of *Unit style libraries.

Composable testing

I’m somewhat ambivalent to XUnit vs composable testing.

Going further down the test track

Property based testing

In property based testing you define the conditions for test data and let generators create that data to check your code against. These style of testing frameworks are excellent to combine with your existing testing.

When it comes to then add property based testing I’ve mostly used FsCheck.

Going deeper down the rabbit hole!

Say that you have gotten your feet wet and tasted some of the functional goodness of F#. Going further down you want to start to use some library to remove some of the copy paste. There are a couple of different libraries that can help you reduce your own infrastructure code.

F#X Extras

Contains a lot of helpful various utilities. Note monoid as an abstract class. It’s under the stewardship of fsprojects so accepts pull requests even though the original authors are not as involved.

ExtCore

ExtCore contains among other things combinations of computation expressions.

YoLo

YoLo isn’t really a library, it’s really a small file that gives you a bit of extra useful methods. Used by Suave to provide some common utilities for base components of F# like Option, Result and Choice.

F#+

My personal favorite. It’s a rejection of the assumption that F# cannot express higher level of abstractions. By using for instance monad transformers you can get the same functionality (combinations of computation expressions) as in ExtCore but without having to manually code it.

Conclusion

F# has a rich eco system of tools and frameworks. It can be a useful tool in order to deal with complexity or just to write something quick using some F# scripts. There are many paths that you can take to get where you need to go.

Tags


Comments

Do you want to send a comment or give me a hint about any issues with a blog post: Open up an issue on GitHub.

Do you want to fix an error or add a comment published on the blog? You can do a fork of this post and do a pull request on github.