Sunday, January 30, 2011

JavaScript in Ten Minutes

JavaScript is a maligned language with a disadvantageous lineage. Hacked together for a limited purpose, rebranded because Java was cool (even though it's totally unrelated), and then stretched and used everywhere whether you want it or not. A good way to sum it up is with JavaScript in a Single Picture.

But despite these flaws, JavaScript is arguably the most commonly used programming language, at least in certain regards. Java may win in terms of official usage, but JavaScript is omnipresent and commonly the language that someone will first try "hacking around" with. And modern browsers have changed the rules - optimizing JavaScript is important and as a result it's actually possible to do some real things with it.

Adding client-side power and expressiveness to a webapp is very valuable, and so knowing at least enough JavaScript not to hurt anything is helpful. The trouble is that nondestructive coding is particularly hard with a language where variables are dynamically typed and by default global (e.g. unless you use var) and "==" comparisons coerce type (you want "===" for a true equivalency). It's incredibly easy to write JavaScript code - it's incredibly hard to write good JavaScript code.

A good place to start is JavaScript in Ten Minutes, which is in some ways almost a hyper-abbreviated version of JavaScript: The Good Parts in the picture linked earlier (the other book is JavaScript: The Definitive Guide which is more of a reference tome than a autodidactical tool). These are all fine resources, but since the first one is quick and free it's a nice place to start.

JavaScript in Ten Minutes gives a very quick overview of types and syntax and then proceeds to  give some nice attention to objects and prototypes. This includes some features that actually distinguish JavaScript in a nice way (the inspiration behind "The Good Parts" book title), where you can use prototyping to easily maintain a common property across many objects.

I won't go into much more detail as the whole point is the source itself is a quick read. If you have any interest in JavaScript I'd say it's worth your 10 minutes. And if you find yourself wanting more, I'd recommend either of the O'Reilly books I linked to above.

Sunday, January 23, 2011

More Scala - is it worth migrating from Java?

After recently playing with Scala, I discovered somebody who did so more methodically and tried it out for a year. He breaks down using Scala to a number of categories and rates them from 1-5. His conclusion:
To summarise Scala evaluation, this is a scores matrix for all categories (1 - poor, 2 - fair, 3 - good, 4 - very good, 5 - excellent).

  • Programming language - very good (4)
  • Testing - very good (4)
  • Performance - very good (4)
  • Tools - fair (2)
  • Language extensions - good (3)
  • Interoperability - excellent (5)
  • Monitoring and maintenance  - good (3)
  • Support - good (3)
  • Scala skills - poor (1)
A good read, especially if your curiosity was at all piqued by Scala from my entry last week. I very much agree that, while pushing Scala for "enterprise" is a bit risky (though at least moderately feasible due to the interoperability), it is definitely a worthy tool for personal use.

Sunday, January 16, 2011

Scala - concise Java?

As evidenced by my projects on Google Code, Python is my tool of choice for general programming. Python has a strong ecosystem and good technical underpinnings, but is also genuinely intuitive and even expressive. My pseudocode is closer to Python than any other language.

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):
def HelloArgs(args):
  print('Hello ' + ' '.join([str(arg) for arg
                             in args]))
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(map(''.join,
                                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.

Now, here is an equivalent method in Scala:
object HelloArgs {
  def main(args : Array[String]) : Unit = {
    Console.println("Hello " +
                    args.mkString(" "))
  }
}
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.

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...

Sunday, January 9, 2011

IPv6 - Now with Nethack

Oil. Uranium. Coal.

There are many limited resources in the world, but with all our gadgetry we have now created a new one - IPv4 addresses.

Our numeric friends which help all our technology communicate are in fact a rapidly disappearing commodity - less than 3% remain to be allocated, with an estimated depletion between June and December this year.

Of course you may already have heard that - the cries of IPv4 end-times have rung for some time. People have been content to ignore them as long as the status quo works, even though we have a perfectly suitable alternative that we really ought to start using. That is, IPv6.

IPv4 was invented many years ago, before the potential of the modern internet was realized. As such, IPv6 offers a number of enhancements - better mobile support, mandatory security, larger packets, and so forth. But the most obvious difference, and the raison d'etre,  is that it supports a much larger address space. That is to say, you can have more of them.

This of course makes them longer and lets them use more characters (letters). Here's an IPv6 address: 2a00:1018:801:1000::4d3f:a9c8

Note that this address is in fact abbreviated. There are rules allowing you to omit leading 0's from IPv6 addresses. The full address being represented above is: 2a00:1018:0801:1000:0000:0000:4d3f:a9c8

An IPv6 address consists of 8 groups of four characters (16 bits), totaling 128 bits and allowing for approximately 3.4 x 10^38 addresses. IPv4 addresses are 32 bits and allow for approximately 4.3 x 10^9 addresses.

In other words, there are already more human beings than IPv4 addresses, while there are more IPv6 addresses than there are stars in the observable universe. As an aside, according to current estimates there are actually still more atoms in the universe than IPv6 addresses.

Of course, if you read the title of this post you're probably wondering "what about Nethack?". Well, the IPv6 address I used as an example is actually pointed at a Debian VPS that I fought long and hard with that now serves Nethack (much like nethack.alt.org). Just telnet to it and you'll be greeted with Nethack, and you can be one of the first to play this classic game in a brave new internet realm.

But those long addresses are a pain to bandy about, so thankfully DNS works just peachy in IPv6 as well. I assigned nethack.proles.net to be the above IPv6 address, so if you have IPv6 access you can telnet right to that and be on your way.

Now you probably don't actually have IPv6 access. If you're on *nix you're in luck - Miredo is easy to install and configure and gets you IPv6 access without even needing to set up an account. Otherwise, I'd suggest Tunnelbroker. It'll take some doing, but you'll be on the forefront of where the internet is going (or at least needs to go).

And that's that - I'll close by just saying that the layers of geekery inherent in Nethack over IPv6 bring me great joy. Thanks for reading!

Addendum - nethack.fi also supports IPv6 (and IPv4, whereas my offering is IPv6 only). Oh well, still cool to add something to the IPv6 world.

Sunday, January 2, 2011

A year in R

Tis the season for retrospectives, and I happened across a particularly appropriate one for statistical programming - R in 2010.

In a nutshell, R is an open-source object-oriented functional programming language intended for statistical applications and based on S (a product from Bell Labs in the 70s). Over the past decade it has become the de facto standard for statisticians (e.g. people actually in stats departments), as well as enjoyed increasing use in biostatistics, computer science, and the social sciences.

The most interesting part of the linked post is the first section listing the top 14 R-related posts from 2010. This gives a good idea of the range of tasks R can be used for, from publishing quality graphics to data mining to game theory (Prisoner's dilemma).

Personally I've most used R in combination with Sweave/LaTeX ("literate programming") to simultaneously perform statistical analysis and typeset the report (potentially even automating the process). This guide looks interesting, though it adds Eclipse to the mix (I find Emacs Speaks Statistics to be the best way to use R).

Of course if all of the above sounds like gibberish to you and you just want to get started, there's a bunch of resources for that too. R is a powerful, well-supported, and surprisingly expressive language and I would advocate for anybody considering statistics beyond basic summaries (e.g. means and the like). It has a steep learning curve (the interactive prompt "command line") but can be made gentler by installing GUI plugins such as R Commander.

All in all, a good year for a good software package.