Showing posts with label scala. Show all posts
Showing posts with label scala. Show all posts

Sunday, September 26, 2010

The Next Big Java Language; my $0.02

Time to to add my $0.02....

What is the Next Big JVM Language going to be?

I've been watching the whole evolution of alternative languages on the JVM for a few years now. Long enough to start tracing the hype around the different languages.

Yet none of these languages are really mainstream, despite their apparent advantages.

I think the reason for this has absolutely nothing to do with the technical features (or lack thereof) of any of these languages.

I think the NBJL will be the one that large organisation or group of organisations with enough clout and deep enough pockets is actually willing to go out and effectively market both to decision makers and developers.

No doubt Java did strike a chord with mainstream developers, but quite honestly was that more to do with Sun's aggressive marketing campaign or because it of it's many technical merits?


Friday, September 18, 2009

A quick sample of Lists and Tuples in Scala

So I've been challenged with a simple problem to solve in Scala, that is; to take a List of values and and then sum each corresponding value in the list together into a resultant list.

For example:

[1,2,3,4,5] becomes [3,5,7,9]

You can accomplish this in Scala using the List class and its functions, these include:
  • zip : this function takes an input list and creates a list of tuples with each mapping pair, discarding jagged values.
  • tail : this function returns a list minus the first element of the list on which tail is performed.
  • foldLeft : combines the elements of the list together with a seed value (in my case I use a target List).
  • reverse : reverses the order of the list.
so here is the code to do this:
object SumPairs extends Application {

val numbers = List(1,2,3,4,5);
val result = numbers.zip(numbers.tail)
.foldLeft(List[Int]()) (
(list, y) => {
y._1 + y._2 :: list
}
)
.reverse;
println(result);

}

Basically this works as follows:
  1. I create a List of Tuples of each adjacent value in the List by getting the tail of the List.
  2. I get each Tuple in the list, sum it and prepend it to the List returned by "foldLeft".
  3. I reverse the List to fix the order as a result of doing a prepend.
Now this is is designed to be functional but not overly complex (at least I hope not), Since I'm still fairly green in the Scala dark arts I'd like to hear on improvements.

BTW the challenge I'm talking about can be found here.

UPDATE:

Another solution would be to use List.flatMap which basically takes a function which returns an Iterable and appends it to a function, it pretty similar, It's maybe a little more concise, I'll let you decide:

val numbers = List(1,2,3,4,5);
numbers.zip(numbers.tail).flatMap {
value => {
List(value._1+value._2);
}
};

Saturday, May 24, 2008

Is Java the new C?

I've been watching the language changes being proposed by the community for Java 7.0 like a hawk.

Alex Miller's Pure danger tech blog has become one of my favourite reads as a result.

However I'm pretty sure I'm not the only one who seems to be rather disappointed about what came out of JavaOne this year regarding the proposed Java language features.

It seems that many of the changes will probably be postponed until Java 8.0.

Maybe this is not such a bad thing though.

If I look at the message coming out from JavaOne I could summarise it as being: "Please use Java as a platform but you don't need to use the Java language at all".

In fact I have have a vast array of excellent choices:

  • If I want a highly productive platform for doing web development, use JRuby.

  • If I want a highly productive platform for my existing Java Developers use Groovy.

  • If I want a concise type safe platform with high productivity use Scala.

  • If I want a mainstream language with Java benefits and high producivity (when it matures) use Jython.


  • Of these languages, Jython (Python in Java) and JRuby (Ruby in Java) are the most "mainstream" being in the top ten on the Tiobe index with Python and Ruby respectively sandwiching C# - which in itself is quite amazing considering that neither have had C#'s marketing.

    Groovy and Scala are a little less mainstream but both have broken into the top 50 with Groovy at number 33 behind Smalltalk and Haskell and Scala finally breaking into the top 50.

    Would I still need to write stuff in Java? sure, but only for those bits and pieces which need some low level stuff where speed becomes important especially considering that Java is the only language in the list which still supports primitives, In this scenario the Java language takes on roll much like C.

    In fact I'd even go so far to say that Java needs to stay the way that it is in in order to keep it viable for this very important role.

    After all we wouldn't want to turn it into the new C++.

    Maybe then we should stop regarding Java as the new Cobol but rather look at it as the new C.