Graduating from CoffeeScript to LiveScript

CoffeeScript to the Haskell equals LiveScript

CoffeeScript is a nice syntactic alternative to JavaScript and, in the end, that’s all it is: syntax. CoffeeScript doesn’t offer anything not already found in JavaScript, no standard library nor any new operators. Looking for more in a compiles-to-JavaScript language, I stumbled across LiveScript.

Where CoffeeScript is a Ruby- and Python-inspired version of JavaScript, LiveScript is a Haskell-inspired version of CoffeeScript. Like CoffeeScript, LiveScript compiles to readable JavaScript about the same size as the original source file.

Here are a few reasons to explore LiveScript.

1. Accessor Functions

To create a function that returns an argument’s date property, JavaScript requires the following:

function(d) { return d.date; }

CoffeeScript shortens this somewhat:

(d) -> d.date

But LiveScript makes it trivial:

(.date)

2. Partial Functions

Creating functions is a core strength of LiveScript. To create a function that multiplies a number by 20 in JavaScript or CoffeeScript, you need to define a new function:

function(x) { return x * 20; }
(x) -> x * 20

In LiveScript, you can get the same result by partially applying a function or operator:

(* 20)

This is great if you want to apply the first few arguments, but what if you want to apply all the arguments except the first? LiveScript offers the underscore syntax for partially applying arbitrary arguments:

oncePerMinute = setInterval _, 60 * 1000

Functions passed to oncePerMinute will be passed as the underscored argument to setInterval.

3. Function Composition

CoffeeScript does not provide any syntax for function composition. LiveScript provides three operators. To create a function that passes an object’s date property to a function x, both JavaScript and CoffeeScript require defining functions:

function(d) { return x(d.date); }
(d) -> x(d.date)

In LiveScript, you can use forward compose, backward compose, or (like Haskell) the dot operator:

(.date) >> x
x << (.date)
x . (.date)

4. Cascades

So far I’ve demonstrated LiveScript’s fluency creating functions, but it offers a few other handy features, such as cascades. Cascades mimic the method chaining syntax commonly used in JavaScript and CoffeeScript, but rather than attaching to the previous value in the chain, each line attaches to the first link in the chain.

document.getElementById('svg')
  ..setAttribute('width', 100)
  ..setAttribute('height', 200)

This can be handy for libraries that do not support method chaining or when you want the original object rather than the result of calling the previous method.

prelude.ls

LiveScript is library agnostic, but unlike CoffeeScript, LiveScript has a standard library of functions designed to work well with the language. prelude.ls provides curried versions of map, filter, foldl, and many more functions you’d expect to find in a functional programming language.

Conclusion

I encourage you to give LiveScript a try. You might find you want to graduate from CoffeeScript to a more powerful language.