For the first half of the summer, I took the online Functional Programming Principles in Scala course on Coursera. I should probably point out that I didn’t take the $50 official I’d heard good things about the language, mostly from Dick Wall on Java Posse podcasts, and it seemed like a good way to try functional programming again after a brief, rather unpleasant, introduction to Lisp in college. Overall, my main goals were to a) re-acquaint myself with functional programming and b) get a basic, can-start-on-some-code-now understanding of Scala.
Before I get into how well I think the whole thing worked out, I’d like to talk about the course itself. This course was broken up into about 7 weeks worth of lessons, with homework assignments due every week (every 2 weeks for a couple of assignments that were correspondingly bigger than the others). These were being run through an automated grader that ran both a wide set of unit tests and a code analyzer to check for coding style (it’s a massively online open course after all -automated grading, as opposed to human grading, is pretty much required for the whole concept to work). The thing I think I liked the most about this is that it forced me to actually go through the course and try. It also caused me to drop the course just before it started a couple of times, but the psychology of knowing that I’m being graded made me take this course a lot more seriously than some other online courses I’ve seen where the lectures were online but never actually required any sort of active participation on my part. This probably only works with people who cared about their academic performance in school, but I was 1 of those people so a grade that would never appear on a transcript was still enough for me.
Homework assignments could be submitted 5 times apiece, with your score being the highest of the 5. You lost points for failing any unit tests the grading system ran (and it ran a lot more tests than the students did), and for every time it dinged you on a coding style issue. Ultimately, the coding style checker was the most annoying part of the grading process. Being used to Java, I kept trying to write code in a manner I was used to, as opposed to writing functional code. Since this course was pushing the functional aspect of Scala, my instinctive way of doing things ran afoul of their style guidelines pretty regularly. However, it was also the only thing that forced me to approach the problems from a functional mindset, so on the whole it really helped drive the concepts from the video lectures home.
Now for how well my plans for learning more about functional programming and the Scala programming language turned out: well on both counts really. I think I have a better understanding of functional programming (emphasis on the fact that its relative to my understanding before starting the course, which wasn’t very good). Function programming, when done correctly, requires a very different way of thinking than imperative programming, which is what I spend all day at my day job doing. My first introduction to functional programming was Lisp, when I was a sophomore in college. I don’t know if it was Scala’s similarity to Java, or the fact that I spent a lot more time getting into functional programming than I did back in college, but after this course I at least know I can get my head into a functional programming mindset, which is a lot more than I could have said 10 years ago.
As for getting a good introduction to the Scala programming language, I was very happy with how that turned out as well. I have a good enough understanding to write some simple Scala code, and to get myself started on some projects, but I’d like some more time forcing myself to write Scala code and being held to doing things the “Scalaic” (I have know idea if that’s a real term, but I’m going to use it) way before I’d consider myself reliably proficient. Since being able to get started and make sense of search results from the Internet when I look for help was the goal, I’d call the course a success on that front.
As for the Scala programming language itself, I get the hype I was hearing on the Java Posse podcast (and apparently Oracle did too, given the Scala features that made it to Java 8). I really appreciate the functional way of doing things, even if it’s hard for me to get my head into that style of thought. However, when I do write some functional code, it’s clean, it’s concise, it’s simple, and it’s powerful. It’s basically everything programmers dream of code being.
Another aspect of Scala that I found to be really fun to put into action was functions as first-class citizens, AKA passing functions around as parameters, variables, and return types. While I get that there are other languages out there where this is possible, I got to see the power in this course, so I’m talking about it here. The context where it came up in the course was a programming assignment on Sets, only instead of dealing with the collection type, our sets were defined as functions, taking an integer and returning a boolean indicating whether or not the integer was in the set (i.e., defining a set consisting of all even numbers). For a slightly more real-world example, if someone were to…say…build a social network that let you define who your initial audience was on the fly, that dynamic visibility would best be implemented as a function, taking in someone connected to you on that social network and returning a boolean indicating whether or not they fit the visibility requirements of that post. Dynamic visibility would then just be some anonymous function passed in as a parameter.
Another very interesting aspect of Scala, albeit 1 that I haven’t had a lot of time to play with, is the Option type. For those of you unfamiliar with this, an Option is basically just a wrapper around some other variable type. This wrapper can have 1 of 2 possible values, Some value, or None. Some holds an actual value, whereas None is similar to a null. While checking to see if your Option value is None is basically the same thing as checking for if something is null in Java, Scala’s Options come with built-in utility functions like getOrElse(), which lets you specify a default value for Nones. So far, I haven’t used Options enough to know if they have any real use beyond the getOrElse() method, but seeing as how it just made it into Java 8, I’m guessing there’s something to it. Still, it’d be nice to know just what the hype was, other than an easy means of ensuring some type of default value.
Lastly, Scala has some powerful functions that run over most of their collections, such as foldLeft()/foldRight(), map(), and foreach(). I’m familiar with the basics of these methods, and the Scala course ends with some assignments designed to force you to use them. Skillful use of these functions can let you do a lot of stuff with only a little bit of code. That being said, I haven’t mastered them enough to really get the most out of them, or understand them immediately when I see them in code snippets online, or to be able to use them without a lot of Googling (including the parts where I had to use them in homework assignments). I’m mostly basing my impression based on what I’ve seen from people who know what they’re doing. I think my lack of mastery here is mostly due to my difficulty getting my head into a functional programming mindset, and I’m pretty sure that with some time, effort, and stress working with these functions I can master them – I just haven’t put in the hours yet. That said, if you’re looking to learn and master Scala, it’s probably worth looking into these functions and seeing if you can use them in your code, just for the practice, although in practice that may be tough given that my instinct would be to go with something I’m already comfortable with.
Overall, the Functional Programming Principles in Scala course on Coursera was a good course. The video lectures could be a bit dry, but they’re broken up into several small chunks and can be watched over the course of a week. Scala itself is an interesting language, and it’s very easy to see why people like it. For people like me it’s a very kiddie pool-esque way of getting into functional programming, although it is dangerously easy to not write functional code at all, almost eliminating the point of using Scala. Still, I like the language, and when I do write functional code that works, I’m amazed at how much I accomplished with so little code. I’d like to take some more time with it, which is why I’m thinking about trying to put together a library that can read in flat data, and organize based on a user-defined JSON template. I have specific scenario in mind, but that’s the topic of another blog post. Based on brief experience with Scala, the endgame I’m picturing for this project would be a great fit for the functional programming style it allows, which would force me to practice some of those more powerful acts of functional voodoo that I’m still not 100% comfortable with. Even if that doesn’t work out, at the very least I’ll have learned some of the practical limitations of Scala, which is good enough for me.