But I recognize that, in the "real world", Java is king. With top popularity ratings, Java is simply what people build "industry" applications with. I have used Java, but exclusively in the classroom - I have never pursued it recreationally or professionally. I can tell you why in one elongated word: FactoryFactoryFactoryFactory...
Python is concise. In the words of The Nuts and Bolts of College Writing, "Concision is intimately connected to clarity." Java is verbose - it can be argued that the verbosity is for sound structural and theoretical reasons, but it is verbosity nonetheless.
And so I was intrigued when I ran across the assertion that Scala == Effective Java. Scala has all the power and resources of Java, but with minimal additional requirements (basically one library) adds a quality that I would have to describe as almost Pythonic.
Java and Python both have well-defined philosophies, but Java tends to enforce itself in an almost exaggerated manner while Python more elegantly encourages good style. Scala tries to give this quality to Java, and from my initial perusing it meets with a fair amount of success.
As an example, here is a simple extension of a "Hello World!" program. The idea is to write a "HelloArgs" program that says "Hello " followed by any command line arguments passed to it. Since these arguments are normally a list/vector/array of strings/chars/whatever, this requires a wee bit of parsing to compress it into one line and separate with spaces.
Here it is in Python (3.1):
A one-liner (at 80 columns, less here on the blog), reasonably sensible, though the idea of using a blank space and applying join to it might be a bit unintuitive at first. The list comprehension syntax is reasonable though. and better than this esoteric alternative:def HelloArgs(args): print('Hello ' + ' '.join([str(arg) for arg in args]))
This works and is fewer characters, but this isn't a game of Perl Golf here. Brevity is connected to clarity but you still need to have enough body to communicate your message.def HelloArgs(args): print('Hello ' + ' '.join(map(''.join, args)))
Now, here is an equivalent method in Scala:
Admittedly there are a few more lines, but this is actually a complete valid Scala application (the Python method above is just a method and would require working with a main() method or similar to run from the command line). It's simple, doesn't take too long to figure out ("mkString" is a way to "make strings" delineated by a given separator), and plays nicely in JVM land.object HelloArgs { def main(args : Array[String]) : Unit = { Console.println("Hello " + args.mkString(" ")) } }
Now really this barely scratches the surface, and that's because I don't know much about Scala yet. I am looking forward to learning more, and for those interested I highly suggest the Eclipse Scala IDE. I'm using the Helios version and it went perfectly smoothly, and I have to admit I do see a place for "robust" (if large) IDEs. I like doing Python in Vim of course, but that's another matter...
No comments:
Post a Comment