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:
is equivalent to and will compile to
As for numbers, they support underscores and suffixes:
will be compiled simply to
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
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)
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
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
unique and then get
.length property of result, unlike in CoffeeScript, which requires brackets. And thus it is equivalent to
this allows for parentheses free chaining
unique node or not empty node
unique(node) || !empty(node)
[x ** y for x in [10, 20] for y in [2, 3]]
[100, 1000, 400, 8000]
as it should.
(a, b, c) --> a * b * c
(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]>
['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.
For more on LiveScript and prelude.ls, follow @gkzahariev.comments powered by Disqus