Learn F# For the .Net Developer

Functional Programming

I wanted to talk to you today about Functional Programming. For those of you who don't know me, I'm a C# developer. I work for a big fortune 500 company as an architect. The product that I work on has the back end written completely in C#. The front end uses normal web technologies such as MVC and AngularJS.

For all of my career I have focused on C#. When I was in college all of my classes were in Java but I have never had a job where I wrote Java code. Every job I have ever had has been writing C#. Lately as I have been doing research, I have read more and more about Functional Programming. As you know both Java and C# are object oriented programming languages. As I continued to hear more and more about Functional Programming this and Functional Programming that, I decided that I really want to learn more about it. Over the last several months I have really been digging into Functional Programming to find out more about it. I have really liked what I have been learning about Functional Programming so I wanted to share some of the things that I have been learning.

What is Functional Programming

I am going to stay very high level today. The first thing that I want to talk about is what is Functional Programming. If you do a Google Search for Functional Programming you will get the following result from WikiPedia:

In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

At first this definition isn't very helpful. However, once you begin to break it down, it starts to make a little more sense.

a style of building the structure and elements of computer programs

If you come from an Object Oriented programming language such as C# or Java, you are used to building programs with classes and objects. However, functional programming languages don't use classes/objects. Instead they build programs by composing functions together.

that treats computation as the evaluation of mathematical functions

One of the principals of functional programming is that every function should always return the same result given the same inputs. Just like in math, if you have a mathematical formula for x and y, given that you always provide the same x value, the y result should always be the same. The same is true for functional programming.

avoids changing-state

Functional Programming is very big on avoiding side effects. If you have a function that is called getPatient it should only get a single patient's data, it shouldn't also update that patient's information or have any other side effects.

avoids ... mutable data

Another big principle of Functional Programming is immutable data. If you come from a C# or Java background you are used to everything being mutable by default. This causes lots of problems once you get into a multi-threaded environment. Thread A can read the value of object X and then Thread B could update that same value but Thread A not know that it has been updated. Functional programming languages avoid this issue by making everything immutable by default.

Introduction to Functional Programming

I am a newbie when it come to Functional Programming. I have only been learning about it for the past few months. If you have been working with a Functional Language for a long time you can probably give me lots of good information and tell me all of the things that I am saying wrong. However the purpose of this series it to help others who are not familiar with Functional Programming. Hopefully as I continue on my journey with Functional Programming, I will be able to help others who are newer to it than I am.

Functional Programming Languages

When it comes to Functional Programming languages, there are a lot of functional programming languages available. The most popular language that I see is Scala. Some of the other popular languages are Haskell, Erlang, and Elm. However, as I mentioned earlier, I use C# in my daily job. Because of this, I decided to focus on F# for the Functional Programming language that I wanted to learn.

F#(fsharp)

F# runs on the .Net run-time environment which means that C# code can call F# code directly. It made more sense to me to learn a language that I could easily use in my day-to-day job so F# seemed like a good fit. If I wanted to use F# I wouldn't have to install any additional software on the servers and I could easily call it from my C# code. I'm sure that Scala is a GREAT language, but it just doesn't make sense for me because it would require me to install the JVM in order to use it at my job. If you are a Java developer and already using the JVM, I would recommend you looking into Scala rather than F#.

There are several different websites that I have been using to learn F#. The first one is the F# Software Foundation. F# is an open source programming language and the F# Software Foundation is the foundation that oversees the language. They have a lot of great guides on how to use F#.

One of the stigma's that F# has is that it is a specialized language, but that isn't true. F# is a general purpose programming language and for the most part, anything that you can do with C# you can do with F#. Somethings, such as desktop applications, maybe more difficult to create with F# but it is typically possible. Some of the things that F# is VERY good at is Data Science, Web Services, Micro Services, Rules Engines, etc.

Another website that came up over and over again when I was searching for a solution to an F# problem was https://www.fsharpforfunandprofit.com/. They have a lot of really good articles that cover almost all aspects of the F# programming language and I would really recommend that you check it out.

So far, all of the sites that I have mentioned are free to use. However, I can't go without mentioning https://www.PluralSight.com. By far, the sight that has helped me learn the most about F# is PluralSight. I am a member of PluralSight and I can't recommend it enough. All you have to do is search PluralSight for F# and you will get a lot of courses that come up. I have watched a lot of these courses, but I still have a Playlist just for F# courses on PluralSight that I want to watch.

Who is using F#?

One question that you may be asking is who all is using F#. By far, one of the most well known companies that is using F# is http://www.jet.com. Jet.com started to create a competitor to Amazon.com. Recently they were bought out by WalMart so that is pretty impressive. Their entire backend is written in F#. They have a great tech blog that you should search for where they discuss the technology that they use.

One of the first times that I was introduced to F# is because of Jet.com. I was listening to Shane Hanselman's podcast several months ago and he had Rachel Reese on as a guest. She works for Jet.com and was talking about how they use F#. Rachel said that a while back they wrote their pricing engine in both C# and in F# to compare the two programming languages.

Why use F#?

After they had the code running for a while they found that the F# code had the following benefits over C#:

Faster Development Time

Fewer Bugs

Happier Developers

I don't know about you, but that is a pretty impressive list of benefits simply by switching from language A to language B. As I read more and more case studies about companies that have switched to F#, these three points kept coming up over and over again. From a business perspective, it seems like F# really has the potential to save a lot of money and product happier employees.

I'm not advocating that you switch all of your C# code over to F#, but what I am recommending is that you should learn another programming language. As I have learned F#, it has had a big impact on my C# code. It has helped me to write better C# code and to look at problems in ways that I wouldn't have looked at them before. Also, it has helped me to learn new areas of the C# language that I knew existed but had never felt comfortable using before.

What's next?

In the next several videos I am going to walk you through how to setup your development environment to use F#. After than I am going to introduce you to a website that I am using to help me get started writing F# code.

Questions I Have For You

Are you currently using F# (or another Functional Programming Language)?