F# or as some says F for Fun

2019-03-30 0 By Nordes

Welcome to the functional world. As someone who believe that we should learn everyday, I always look around for new technologies. Those technologies range from anything that could potentially touch software. It can be interacting with either something physical or simply an another program.

The language in itself

Maybe you know, maybe you don’t know. F# is the functional language from the Dotnet platform and has been developed quite a long time ago (see wiki). Functional is the rival to Object Oriented programming. In the MS stack, it is considered a first class citizen when it come to develop an application. While this language, in my opinion, is not used that often. If we are willing to have a mixed technology project, it could bring a lot of goods in the development. The domain could actually be driven by the architects and then the development made by the developers.

This language allow you to enforce your business rules at compile time. Since it’s under the Dotnet platform, it compiles in IL and can interop natively with the other languages such as C# or VB.

How I got to play around F#

I actually gave myself a challenge during the AdventOfCode 2018. That side actually create a story with puzzle games during the month of December. This allowed me to have a first impression on F#. I completed a bit more than the half of the problems. My two issues were that I had a lot of work and I had to read all the documentation online in order to understand how to use F#. Great resources can be found at https://fsharpforfunandprofit.com.

What are the advantages?

Strong typings

If you’ve written some code in ADA (which I doubt), you probably remember it’s a strongly typed language which does not allow you to do anything without typing. F# is no different, except that it allows you to have a bit less restriction on your types. For example you can easily redefine an int to something more meaningful (e.g.: PaperInTray: int). Which mean that you could talk about having a number associated to a tray.

let Printer = {
    Tray1: PaperInTray
    Tray2: PaperInTray
  }

This was a simple example. However, if you want to know more about the typings, I recommend you to read a book or the documentation found for F#.

There’s no Null

Basically, there’s no null within the language. Instead we have “option” in order to say if you have something or nothing (none). From there you can clean a collection having many none and keep only those with some data. This can also be used afterwards within the pattern matching.

If it compiles, it will most likely work as expected

Since it’s strongly typed, it will most likely works at first run. This is because it won’t compile if you mess around. If you miss some cases in your pattern matches, it will tell you.

Your records are automa-gically “equatable” and “comparable”

Yes, you’ve heard it, your records (a type of object which translate into either class or struct under the hood) allows you to do “=” or even some other operations.

If you don’t mess around, by default, what you write will be “Pure”

What is “pure” coding? An easy answer to that is that everything is immutable and that everything could work in a static world where you call over and over the same method in order to have over and over the same result.

By default, the language create everything immutable and only if you “really” want to, you can add the mutable keyword in order to have mutability of your values. (E.g.: let mutable myString = “abc” , this will allow you to do myString <- “hello” ).

Conclusion

I don’t want to enumerate everything here, because there’s great site already explaining what you can or cannot do (fsharp.org, fsharpforfunandprofit.com, and many others). But from a discussion I had with a colleague, it seems that C# is going slowly towards F# regarding a few points. In C# 8, just to name one feature, there’s the possibility to avoid having null going all around. Recently I saw that it is a nice language to even do your shell script in Linux (fsx) ;), how about you?

I hope you’ve enjoyed.