Last year I started to go in deep of functional programming world. Indeed, for the majority of my time I build web stuff: web apps and APIs, mostly on the .NET stack and sometimes on Node.js stack.

So, since I started to face with functional, my desire was to apply functional programming on the web architectures. This article groups some information, frameworks and best practices regards web development in F#.

Since this article is only an introduction, I also suggest: Build web service using F# and ASP.NET Core

Why functional programming in web world ?

Why use functional approach in web applications?  Functional comes with some benefits.

First of all, the concise and declarative syntax may be useful to solve problems and implement complex problems. On opposite of a imperative language, it has a declarative style focused on results, in consequence, you write code that describes what you want.

F# also reduces accidental complexity. The accidental complexity is the complexity that we inflict upon ourselves through the tools that we use. On the other side, the essential complexity is the actual complexity which affects a particular problem. That software engineering theory is called No Silver bullet.

Finally, it has also an excellent integration with C# and .NET ecosystem.

Functional web frameworks

Let’s talk about some different functional web frameworks in F#.

Evergreen names are: SuaveIO and Giraffe.

Both of them can run on .NET Core, and both provides some minimal feature and support over HTTP protocol and they provide full control over the flow of the program because both of them act on HTTP using a low-level approach.

Saturn

On the other side, some times an high-level approach on F# stack is preferred. Krzysztof Cieslack has recently introduced Saturn, a new F# web framework which stands on top of Giraffe.

It implements MVC pattern which is absolutely more familiar in web development.

ASP.NET project

As said before, F# can work efficiently with the .NET ecosystem.

fsharp.org offers some template projects in order to build web application in F#. You can find them on the Online templates section of  Visual studio “New project” dialog.

In addition, we may also consider to mix C# and F# projects in a single solution. The C# projects may contain all the client-side stuff, plus the Razor views. On the other side, the F# project contains all the models, controller and logics of the web application. That approach could be useful in order to gradually introduce F# inside your development team.

What about client-side?

Fable brings F# on Javascript ecosystem.

It uses popular technologies under the hood like Babel and Webpack, therefore you don’t loose all capabilities and advanced features derived from them.

SAFE stack

Recently, I also heard about SAFE stack:

SAFE is a technology stack that brings together several technologies into a single, coherent stack for type-safe, flexible, web-enabled applications that are written wherever possible in F#, thus allowing you to reuse your existing skill set as much as possible.

You can listen a cool presentation of the stack on the Scott Hanselman podcast: F# and the functional SAFE Stack with Krzysztof Cieślak,

SAFE stack combines different technologies:

  • Suave.IO covers the web server functionalities;
  • Fable brings all the power of F# to the JavaScript ecosystem;
  • Elmish implements core abstractions that can be used to build Fable applications following the “model view update”;

You can find an concrete sample of the power of SAFE stack on github.

Final thoughts

In conclusion, the following article exposes the current situation of web development in F#. Since F# was born in an open ecosystem, also before of ASP.NET Core, it offers a lot of choices in terms of framework and tools. If you are interested to build web service using Giraffe, I suggest you the following article: Build web service using F# and ASP.NET Core

Cover photo: Kunsthaus Graz art museum

About Samuele Resca

Samuele Resca is an Microsoft MVP Visual Studio and Development Technologies, Software Engineer, specializing mainly on ASP.NET MVC and in general about everything that revolves around the web. Samuele was born in 1994, and works as a software developer @ YOOX NET-A-PORTER Group He loves the MVC frameworks, ASP.NET MVC, Javascript, Node.js and Typescript.