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:
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
|>
Pipe OperatorIt 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
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.
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)
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.
=
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
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)
[x ** y for x in [10, 20] for y in [2, 3]]
will create
[100, 1000, 400, 8000]
as it should.
(a, b, c) --> a * b * c
instead of
(a) -> (b) -> (c) -> a * b * c
Very useful for functional caching of stuff.
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, follow @gkzahariev.
comments powered by Disqus