It’s important for software professionals to learn new skills and remain current, and I’ve often wondered whether freely available online courses are a good way of doing this in a structured way. When one of our engineers suggested that some of us might like to try Coursera’s unit on Functional Programming In Scala, it sounded like a great opportunity to kill three birds with one stone: brush up on my functional programming, start learning the Scala language, and evaluate the experience of studying online through Coursera.
The Rise of Functional Programming
Functional Programming (FP) has long been advocated within academia and small circles within the industry, but has taken its time in breaking through to the mainstream. Scala first appeared in 2003 as a hybrid functional/object-oriented language running on the Java Virtual Machine. Shortly after, in 2005, Microsoft introduced F# to their .NET CLI platform. Since then, functional techniques have found their way into many languages in an attempt to leverage the advantages of a succinct, declarative programming style that can overcome some of the more annoying aspects of imperative OO languages such as Java and C#.
One of the key features of FP languages is the idea of functions as “first class citizens”: they can be passed around as arguments to other functions, returned as values from functions, and in a true functional language they can also be “curried” – partially applied by providing just some of the required parameters, which creates a new function. Another important idea is that functions should not produce side-effects: there is no mutable state, and the same function applied twice with the same parameters is guaranteed to give the same result. This property allows scope for operations to be multi-threaded automatically by the compiler, without the programmer having to use threading and synchronisation constructs that can be error-prone and difficult to reason.
Through the use of anonymous classes and interfaces, it has been possible to emulate some basic functional principles in Java, albeit with an unnecessarily cumbersome syntax. This trend shows itself in libraries such as Google’s Guava, which provides filter() and transform() functions in the Collections2 utility class and functional interfaces such as Function and Predicate. With the recent release of Java 8, certain FP techniques have finally made their way into the core language, although they are often just syntactic sugar built on top of the same old OO concepts. That said, we all like a sugar fix, so bring it on!
Why Learn Scala?
Scala runs on the Java Virtual Machine and allows developers to mix OO and FP while benefiting from the rich ecosystem of libraries and frameworks that already exist in Java. The object-functional paradigm of Scala is a powerful one in its own right, and it also provides an entry point for the OO programmer who wants to test the functional waters without turning to a “pure” functional language such as Haskell.
The Course Structure
The intention of Coursera’s unit on Functional Programming in Scala is to teach the basics of FP and how to combine it with OO programming. This is done using Scala, but with the purpose of teaching the paradigm rather than everything there is to know about the language and its core libraries. This might disappoint some students, but it’s definitely a good thing; FP requires a considerable mind-shift and it’s important to be guided through this before delving too deeply into language specifics.
The course is free unless you decide to enrol in the optional “Signature Track”, which provides you with a verified certificate if you pass. Once you’re enrolled and the course begins, a new set of video lectures appear each week, presented by Scala language designer and academic Martin Odersky. Who better to learn from?
The lectures are arranged into seven modules, covering the basic evaluation model, recursion, higher order functions, data types and abstractions, types and pattern matching, lists, collections, and lazy evaluation. Because my university taught FP in Haskell for our first-year programming course, I had encountered all of these ideas before, and the first module in particular seemed slightly redundant. Having said that, I was glad to have the refresher and definitely needed to have the concepts repeated in a Scala-specific way before tackling the assignments.
The true power of FP became apparent in the module on types and pattern matching, which reminded me of the things I liked so much about Haskell. The final module on lazy evaluation covered streams, which felt particularly applicable to Java programmers given the addition of the Streams API to Java 8. The course ended with a case study that led into the final assignment, which required students to implement a solver for the Bloxorz game.
Alongside the lectures, a new assignment is released most weeks. The course website has comprehensive instructions for setting up your development environment for Scala programming in either Eclipse or IntelliJ, and each assignment is a new Scala project that requires the student to fill in the unimplemented functions.
The assignment tasks are well-explained, although I found it was possible to simply start filling in the required functions without having a very deep understanding of how everything was supposed to fit together. I would advise against doing this; I definitely started learning more once I looked at the bigger picture, thinking more deeply about the data types and functions that were provided.
To write effective functional programs, you need to choose the right data structures to begin with, and then use functional decomposition to break down the problem space into manageable units. In this course, both of these steps were largely done for us, so I don’t feel that it did a good job of teaching functional software design. This probably reflects the fact that it’s an introductory-level course and that the marking is entirely automated.
Despite this criticism, the assignments were still a good way of becoming familiar with Scala syntax and applying the concepts that were taught in the lectures. The assignment structure also encouraged the use of unit tests to verify your program’s correctness, although in FP it can be surprising how often your first attempt turns out to be correct.
The course website comes with a discussion forum if you get stuck or want to feel like part of a student community, but I didn’t use this feature; since a few of us at Tyro were doing the course at the same time, we were able to discuss our progress each week. Each assignment is marked 80% for correctness and 20% for style. I found that submitting an assignment that failed one or two test cases still resulted in a fairly high mark. The test result output didn’t give many hints as to what was wrong, but it was still easy to go back and do more testing of my own before resubmitting to achieve full marks. I never received any negative feedback for style, so I’m not sure what this was checking for. Because it’s fully automated, assignment feedback was usually available within a few minutes of submitting the assignment. This is a very efficient way to deliver a MOOC, as no tutorial or marking staff are required. Although personal feedback would be preferable, I didn’t find it necessary, and it seems that this automated delivery style works well for an introductory programming course.
Tyro is a technology company, and Tyros are passionate about delivering high quality software and shaking up the banking and payments industry. An important part of this is being fluent across a wide range of tools in order to pick the right tool for the job. While our core products are Java-based and likely to remain so, who’s to say that we won’t find Scala a better choice for some solutions? Regardless of whether we choose to use Scala or not, this course has helped me to get back into the FP mindset, which I’d argue is a valuable skill for any programmer, and perhaps a prerequisite in order to fully benefit from the latest features in Java 8. I would recommend this seven week course to any programmer with an interest in FP.