Another Java technology blog from a developer far away from home

This month, I started to work on my (non existent) Scala skills by following the online class of Coursera. The lessons are taught by Martin Odersky itself and the best part of it: it’s free. Martin Odersky is the man behind Scala but he also worked on javac, the Java compiler and finally founded the company Typesafe Inc. This company leads many Open source projects like Akka or Play framework (which we both use at Skiddoo).

That being known, we can assume the guy knows what he is talking about !

The class runs for 7 weeks, and multiple sessions are opened every year (the last one was from Sept 15th to Nov 20th). You can join a class at any time but you won’t be eligible to a certificate (Statement of accomplishment) if you didn’t start at the opening of a session. Because at the end of each week, you have to accomplish an assignment and publish your work before a certain deadline. Obviously, when you join in the middle of the session, the deadline of the previous weeks are already gone.

But, if you’re just looking for great Scala lessons and functional programming paradigm, that’s definitely one of the place to go.

What I liked about the course, is the introduction given by Martin about the different programming paradigm. We don’t go straight into Scala. He explains where does the (mainly used) imperative paradigm comes from and what the functional paradigm is trying to do (escaping from the Von Neumann style) by using the concept of theory in mathematics.

The most interesting part of the “mathematical theory” is that it consists of laws that describe relationships between values and operations. It does not describe “mutations”. A good example could be working with the String object in Java: The concatenation of two strings can be see as a mathematical theory (one data type, with an operation on this type and a law that describes the relationship between the value and the operation). When we concatenate two String objects, it does not mutate the existing objects, it creates a new one, there are no mutations of the existing entities.

So that’s what it is about in the end, trying to implement high-level concept following mathematical theory’s behaviour. Mutation is not a part of this behaviour so we want to avoid it as much as possible. If you really want to go down the path of pure functional programming, you’ll have to do without the imperative control structure (loop, etc.) and mutable variables. There are not a lot of pure functional programming languages like this, Scala is not one of them. You can still use the imperative paradigm in Scala, but the language considers functions as first-class citizens and provides great features to work with it.

Ok, enough of theory and back to Coursera. Most of the videos contain small exercises at the end of it, to acknowledge you understood the concept which has just been explained . Once you’re done with all the videos of the week, you can work on the assignment. Each assignment comes with a couple of exercises. An SBT program’s skeleton is given with all the libs you need to accomplish the statement.

Here is the assignment example they give you, to get familiar with the process:

package example
import common._
object Lists {
/**
* This method computes the sum of all elements in the list xs. There are
* multiple techniques that can be used for implementing this method, and
* you will learn during the class.
*
* For this example assignment you can use the following methods in class
* `List`:
*
* - `xs.isEmpty: Boolean` returns `true` if the list `xs` is empty
* - `xs.head: Int` returns the head element of the list `xs`. If the list
* is empty an exception is thrown
* - `xs.tail: List[Int]` returns the tail of the list `xs`, i.e. the the
* list `xs` without its `head` element
*
* ''Hint:'' instead of writing a `for` or `while` loop, think of a recursive
* solution.
*
* @param xs A list of natural numbers
* @return The sum of all elements in `xs`
*/
def sum(xs: List[Int]): Int = ???
/**
* This method returns the largest element in a list of integers. If the
* list `xs` is empty it throws a `java.util.NoSuchElementException`.
*
* You can use the same methods of the class `List` as mentioned above.
*
* ''Hint:'' Again, think of a recursive solution instead of using looping
* constructs. You might need to define an auxiliary method.
*
* @param xs A list of natural numbers
* @return The largest element in `xs`
* @throws java.util.NoSuchElementException if `xs` is an empty list
*/
def max(xs: List[Int]): Int = ???
}

As you can see, the goal is to implement the sum and max functions by using a recursive solution. Once you’re satisfied with your implementation, you can submit your exercise to Coursera and it will be automatically graded. The program’s skeleton contains everything you need to submit your assignment by defining new SBT’s commands:

> submit edouard.kaiser@gmail.com <passwordGiveByCoursera>

You are allowed to resubmit a maximum of 5 times! No more. This is an example of a first attempt for the sum function:

Once you submit your work, you can find almost immediately the feedback in your assignment section:

Your overall score for this assignment is 9.92 out of 10.00
Your solution passed all of our tests, congratulations! You obtained the maximal test
score of 8.00.
Our automated style checker tool found issues in your code with respect to coding style: it
computed a style score of 1.92 out of 2.00 for your submission. See below for detailed feedback.
======== CODING STYLE ISSUES ========
Checking file /home/ubuntu/progfun1/submission/src/main/scala/example/Lists.scala...
1. warning at line 28 character 6:
Avoid using return
2. warning at line 30 character 6:
Avoid using return
Processed 1 file
Found 0 errors
Found 2 warnings
Consult the style guide at https://class.coursera.org/progfun-005/wiki/ScalaStyleGuide

As we can see, our code is working but in Scala the return statement is not necessary here, this is why we didn’t get the maximal score. If we submit this code again without the return statement, we would get the maximal score (10/10).

Now you have a global idea on how it works. And so far, it has been a great exploration into functional programming. Martin’s expertise and pedagogy are brilliant, I’m very excited about going on with this class.