Victus Spiritus

home

Wakeup with a fresh cup of CoffeeScript

06 May 2011

I like programming languages with rich object models. I love dynamic languages. Sure, minimizing execution time and resource optimization may demand speed, and c variants and java (mirah!) are screamers. Yet after having coded heavily in one of these languages for years, it's a breath of fresh air letting go of the training wheels of type safety and discarding the mental overhead of complex inheritance trees. Verbosity be damned!

An object is the interface it defines.

Ruby, Python and Javascript have all sparked my imagination as incredibly flexible dynamic languages. They've also inspired a generation of delightful framework and tool builders. But while learning their syntax, object models, and functional features, it bugged me that JavaScript missed out on some of the syntactical sugar of its dynamic kin.

It bothered another gentleman far more, and from his frustration was born a language of beauty. Jeremy Ashkenas crafted and continues to improve CoffeeScript, which is actually a transliteration of JavaScript (to which it cleanly compiles) combined with a utility library. I'm having a great time learning the language with Trevor Burnham's CoffeeScript book, published by the Pragmatic Bookshelf.

CoffeeScript

square = (x) -> x * x

math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x

list = [1, 2, 3, 4, 5]

cubes = (math.cube num for num in list)

and the equivalent JavaScript:

var cubes, list, math, num, square;

square = function(x) {
  return x * x;
};
list = [1, 2, 3, 4, 5];
math = {
  root: Math.sqrt,
  square: square,
  cube: function(x) {
    return x * square(x);
  }
};

cubes = (function() {
  var _i, _len, _results;
  _results = [];
  for (_i = 0, _len = list.length; _i < _len; _i++) {
    num = list[_i];
    _results.push(math.cube(num));
  }
  return _results;
})();

Another example: (source)

restaurants = [‘Fridays’, ‘Applebees’, ‘Chilis’]
for restaurant in restaurants alert restaurant

//compared to JavaScript

var restaurant, restaurants, _i, _len;
restaurants = [‘Fridays’, ‘Applebees’, ‘Chilis’]
for (_i = 0; _len = restaurants.length; _len < _i; _i++) {
  restaurant = restaurants[_i];
  alert(restaurant);
}

One more example, the Y Combinator in JavaScript and coffeescript (thanks niko)

Installation

Note that with node.js, nodemon and a configuration file you can have coffee files automatically compiled to JavaScript on save for interpretation. I found nvm a great configuration tool for keeping my versions of node straightI opted to manually install and maintain node versions for the time being. I had issues with where nvm was installing files and some conflicts with npm.

The following installation instructions are from Jashkenas github linked above:

Installation and Usage

The CoffeeScript compiler is itself written in CoffeeScript, using the Jison parser generator. The command-line version of coffee is available as a Node.js utility. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser (see “Try CoffeeScript”, above).

To install, first make sure you have a working copy of the latest stable version of Node.js, and npm (the Node Package Manager). You can then install CoffeeScript with npm:

npm install -g coffee-script
(Leave off the -g if you don’t wish to install globally.)

If you’d prefer to install the latest master version of CoffeeScript, you can clone the CoffeeScript source repository from GitHub, or download the source directly. To install the CoffeeScript compiler system-wide under /usr/local, open the directory and run:

sudo bin/cake install
If installing on Ubuntu or Debian, be careful not to use the existing out-of-date package. If installing on Windows, your best bet is probably to run Node.js under Cygwin.

Once installed, you should have access to the coffee command, which can execute scripts, compile .coffee files into .js, and provide an interactive REPL.

Notes: