Lambda Expressions: Java 7 and Scala


I just came across a blog post written by Baptiste Wicht in which he points to
some test cases of lambda expressions in the upcoming Java 7 release.

For the first moment I was completely shocked about the syntax and it took me
some time to figure out what this short snippets are doing.

Here is the Java code:

int i1 = #()(3).();
Integer i2 = #()(3).();
int i3 = #(int x)( x + 1 ).(3);
int i4 = #(Number x)(x.intValue()).(new Float(3.0f));

In Scala this could be written as follow:

val i1 = ( () => 3 )()
val i2 = ( () => 3 )()
val i3 = ( (x: Int) => x + 1 )(3)
val i4 = ( (x: Number) => x.intValue )(3.0f)

I definitely prefer the Scala way.


Marco Rico Gomez is a passionate software developer located in Germany who likes to share his thoughts and experiences about software development and technologies with others.

blog comments powered by Disqus