Monthly Archives: October 2009

Quick introduction to functions in Scala

So, as you might already know, Scala is a functional language. This means that it is heavily based on functions (Scala actually mixes Object Orientation and Functional Programming).

Lets take a look on how to use functions in Scala. It is really fun, after you get the feel for it.

First, you must know that in Scala, almost (if not) everything can be used as a function identifier. An example:

1.+(2)

This will  result in, obviously, 3. It is even like Scala had operator overload, except that +, -, / and * are not exactly operators in Scala. They are functions, like everything else. Take a look at the scaladoc for the Int class, and notice all the functions for those operators.

Of course, the code above is really ugly. So you should write it like this:

1 + 2

and the Scala compiler takes care of transforming it to the previous example. And what’s more: this is not any kind of special case. This is valid for any function. So, instead of writing this:

calculator.sum(2,3)

you could write this:

calculator sum(2,3)

Dumb example, but nice anyway, isn’t it?

Something more interesting now. If you are used to Javascript, you are used to sometimes pass functions around to other functions. Scala allows us to do the same thing:

def operationTest(operation: (Float, Float) => Float): Float = {
    operation(5,3)
}

The code above declares a function, operationTest, that receives another function, operation, which receives two Floats and returns a Float as well.

The operationTest function then calls the function it received. The result of operationTest is whatever results from operation – in Scala, the return value of a function is always the result of the last statement in it.

You can exercise operationTest this way:

// returns 8.0
operationTest( (x: Float, y: Float) => x + y )

// returns 2.0
operationTest( (x: Float, y: Float) => x - y )

// returns 1.66666666
operationTest( (x: Float, y: Float) => x / y )

// returns 15.0
operationTest( (x: Float, y: Float) => x * y )

So in the code above, we are declaring anonymous functions and passing them to operationTest, which then executes whatever function it received.

Here, (x: Float, y: Float) is the function header, or declaration, and everything after => is the function body: x + y, x – y, x / y and x * y in these cases. Notice also that we are not declaring the functions’ return types. Scala infers them from the execution of the body, and thus from the type of the parameters.

That’s it for now. Willing to learn something in specific about Scala? Post a comment and let me know! I’m in the quest for mastering this language, so challanges are much appreciated! =)


Base Scala classes

And I’m back! Last month was really busy. Among other things, I had to travel, both for business and a small vacation. But now I’m back. Uhu! Thank you all for reading!

Now, back on topic. This time, I’ll cover the basic Scala classes on its class hierarchy. This is really simple stuff, but nice to know.

In Java, all the classes inherit from the class Object. And beside that we have primitive types. Now, in Scala, everything is an object. But to help things to be clearer, the top level classes are a few more then the sole Object class in Java. Take a look at the picture bellow:

Scala top level classes

Scala top level classes

The class Any is what everything else inherit from, directly or indirectly. Then we have Nothing, which represents, ahm… nothing. You can use it when you don’t want a method to return anything at all – we might come back to this in a later post, with examples. The two other interesting “sons” of Any are AnyVal and AnyRef. As their names help us understand, the former represents values that would be primitives in Java and the later represents object references.

An extra decendant of AnyVal is Unit. Its meaning is the same as void in Java – with this, you can explicitly return void from a method if you want, for example. On the AnyRef side, there is Null, which represents, ahm…. null. Questions? Pretty obvious, right?

One thing that I really like about this whole hierarchy is that it makes clear that in Scala EVERYTHING are objects. Beautiful, ain’t it??


Follow

Get every new post delivered to your Inbox.