Learning Scala by Example, chapter 4

Please note that I am skipping chapter 3. Chapter 3 is a “running ahead of myself” type of chapter. It shows examples of what is possible for you in the future, with Actors and Messages. It is there to keep your interested in learning further. But the actual meat of Scala continues in chapter 4, so here we go.

Chapter 4 introduces you to expressions and functions. Most of the example are best run in the Scala shell, rather than as code in a separate file. However, this does not really matter, and the distinction is blurred by the fact that you can copy any code and then paste it to run in the shell.

First, you will learn about simple expressions and functions. The difference between Java function syntax and Scala syntax is minor. For example, what was in Java

long myFunction(int a, double b) {

  return myresult;
}

will become in Scala

def myFunction(a: Int, b: Double): Long = {

  myresult
}

where the changes are obvious. (Please name the differences anyway, for practice). Now that you have done so, I will do it for, just to compare.

  1. The type of a variable or the return type of the function have the same syntax, for example ‘: Long‘. The emphasizes that functions are variables too. You can assign them, pass them, and eventually, create them in your code. Even dynamically!
  2. You see ‘myresult‘ as the last statement in the Scala function. Whatever happens to be on the last line is what the function will return, and the word ‘return‘ is not necessary.

In fact, many IDE’s will notice when you are pasting Java code into a Scala file, and suggest to change the syntax to Scala – which they do very well, and without errors.

The not-so-obvious Scala constructs will be nested functions, recursive functions, and the concept of tail recursion. What are these?

Nested functions allow you to place a helpful support function inside of your function definition. Cool and easy, and makes your name space clean.

Recursive functions are nothing new. Except that in Scala they get a great emphasis, because they are from the world of functional programming. So you will get a few exercises on thinking in recursion, and letting the compiler take care of the optimization.

The really new notion, something that requires a little thinking, is a concept of tail recursion. This simply means putting the call to the function (this is the call that makes it recursive) at the very end of the function body. Example? – Be my guest, here

def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)

You see that the last call is the same gcd as the function we are defining.

Why does it make a difference? It is because the final call in a tail-recursive function can be implemented by a jump back to the  beginning of that function. This leads to limiting the number of expansions in the implementation, and results in better execution efficiency.

Now you are ready to read the chapter and do the exercises. My solutions are found right here, and you can compare them to yours.

Good luck and see you in chapter 5.

Mark Kerzner
Written by:

Mark Kerzner

Mark Kerzner is the co-founder of Elephantscale. He is a Trainer, Author(AI, Machine Learning, Spark, Hadoop, NoSQL, Blockchain)

Leave a Reply

Your email address will not be published. Required fields are marked *