A review of my experience with the Functional Programming Principles in Scala course

Introduction

Functional programming is a programming paradigm where programs are constructed by applying and composing functions and it quite popular in the data science application because of some of its useful properties that can help for example with scaling computations. One well-known resource to get into functional programming is the Functional Programming Principles in Scala course by École Polytechnique Fédérale de Lausanne.

In this post, I share my personal experience with completing the Functional programming in Scala course on Coursera in May 2020, briefly walk through the content and write about the course assignments. I wrote down each of the paragraphs as I went through the course, so it is not a retrospective evaluation but more of a “review-style diary” of the process of completing the course. Since this blog is oriented towards R, I will also try to make parallels with the R environment that can be relatable to R users and developers.

Disclaimer, what to expect

First off, this post does not mean to be an objective review as your experience will most likely be very different to mine, based on both

  • your expectations coming into the course and
  • your prior background
  • your prior experience with Scala and sbt

I expected to get deeper and more structured knowledge for practical use in Scala with relation to data science and functional programming, as my prior exposure to Scala was mostly maintaining/fixing in an already established code base and creating Spark extensions to work with Spark from R.

I wrote these comments as I went through the content and assignments instead of after finishing, so you might find the tone of the entire article change as the weeks change.

Course organization, pre-course preparatory work

Organization

The course is organized into video sessions split into 6 weeks, but since it is fully online you can choose your own pace. I completed the course in two weeks while being on a standard working schedule. Each week apart from week 5 has a programming assignment that is submitted to Coursera and automatically graded.

I found the assignments executed very well from a technical perspective and had no issues at all with downloading, compiling, running, and submitting them.

You can submit each assignment as many times as you want, so there is no stress making the submission right on the first try. Once the course is completed, you get a certificate.

Pre-course setup

Since I have prior experience with Scala and sbt, the preparation was not difficult and I was able to get going quickly.

If you are an R user used to conveniently opening RStudio and easily installing packages, you may be surprised by the difficulty of the whole setup. The course does provide setup videos for major platforms, so with a bit of patience, you should be good to go.

Week 1

Content

Briefly present basic programming paradigms and concepts, model of evaluation of expressions, call by name and call by value strategies, and focuses on recursion, also introducing tail recursion.

Assignment

The assignment is purely recursion oriented - Pascal’s triangle and such. I was able to complete the assignment easily, even though I found the final exercise challenging. My issue with the content was that this felt more like school homework and I was coming into the course looking to improve and gain practical skill with Scala.

Week 2

Content

The lectures won me over with constructing a custom class for working with rational numbers. This immediately clicked with me and also was very useful, because it walked through creating classes, defining methods and operators, constructors, requirements, and assertions in a very concise and practical way.

Assignment

A different story. The introduction to the problems goes something like

“We represent a set of integers by its characteristic function and define a type alias for this representation.”

I had a bit of intuition around this (and if you come from a CS background this may come as second nature), but if you have neither of those, you might have some terminology to study before you even understand what the assignment is about. This is fine if it is in line with your expectations of the course, but if you came for something practical, thinking about programming recursive transformations on integer sets represented via characteristic functions may not seem like the best investment of your time.

Week 3

Content

Has a nice explanation of singleton objects and finally, we look at organizing classes, traits, and objects into packages, very nice. Until we are back to recursion again, this time on binary trees.

Assignment

Writing recursive methods on binary trees. I spent way more time thinking about how to do it than programming. The methods are very short once done, but complex to think about, especially if you are not used to thinking about recursive traversal of binary trees.

Also, it got frustrating. The assignment tests were failing because of some predefined timeout that is hard to reproduce locally (you don’t know what tests the grader runs) and you only know when you submit. The issue was to make a recursive method more efficient by placing brackets better around infix operators, which I honestly would not figure out without reading through the course’s discussion board.

Especially frustrating about this is that the video content only covered trivial cases and the assignment asked for far more complex problems. I was close to just quitting the course at this point.

Week 4

Content

Starts by rewriting Boolean and Integer types as abstract classes - Peano numbers in case of non-negative integers. Quickly flies over subtyping, generics, and pattern matching and shows only trivial examples. The more interesting example, well, go do it yourself! The final video shows a bit of practice with lists programming a recursive insertion sort.

Assignment

Implement Huffman coding methods via binary trees using pattern matching. I had no idea what Huffman coding is, so I had to first research a topic I had no interest in to even understand the assignment. To give a taste, this is one of the exercises:

“Define the function decode which decodes a list of bits which were already encoded using a Huffman tree, given the corresponding coding tree.”

Also, one of the hints, “hint: very simple” was simply priceless.

Weeks 5 and 6

Content

Week 5 looks at methods available for lists and gives more details behind the intuition. The later sessions even show how we can prove some properties of the recursive methods, which I found interesting. In week 6 we go deeper into the collections is Scala and look at for expressions. We solve the n queens problem with Sets and for expressions. One session is dedicated to Maps, Options, and methods such as groupBy.

As an example, we implement addition of polynomials using these concepts. The conclusion session nicely brings things together by walking through one implementation of the conversion of telephone numbers to sentences. The session looking at Map, Filter, and Reduce methods was very relatable to R’s Map(), Filter(), Reduce() and Position() functions as their design is similar to the corresponding methods in Scala (look at ?Reduce in R for more).

Assignment

There is no assignment in week 5. Week 6 assignment is to compute anagrams of sentences. Compared to the previous assignments I found this one much more fun and interesting, so it felt like a positive ending to the course. Apart from the very first one, this is the only assignment I worked to get a 100% score as I found it motivating. If more of the course had at least this level of practicality, I would have enjoyed it much more.

Scala in VS Code

Scala in VS Code

Course execution and technical notes

  • Each assignment comes with a pre-prepared sbt project that can be compiled and partially tested, so it is easy to start working on the assignment
  • The submission and grading process work conveniently and automatically
  • The reading materials themselves often refer to Java constructs to explain Scala constructs, which may tell you nothing if you do not have prior experience with Java. For instance: “Traits are like interfaces in Java, but they can also contain concrete members, i.e. method implementations or field definitions.”
  • Some video lectures are placed in the wrong weeks (the narration says Week 3 but they are actually in Week 2) so it can get a bit confusing
  • During the first 4 weeks the videos stop for the viewer to fill in the examples, which I guess was meant to be interactive but I found it distracting and always skipped. In the final weeks, this feature was not there, which I found nice.

TL;DR - Just give me the overview

  • The course introduces key concepts of functional programming in Scala with a strong focus on recursion and walks the students through methods on immutable Scala objects. It also introduces pattern matching, for expressions, subtyping, and generics
  • The feel of the course was school-like as opposed to more practice-oriented courses
  • The assignments are challenging and I found them school-like, which I did not prefer as I was looking for more of a practical course