Wednesday, February 2, 2011: CoffeeScripting @ Code Jom « from the old blog archive »

Last year, I used JavaScript at Code Jom competition. It was great, and I thought that I will use JavaScript in this year, but not using the old PHP+SpiderMonkey shell. I wanted to try something different, and I was really interested in Node.js and Narwhal.

However, I stumbled upon a question on StackOverflow asking about CoffeeScript, then I looked at CoffeeScript's home page.

When I saw the example CoffeeScript in that page, compared to the compiled JavaScript, I had a feeling that I want to try to use it. The code indeed looked a lot less messy! And because CoffeeScript works with Node.js (it is available as a Node.js package), I went on with Node.js.

First Round

The first round started after I first knew CoffeeScript for less than 1 day. A day before that, I ported my old library for use in Code Jom written in JavaScript into CoffeeScript and used it.

Surprisingly, I felt very comfortable. I felt like I still have all the power of JavaScript, but in a new, cleaner syntax. I finished the first round with a score of 9 (out of 14, ranked third).

Final Round

I prepared for the final round by familiarizing myself with CoffeeScript. I made a simple mashup webapp called Crew Standings in CoffeeScript and using jQuery and YQL.

Because I planned it a bit wrong and went on working on the main scope, things got messed up when I create functions as sometimes it use the variables outside the function's scope (because I used a variable name outside that scope.

But JavaScript (and CoffeeScript) is specialized in string handling, for example, when I need to get the first digit of a number, I can just convert the number to a string and use it. When I want to match a pattern from a string, I simply check the string if it matches a regular expression.

I couldn't do some questions that is based on some algorithm, where some others would be able to do it easily. For example, I was a bit too excited to be able to do the knapsack question.

Also, a lack of fixed multidimensional arrays in JavaScript made things a bit harder, my solution is to put it in an object:

price[p + '-' + m] = ......

But that looked ugly and less straightforward than built-in fixed multidimensional arrays like in C where you can int price[1000][1000];

However, I did it. My team got the first place for the student level.

The Library

I ported the last year's library to CoffeeScript, so that I don't have to learn all the new set of functions.

I used a simple hack for forcing functions into the global scope

(-> # force global scope

    @print = (x...) ->
        sys.print y + '\n' for y in x

).call null

Anything that got added to this will be added to the global scope, thanks to CoffeeScript, this.name can be called @name, so the above code added print function to the global scope.

Here is the full library script:

And a small example:

require './LIBRARY'

for testcase in [1..scanNumber()]
    n = scanNumber()
    print (n * (n + 1)) / 2