Stonefishy Blog - by Andrew Shi

Scribbles of a front and back ends developer

Give Me a Cup of Coffeescript

is programming language, which is built on top of JavaScript. CoffeeScript compiles into JavaScript that is efficient and consistent with many best practices. And it offers a clean syntax that should appeal to anyone who likes Python or Ruby.

Actually, The CoffeeScript is just a JavaScript. So you can use any exsiting JavaScript library seamlessly from CoffeeScript. And both client-side and server-side can be written by CoffeeScript. So CoffeeScript can write entire web application completely, not like JavaScript only write client-side.

The CoffeeScript is a Node.js package, so you need to install it with Node Package Manager. There assume you have installed node.js. You can watch here to see how to install node.js if you dont’ know it. Putting below commands to install CoffeeScript:

1
npm install -g coffee-script

CoffeeScript supports many commands to let you build your application. You just need to input below command line:

1
coffee -help

It will print awesome useful content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Usage: coffee [options] path/to/script.coffee -- [args]

If called without options, `coffee` will run your script.

  -b, --bare         compile without a top-level function wrapper
  -c, --compile      compile to JavaScript and save as .js files
  -e, --eval         pass a string from the command line as input
  -h, --help         display this help message
  -i, --interactive  run an interactive CoffeeScript REPL
  -j, --join         concatenate the source CoffeeScript before compiling
  -m, --map          generate source map and save as .js.map files
  -n, --nodes        print out the parse tree that the parser produces
      --nodejs       pass options directly to the "node" binary
      --no-header    suppress the "Generated by" header
  -o, --output       set the output directory for compiled JavaScript
  -p, --print        print out the compiled JavaScript
  -s, --stdio        listen for and compile scripts over stdio
  -l, --literate     treat stdio as literate style coffee-script
  -t, --tokens       print out the tokens that the lexer/rewriter produce
  -v, --version      display the version number
  -w, --watch        watch scripts for changes and rerun commands

Let’s give a example, how to use CoffeeScript to solve a mathematics problem. Here we will solve the first mathematical problem of Project Eluar. The problem is below:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

And now we solve this problem with CoffeeScript elegantly:

1
([1..999].filter (x) -> x % 3 is 0 or x % 5 is 0).reduce (x, y) -> x + y

You see, just one code line. It’s very concise and elegant. Not like JavaScript need to defined many variables and functions to solve it.

Cheers, I think you will love it.