Friday, November 20, 2009

Scala for dummies like me!

Well! what should I be saying? I am that kind of person who keeps shifting from one interest to another. Once I am extremely interested in .NET (and I still am, but .NET is now an ocean – would take me too long to catch up with everything) and now I want to explore Scala – all new programming language on the JVM (new to me!) which has been receiving rave reviews. So, I went ahead and ordered Programming in Scala book on a1books.com (which by the way is a great site) but unfortunately I did not receive my copy yet. So here i am with all intention to do something with Scala but am out of resources (no offense but there are no simple tutorial on Scala which is interesting and which doesn’t make me fall asleep in 2 minutes).

Lets first get started and write a Hello World program.

class HelloWorld
{
def main(args: Array[String]){
println("Krishna Vangapandu - Hello Scala world!");
}
}




When you compile this and execute it, you would get a “java.lang.NoSuchMethodException: HelloWorld.main is not static”. Well, the mistake that I did was to put a “class” – but it should be object. So the hello world would be



object HelloWorld
{
def main(args: Array[String]){
println("Krishna Vangapandu - Hello Scala world!");
}
}


So what's the difference between "class" and an "object". Obviously there is no problem for the compiler, only the runtime blows! So what does the documentation say about this? Well even better lets use a decompiler to decompile the .class file we obtained and see how the scala code would when written in java. By the way, I am using this decompiler - which i should say is freaking awesome.


"class HelloWorld" decompiled.




import java.rmi.RemoteException;
import scala.Predef.;
import scala.ScalaObject;
import scala.ScalaObject.class;

public class HelloWorld
implements ScalaObject
{
public void main(String[] args)
{
Predef..MODULE$.println("Krishna Vangapandu - Hello Scala world!");
}

public int $tag()
throws RemoteException
{
return ScalaObject.class.$tag(this);
}
}


"object HelloWorld" decompiled.



import java.rmi.RemoteException;

public final class HelloWorld
{
public static final void main(String[] paramArrayOfString)
{
HelloWorld..MODULE$.main(paramArrayOfString);
}

public static final int $tag()
throws RemoteException
{
return HelloWorld..MODULE$.$tag();
}
}


But what the hell is a $tag() method? Well, I looked into the source code which had a comment on the $tag method which says



This method is needed for optimizing pattern matching expressions which match on constructors of case classes.  




Well, then what is the HelloWorld actually doing? Well, looks to me it was using the HelloWorld$ which was also generated by “scalac”. I cannot dig into what is  going on here, may be sometime in the future.



So far, what I understood is that “object” creates a final class whereas “class” creates a ScalaObject and methods inside would all be instance methods. So anything declared “object” can act only as a static-only container.



Lets do a simple bubble sort program in Scala. What should we be knowing to write a button sort?




  1. Assuming we pass the numbers to sort from command line, how do we convert strings to numbers ?


  2. How do we loop the array?



object BubbleSort
{
def main(ip_args: Array[String]) //we shall get the input numbers to sort into "args"
{
/*
we have a collection of strings, we should get a collection of numbers.
so we use the map which says for each i in the ip_args,
return the value after converting into expression. we get a 1 to 1 returned array.
*/
val args = ip_args.map { i => Integer.parseInt(i)}

/*
Looping : for index j which starts from 0 and ends at args.length-1 (inclusive)
*/
for(j <- 0 to args.length-1)
{
for(i <- 0 to args.length-2-j)
{
if(args(i) > args (i+1))
{
//we do an ascending order sort.
// Swap routine is shown belo.
val temp = args(i) //this is how we define variables in scala
args(i) = args(i+1)
args(i+1) = temp
}
}
}
//print all the numbers
for(i <- 0 to args.length-1)
println(args(i))
}
}


I do agree that even without the comments the code does not look as concise as it should be. But right now, we are just getting started – we would slowly look at how we can write concise code when we think functional programming (I am saying this with my past experience with Groovy and C# Closures – by functional, 90% of the time I mean closures – which I know is not accurate).I have also observed that when you compile the BubbleSort.scala, you end up getting more than one .class files - which I believe is for the anonymous method (closure) we used.



That's it! for this post. See you soon with functional programming using Scala!

3 comments:

zaly said...

thank for information is best site

Anonymous said...

Thanks! Please change blog's font-style, its hard to read.

octopusgrabbus said...

You can't learn everything. Years ago, I wanted to know everything I could about MFC (when it first came out). My assignments and duties took me away from it. I learned what I could. To me, adaptability and ability to learn is the most important thing.