My first month working with F# daily

Iuri L Machado
3 min readJul 26, 2016

--

Yesterday I completed my first month using F# in a daily basis. I might say that the “adventure” was somewhat refreshing and quite enjoyable. There are so many gems hidden inside the .NET world (in special inside the F# world) and I want to say to everyone that works with C#/VB that you should try, for at least one month. You will start seeing the power and expressiveness behind it.

The first thing that I want to tell you is that, by no means I’m an .NET expert. To be fair, I’m a .NET newbie that knows some F#. Not a lot but I’m comfortable enough to write not sucking code (this is due to my functional programming background).

So, as I said, there are so many gems inside .NET and F# is one of them. Why, you might ask? Well, let me tell you that F# has some features that are game winners when you talk about productivity, safety and good written code. Let me tell you about some of them:

  1. No circular dependencies. All your files are compiled in a pre-defined order, so you cannot use something from Foo module if they are not declared before. This looks odd but leads to understandable, well designed code in the long run, since circular dependencies are evil (and tends to lead to hours of trying to understand dependencies between objects).
  2. Immutable data as default. This is great when writing concurrent programs. Not needing to deal with threads, locks and mutexes makes everyone’s day more enjoyable.
  3. Strong Type System. Since F# is a ML family language — the same from OCaml, Haskell, Elm — the types are your friends. I find that declaring types as your domain and what actions can be done with your domain a great way to write good code. Strong typing has the advantage of letting the compiler do extra work for you by guaranteeing that your functions arguments and return values are consistent. This directly leads to: when the actions in your domain change (also know as business model’s rules) — and they often change — it will guarantee that your program will not compile until you solve all inconsistencies in your code. Quite neat!
  4. Type providers. This for me is a killer feature in F#. Imagine you reading a CSV, XML or JSON file. Instead of writing the model, F# data/type providers infer the data structure for you automatically, with types, and with Intellisense! All you need to do is to import the file using the Type provider functions are you are done with it. Now imagine extending this feature to REST APIs, databases, other languages’ code. Who needs ORMs when you have the power of F# Types and LINQ.
  5. Asynchronous semantics. When you use functional programming, most of the time you’ll be building your logic on top of Collections (Seq, Array…). In imperative languages, it’s hard to parallelize loops, because you are mutating data and the job rapidly becomes a nightmare. In F#, you can wire up your code inside an async{} block and change all your functions, like map to pmap. That’s it, you are done. Go home.

And there are more… but I won’t spoil all the fun of discovery. I’ll just write names and let you do the heavy lifting: pattern matching, active patterns, operator definition and overload (use with care), computation expressions and quotations (metaprogramming). Give a try on F#, there are many chances that you’ll fell in love with it.

--

--

Iuri L Machado

Distributed Systems guy. Interested in Machine Learning and Deep Learning.