#begin
During the past holidays, when most of us had some time off, me and some other colleagues continued working. Amidst the covid rampage there is not much of a vacation to celebrate anyway… Somewhere between Christmas and new year we needed to do some maintenance on our servers and this was delegated to our awesome devops team. This meant, we could not really make progress since we were unable to make changes to our repositories.
Fortunately, Dephion had just singed the entire team up for Udemy for business which meant we could access any course within predefined categories we wanted. There is so much stuff on Udemy, I think anyone will find a plethora of courses they find interesting within the vast sea of courses. So, we could, and still can use Udemy as much and whenever we want. Which is awesome!
So.., I would like to start a new series blog regarding any course I follow on Udemy. During the holidays I finished one course and already started on my second. This blog will describe some of my experiences with Udemy for business, and the course material itself of course.
Udemy for business
So first of all let’s discuss the concept of Udemy for business. I think this is great. It is a really nice incentive of the company to allow anyone on the team, unlimited access to any course on udemy within certain boundaries. Luckily, the boundaries we work in are kind of fuzzy since there are many different departments in Dephion I (we) have access to programming, game, web development, software, architecture, infrastructure, health, fitness, mindfulness, personal development, film + photography, audio and more. There are no excuses for anyone, not to find anything interesting.
On the other hand, I’ve always leaned more towards a more traditional way of learning. I like reading books and research papers and then try to make/program something based on what I have learned. I’ve never really enjoyed watching video tutorials/lectures since it is essentially just copy pasting whatever the author is doing. Now, I don’t mean this is invaluable but it is just not my preferred way of learning. What is particularly useful about video tutorials though, is the fact that you gain some more insight in the thought pattern the author has while he is creating something. This can be really valuable since you can learn a lot from that.
One thing I dislike about Udemy is general, or better to say, could see some improvement are all the deprecated, really old, courses that you can still find there. This is purely a personal comment since some people will definitely like to follow courses on old material, maybe they work in a legacy system trying to grasp how everything works, some old courses will definitely help. But if I for example search for tutorials in Unity3D, I still find courses that use Unity3D 5.x.x which was around the 2014 – 2015 time-frame. I think any sensible company that uses Unity3D will be on much newer versions of Unity3D by now. Many things have changed in the Unity3D space over the past 6 years. And some systems that were in 5.0 have seen a major overhaul ever since, like WebGL. So, how relevant are those courses using deprecated and obsolete systems or API’s? I’m not sure.
But in general I think this is a great idea. I’m looking forward to following some course with the entire team sometime and discussing everyones point of view on the course material and things we have learned. We will probably have some nice discussions come out of that.
F# from the ground up
ha! Do you see what I did with the title there!? No but seriously… the first course I followed on Udemy was a course on F# called F# from the ground up by Kit Eason. F# has been this language I’ve had some interest in for a while but never got to a point ever really writing a program. Thus I thought let’s see if there are nice courses on F# on Udemy, and of course there are. So I started this 5.5 hour course on F# and I have to say it was really informative. Although I was quickly reminded of how slow “video tutorials” actually are. Why I actually still prefer good old “physical” books instead of video’s. Nonetheless I really enjoyed the course, gained some great insight in the language and learned the basic syntax. F# does not have much syntax so it is quite easy to learn and if you are familiar with C#, you will recognize many features of C# in F#. Well, actually it is the other way around, many F# features have been back-ported to C# over the past couple of years.
The past couple of weeks I’ve been skimming through this book: Domain modeling made functional by Scott Wlaschin. This book is truly brilliant. It is written in such a friendly style and combines Domain Driven Design (DDD) with (the functional aspects of) F#. I’m a big fan of DDD, and to see it be applied in the functional domain (pun intended) is really cool. Functional programming is often thought to only be applicable in highly mathematic software systems, I thought that too before I learned Clojure. But having an entire book on DDD in a functional style is really cool. Now, why would I mention the book? The combination of the information I got from the book and the knowledge I gained from the course really strengthen each other. The book is way more in depth but the course gives a more hands on explanation of syntax related matters.
The rest of this blog will dive a bit deeper into the contents of the course. I will try to write some comments for each of the subjects in the course, without exposing the course material itself. If you really want to know it in detail, I encourage you to follow the course yourself. There are 19 sections in this course which include the introduction and conclusion. The introduction is just Kit introducing himself and guiding you with installing an IDE. The conclusion is just him saying his goodbye’s and suggesting further studies. So let’s talk about the remaining 17 sections.
Section 2 – Start F# Programming
So section 2 was all about getting a hello, world program running. Kit shows you how to actually run the program which he does through the command line. Another useful thing he shows in this section is how to attach you debugger to VS-Code. Personally, I use JetBrains Rider and I know perfectly well how to debug a program but I can imagine that newcomers do not know this. I think everybody has seen some skilled programmer debugging through console.log/println because he did not know, or was unaware of how to debug the actual code. So explaining this in the very first section of the course seems like a good idea to me.
Section 3 – Control Flow
Next was a section on control flow where Kit explained the concept of an if-else statement. Yeah this is really beginners stuff haha yet I understand why he added such explanations to his course. You can step into this course without any prior experience with programming and you should find your way through it. In this section he also talks about indentation. Indentation is used to group blocks of code together. This is something I particularly like. I think the code is much cleaner when all those annoying curly brackets are gone. I’ve adopted a style in C# myself where I use as less curly brackets as I can so this really appealed to me. Another important thing he mentions here is the difference between mutable and immutable fields. If you want a field to be mutable, you must specifically tell it to be. So if you want to do some branching in you code through if-else you have a choice to declare some mutable variable and assign it in either of the branches. Or you can use a conditional expression where you merge the declaration and the assignment. The latter option here is the most idiomatic for F#.
Section 4 – Forward Piping
This section is one of the more interesting since it is specific to F# syntax. The forward piping operator ‘|>’. This operator is similar to the Threading macro in Clojure so I was familiar with the concept of the operator. I think this makes the F# (or Clojure for that matter) code much more readable. The operator simply forwards the previous result or value to the first argument of the next function. Since F# is a functional language, and it supports currying you are able to write some really nice and readable pipelines. Currying is simply defining a function as a sequence of functions with (one) less argument(s) (very simplified explanation…). It’s also really nice the the compiler will check the correctness of the pipeline. Additionally, he explains the concept of ‘Unit’ which is similar to ‘void’ in C# or Java. The concept op Unit is really weird in the context of a functional language since pure functions should always return a value. Yet, some functions, like println for example just “do” stuff and don’t return anything. That is why we need something like Unit (even a pure functional language like Haskell has the Unit type).
Section 5 – Collection Functions
This section should be familiar to anyone who has some programming experience. Most of the concepts Kit explains here are compairable with Linq and other collection functions in c#. Nothing particularly interesting in this section for me since this is so closely related to Linq and collection functions I have used a lot in Clojure.
Section 6 – Record Types
Record types, again more interesting. They are very useful for data modelling since they are simple values or reference types that bind names to values. Records differ from classes in a couple of ways, they are immutable by default, do not support inheritance, work automatically with F# type inference system, and work very well with pattern matching mechanisms in F# since Records are equal based on structural equality semantics, not on reference. This section also explains what a module is and how to group related code inside a module. You can see a module as a “replacement” for a namespace in C#. Namespaces in F# cannot declare any actual code, since all code must be grouped inside modules. Namespaces can only hold types and modules. The last important subject that is explained here is the concept of anonymous functions i.e. lambda functions.
Section 7 – Missing Data
The missing data section is all about option types, which are common in functional languages and really powerful. By modeling your domain with optional types you can eliminate null ref/pointer exceptions. Option Types simply allow you to define the null case for your data structure. And when you define your data this way, you will get compiler warning when you forgot to cover the null case in your programs. This way you can eliminate the null pointer exceptions, if you do not ignore the warnings at least. F# also provides you with some interesting functions with built in support for option types, like the Array.Choose() function. This is a simple filter over an array that removes null objects from the array and returns it. Kit shows how you simply add it to the pipeline to protect your program from null reffs.
Section 8 – Arguments and Parameters
Next we again have one of the more interesting sections about functions arguments and parameters. This section explains all about currying, and I have to say Kit explains it in a really clear manner. He explains the difference between curried functions and tupled functions where, tupled functions must be invoked with all arguments at once. Curried functions however support delayed invocation. When you call a function without all arguments, F# simply binds a function with the remaining arguments to what ever you called it.
Section 9 – Project Organization
Section 9 is all about how to organize your code properly in your F# project. Kit talks about namespaces and how you source files are compiled. This differs greatly from C# because in F#, files are compiled in a linear top to bottom order. This means you have to think about the dependencies in your code, because when you for example use or inherit other code, you must make sure you have it higher in the compilation order. This way of ordering also contributes to the strong compiler F# has. So when you add new files, you need to order them correctly!
Section 10 – Discriminated Unions and Pattern matching
Yet again, a very interesting section and really core to the language. Discriminated unions look and feel a lot like option types, yet, you as a programmer are the one defining the options. Imagine you have some object like ContactInformation where you can have either a phonenumber, an email address, or a zip code + address. In C# you would probably model this with some strategy pattern for example, yet in F# there is native support to express this type of construction. You can define a ContactInformation object to have either, a phonenumber, email address, or zip code and address. This is why these types are often called either types in the functional programming domain. Also you can use pattern matching on all the options to properly operate on your data. This is such a cool concept which makes code so much more readable.
Section 11 – Error Handling
This is a simple section on Exceptions, how to catch them and how to mitigate negative effects when an exception is thrown. Nothing really special here. The only thing I really thought was really nice is that you can use pattern matching on the types of exceptions that are thrown. So you do not have to make these ugly nested try catch statements like some people do in C# (nested try catch statements is bad practice anyway, and you should never do it!).
Section 12 – Generics
Exactly what you think it is. A section on how to define data structures that have generic fields and functions with generic parameters.
Section 13 – More Collection Functions
Even more collection functions that are built into the language. This section revolves around sorting data. Kit shows how to query some data, group it and order it depending on some predicate. This is all closely related to Linq, if you have experience with that, you probably find everything in this section to be quite familiar.
Section 14 – Arrays
This section describes some of the ways how to create and populate arrays in F#. Kit shows you how to initialize arrays based on functions that return some lists of objects. I liked the array.init function, which just takes some dimension and a function to create elements for that array. This will be very useful in specific cases. He also explains the yield keyword, which is implicit in many cases. Yield just allows you to specific where an element, or expression in this case ends.
Section 15 – Sequences
One of the more interesting sections again. This section is mainly focused on the differences between arrays and sequences. Kit explains that sequences are lazy, whereas arrays are not. If you need access to elements only once, you are probably better of by using a sequence instead of an array. When you access an element in a sequence, the result is computed on the spot. Here, you also need to consider any side effects access to the sequence might have. Kit demonstrates this with a simple console.log but you really need to keep this in mind in real life situations. So when you need access to elements in a enumerable objects multiple times, you might be better of with an array.
Section 16 – Lookups
Lookups are simple, they are hash-tables or dictionaries as you know them from C#. The only big difference here is that when you implement the IDictionary interface from .Net, the entries are not mutable anymore. So when you need a mutable hash-table/dictionary make sure you have got the native F# one.
Section 17 – Object Orientation
Next, a section on OOP in F#. Somehow, this feels really strange. F# has such nice and powerful mechanisms with records, pattern-matching, options types, either types and discriminated unions; why would I need overly complex OOP stuff when I have all that? Well, sometimes you will probably want to do a direct port of C# code to F# code, or you need to integrate with other .Net languages, or you simply want to use F# with a OOP approach. Kit shows some syntax and gives some tips on how to get started.
Section 18 – F# Lists
The last section is about F# lists. It is mainly about how they are stored in memory. He gives some advice on when to use arrays over lists and how to convert a list, to a sequence for example.
Conclusion
So although I’m still no big fan of video tutorials, I’m definitely going to follow more courses on Udemy. I’ve learned a lot from F# from the ground up. The real basics and how to apply it in some simple programs. Combined with Domain Modeling made Functional this is a great way to step into a new language.
#end
01010010 01110101 01100010 01100101 01101110
A podcast I listen to about F#, the WTF# podcast, just posted something interesting; The host is talking with Kit Eason about F# and they talk about his history with teaching and this course specifically.
https://podcasts.google.com/feed/aHR0cHM6Ly93dGZzaGFycC5uZXQvcnNz/episode/NDI1NGNkZmEtYTE0ZC00YzVmLTg4NzMtOTFhMGMxMDExNjcy?sa=X&ved=0CAUQkfYCahcKEwjA46-mqt3uAhUAAAAAHQAAAAAQAQ