Monday, August 29, 2011

Scala quicksort

Here's a version of quicksort in Scala that I took from the excellent "Scala by Example" pdf from the scala website. The main algorithm is intact; I just thought it would be nice to get a print out of the steps being performed. So basically the program has been changed to include a few extra variables so that their value can be printed to the console. The unsorted input array has been hard-coded into the program just to avoid writing additional code to parse input arguments.

If you have scala installed, you can copy and paste the code to a file, compile it (scalac filename), run (scala compiledfilename) and see the output. Enjoy!


Sunday, August 28, 2011

Scala, Python and language bias

The wonderful Scala by Example pdf by the creator Martin Odersky himself has a quicksort program written in Scala. Its a beautiful piece of succinct code that lays clearly the quicksort algorithm. I started digging into Scala a couple of months back and being a lover of Python, the fact that it didnt need semicolons for indentation was a pleasure to see. As I dug into it further, it seemed I was learning some very new concepts, which I hadnt learned in a while sticking to languages like Java, .NET and even Python. The concept of Actors, Case Classes with pattern matching, parametric classes, variances to name a few were extremely delightful to learn.

One more thing that was really interesting to learn about was the type system. Coming from languages where the concept of types exposed were mainly primitive types and objects, the concept of a type being much more than that was eye-opening.

I have also heard a lot of good things about Clojure, a lisp that runs on the jvm. Just like people gravitated towards Python or Ruby when they became popular, it seems like the same thing is happening with Scala and Clojure. I discovered Python before Ruby and when exploring Python I was so enamoured with the functionality, the ease of programming and the productivity that when I looked at Ruby, it had to be something a lot more than Python for it to catch my eye.

So one could say I was looking at Ruby with a "language(python) bias". So that was mainly what nudged me into the Python camp. I see the same happening with Scala now. I tried it out first, and it seems like it could teach me so much that I would like to be in the Scala camp for now. And "for now" has a good chance of being "forever".