10 Reasons to Switch from CoffeeScript to LiveScript

5 Jul 2012 - Paul Miller

The following is a guest post by Paul Miller. You can submit a guest post as well, just send a pull request.

CoffeeScript has been around for a while and is now used by many developers and tools.

But why should someone switch from CoffeeScript to its fork, LiveScript? Here are ten reasons:

  1. Improved Readability

    LiveScript supports dashes in identifiers and mangles them to camelCase to keep consistent with JavaScript code style.

    document.query-selector-all

    is equivalent to and will compile to

    document.querySelectorAll

    As for numbers, they support underscores and suffixes:

    64_000km

    will be compiled simply to

    64000
  2. |> Pipe Operator

    It works with functions instead of methods on prototypes which allows to easily write your own modular functions for chains. For example:

    [1 2 3] |> map (* 2) |> sum #=> 10
    
  3. Standard Library

    prelude.ls - based off of Haskell's Prelude module. ECMAScript 5 array extras are nice, but they’re not enough. Plays awesomely with the pipe operator.

  4. Partially Applied Operators and Member Access

    This allows writing pure functional code without boilerplate:

    people |> map (.name) |> filter (in visitors)

    instead of

    people.map((man) -> man.name).filter((name) -> name in visitors)
  5. Constants

    const x = 10

    They are checked at compile-time for less errors. Works like ES6 const, but it compiles to var, because const isn’t supported greatly by engines today.

  6. Improved scoping

    = always declares variable in current scope, use := for redeclaration of outer scope variables, which reduces bugs.

    x = 1
    y = 1
    do ->
      x = 2
      y := 2
    x #=> 1
    y #=> 2
    
  7. Improved operators associativity

    unique pulls .length

    will execute unique and then get .length property of result, unlike in CoffeeScript, which requires brackets. And thus it is equivalent to

    unique(pulls).length

    this allows for parentheses free chaining

    unique node or not empty node

    compiles to

    unique(node) || !empty(node)
  8. Real, ECMAScript 6 / Python / Haskell-style flattened list comprehensions

    [x ** y for x in [10, 20] for y in [2, 3]]

    will create

    [100, 1000, 400, 8000]

    as it should.

  9. Curried functions

    (a, b, c) --> a * b * c

    instead of

    (a) -> (b) -> (c) -> a * b * c

    Very useful for functional caching of stuff.

  10. New idioms

    Async callbacks flattening syntax:

    error <- fs.write-file path, data
    alert error
    

    “Array of words” Ruby idiom:

    <[one two three]>

    vs

    ['one', 'two', 'three']

In case you’ve already written some CoffeeScript code, there’s a conversion guide.

If you decided to write a web application with LiveScript, I recommend you take a look at Brunch, which "just works" and supports LiveScript cleanly.

Interested in functional programming? Take a look at Functional Programming in JavaScript using LiveScript and prelude.ls.


For more on LiveScript and prelude.ls, .

comments powered by Disqus