Thursday, June 26, 2008

scala functional programming

i continued learning scala reading the 'Scala By Example' tutorial from the website. next up was functional programming in scala. in scala functions are first class citizens. that means you can declare functions and pass them around as parameters or return them from other functions. let's first see, how to define a function:

def plus1(a : Int) : Int = 1 + a

it takes an argument a of type Int and returns 1 + a (you guessed it). you can call this function like plus1(2). [you could have enclosed the body of the function in curly braces, but it's just one expression, so you don't have to]

so, now let's define a higher-order function. that's a function which takes another function as parameter or returns a function.

def doWithInt(a : Int, f : Int => Int) : Int = f(a)

this is a function that takes an Int as first parameter and a function type as second. the parameter f is a function of type Int => Int which takes an Int as parameter and returns an Int. this function simply applies the function f to the parameter a. you could call this function passing to it a reference to the function plus1 declared before: doWithInt(3, plus1). this will return 4 as it applies plus1 to 3.

so, these are just the basics of functional programming with scala. things can become quite weird when it comes to more complex higher-order functions, e.g. function returning functions, currying or recursion. have a look at 'Scala By Example' for examples.

one thing i forgot to mentioned is some syntactic sugar for the example above. instead of calling doWithInt(3, plus1) you could have equally done

doWithInt(3, 1 +)

this is because + is a member function of the type Int, as seen in a previous post.

so, that's it for this post then. more on functional programming with scala coming soon.

Wednesday, June 25, 2008

eclipse ganymede release

last week i downloaded a release candidate version of the new eclipse ganymede release and today the final version is coming out. i've downloaded the big bundle including modelling tools.

i like the new modelling features, especially because i've been waiting so long for some (free) support in that area in eclipse. all the uml plugins i've tried so far were either quite limited in their functionality or their usability. but now all sorts of uml diagrams are supported, e.g. class diagrams, activity diagrams, use case diagrams, etc. What's missing are some kind of reverse engineering features and sequence diagrams (or i haven't found them yet).

the new breadcrumbs feature shows the path to the current item at the top of the editor. it's like project -> src folder -> package -> class -> class member. each of these crumbs has a context menu which shows other items at the chosen level. i think that's quite nice, as it allows more context-sensitive working like mylyn task focussing, which makes it easier to find items than in the package/project explorer.

javascript support has been greatly improved. it now offers many features known from the java perspective. For example you can follow function calls by pressing F3 oder holding Ctrl and clicking on a method call. you can display the call hierarchy of functions and there is refactoring support, e.g. renaming functions.

one thing i'm still waiting for is a mylyn feature that allows for creating reports, e.g. a report that lists all completed tasks per week with estimated time and actual time. sadly, mylyn hasn't been improved that much, i think. but still it's a great feature.

these are just some of the new features and improvements. there are many smaller ones, like junit test excectution time, enhanced tooltips (e.g. javadoc on types), autocompletion, subversion support, etc.

one feature i haven't tried out so far is the ECF (eclipse communication framework), but that is one i'll try out for sure. from what i've read, it seems to support remote team collaboration, e.g. to share an editor. that sounds interesting.

Friday, June 20, 2008

quicksort source code

andres asked for the source code of the quicksort comparison.


Please give me some feedback

google syntaxhighlighter

recently i've found the google javascript syntaxhighlighter. it looks kind of like this - i can't get the blog to not to convert line breaks in a pre-tag to br-tags. but i think you can guess what it does ... and it's quite cool.

public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;

Tuesday, June 10, 2008

quicksort java scala groovy

today i compared the running times of the quicksort algorithm implemented in java, scala and groovy. i took the scala implementation from 'Scala By Example' [available on] and based on that i wrote the other two. i ran a test on an array of 1.000.000 integers. the algorithms were not optimized for each particular language [but i compiled the groovy file at least] and i don't really know if these results are quite representative. but anyway, here are the running times:

java: 280 ms
scala: 359 ms
groovy: 535250 ms

Thursday, June 5, 2008

first look at scala

recently people are talking a lot about scala ('the next java'). so i decided to have a look at it. in this post i am not going to give an introduction to scala, but i will give some examples of the language features that show what scala can do for you. the examples are taken from the scala tutorial, which is available at

scala runs on the jvm and the syntax should be quite familiar to java developers. hello world in scala looks like this:

object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")

there are some interesting things about this. first, the object declaration means that HelloWorld is a singleton object. that is also why the main 'method' is not declared static. as HelloWorld is a singleton main is 'globally available' and doesn't need to be static. static members (methods) or fields do not exist in scala. second, main doesn't declare a return type. it's called a procedure method.

as said, scala runs on the jvm. because of that scala classes can interact with existing java classes. for example you can import and use java classes:

import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._

these import statements show that you can import multiple classes from one packge with that curly braces notation. and you can import all names from a package or class with the underscore notation. the above import makes members of DateFormat available, so you can call the static method getDateInstance() directly. Why not use the * notation for the imports? * is a valid identifier in scala. you'll see why, soon.

Methods with a single argument can be called in infix syntax in scala that means the method format of DateFormat can be called like in the following example:

val now = new Date
val df = getDateInstance()
val str = df format now

this call is equivalent to df.format(now). at the first look this is not quite an interesting feature but it offers some possibilities, one of which i'll show next. in scala 'everything is an object', e.g. primitive types as well. then remember that * is a valid identifier and the infix
syntax for methods. then look at this code and try to figure out how it works:

2 + 3 * 4 / 2

this is equivalent to


all the integers are objects and the operators are methods of these objects. operators are method calls.

this was my first look at scala. more looks coming soon, for sure ...

Monday, June 2, 2008

staying up to date

so, recently i found some ways to stay up to date. first i'm reading news everyday on some news sites, e.g. theserverside, infoq and slashdot. then i found some interesting blogs on, theserverside blog news, adam bien's blog and some more. and i listen to the javaposse podcast, which is very interesting.