Partiality *

Controversial? Perhaps; Opinionated? Certainly.

LiveScript: Haskell flavoured Javascript

Derived from [Coco](http://satyr.github.io/coco/) and very like [CoffeeScript](http://coffeescript.org/). Even comes with it's own prelude.
[LiveScript](http://livescript.net/) and its related [prelude.ls](http://www.preludels.com/) aren't exactly [Roy](http://roy.brianmckenna.org/) or [PureScript](http://purescript.readthedocs.org/en/latest/) but do bring in much needed Haskell flavour to JavaScript. No strong static types here; just some syntax, like currying, pattern matching etc., to mitigate some of the pain and boilerplate in the Javascript land.
## Past {#past}
There was a time when `Ecmascript` on the server side was a big part of how I earnt my living, much before the present `node.js` server side popularity. I still remember all those type hints done as embedded comments. These comments helped maintain sanity when LOC breached a certain boundary.

A simple but contrived version is the following greet function.

```javascript
function /*String*/ greet( /*String*/ name) {
    return "Hello, " + name + "!"
}
```
They are still needed but the boilerplate is minimal.

```livescript
# greet :: String -> String
greet = (name) -> "Hello #name!"
```

Compare it to some equivalent code in `Scala`; though in practice it isn't equivalent because it captures the types and therefore the compiler will disallow a class of possible misuses.

```scala
def greet : String => String = name => s"Hello $name"
//or
def greet2 = (name:String) => s"Hello $name"
```

Ah, yes! there are also the typed brothers-in-arms like [Dart](https://www.dartlang.org/) and [TypeScript](http://www.typescriptlang.org/) etc. but they aren't as good as Haskell, OCAML or Scala when it comes to type-systems and the boilerplate isn't reduced either. Flame anyone?

## Missing parts {#missing-parts}

How to set up Livescript and prelude on the client side isn't described anywhere. I had to pull in bits and pieces and do some trial and error to make things work in the end. The following part of an HTML document is where the magic happens.

```html
...
<script type="text/ls" src="test.ls"></script>
<script src="http://www.preludels.com/prelude-browser-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/livescript/1.2.0/livescript.min.js"></script>
...
```

It is a conscious decision to push everything to the client, i.e. there is no compile-to-js on the server side etc. The Livescript custom-script is sent, as is, to the client/browser and is interpreted after the Livescript is loaded.

The 'test.ls' is next.

```livescript
console ?.log 'LiveScript up'
{map, fold1} = require 'prelude-ls'
    
console.log(fold1 (+), [1 to 3])
    
# if jquery is available
#$ \h1 .find \img .prop \src, "bla"

console ?.log \end
```

This blog also uses Livescript. In order to try and beat the 1000ms for-mobile barrier we don't interpret Livescript in the browser. Instead we use grunt to compile, combine and minify the resulting Javascript.

## Frequent pain points or Gotchas {#gotchas}

Here are a few of the bits that annoyed every now and then.

1. `#` is for comments and for string interpolation.Combine that with css id selectors and you end up in trouble.

2. Function declaration and function application of parameter-less functions. Define `reset` and then call it `reset`. No! it has to be `reset()`.

3. If-statements without parentheses results in some unexpected behaviour.

On the whole, `Livescript` has been a better experience than `Coffeescript`.


## Further Reading {#readings}

### Presentation {#presentation}

* [Functional programming in JavaScript ecosystem](https://speakerdeck.com/paulmillr/functional-programming-in-javascript-ecosystem)

### Blog {#blog}

* [Functional Programming in JavaScript using LiveScript and prelude.ls](http://livescript.net/blog/functional-programming-in-javascript-using-livescript-and-prelude-ls.html)
* [Functional Programming in JavaScript using LiveScript - Part 2](http://livescript.net/blog/functional-programming-in-javascript-using-livescript-part-2.html)
* [10 Reasons to Switch from CoffeeScript to LiveScript](http://livescript.net/blog/ten-reasons-to-switch-from-coffeescript.html)