LiveScript presentation – by Khalid Jebbari – November 26th 2013



LiveScript presentation – by Khalid Jebbari – November 26th 2013

0 0


presentation-livescript

Presentation of LiveScript during Functional Programmers Paris Meetup November 26th 2013

On Github DjebbZ / presentation-livescript

LiveScript presentation

by Khalid Jebbari

November 26th 2013

@Dj3bbZ

But : présenter LiveScript à des adeptes du fonctionnel pour avoir leur avis. Pas encore utilisé en prod, mais utilisé CoffeeScript

Javascript anyone ?

Who knows/doesn't know it ?

Used it in prod, I mean

What do you think about it ?

A trip down memory lane...

Problems with Javascript

  • Lots of WTF
  • this is complex and causes many bugs
  • Missing API's : collections, function manipulations, objects/prototypes
The World's Most Misunderstood Programming Language Douglas Crockford Lisp in C clothing ; community recently understood its functional power

The functional side of Javascript

  • Functions as first-class citizens
  • map, reduce and filter only recently arrived in the language
  • But not available in all browsers (IE <= 8)
  • Where's the rest ?

Underscore.js ?

  • "is a utility-belt library for JavaScript that provides a lot of the functional programming support [...] "
  • Cross-browser*
  • The "Most Depended Upon" lib on npm
_.map, _.reduce, _.pluck, _.debounce, _.memoize
// lot of goodies inside
* : Almost, its fork Lo-Dash is better.

Underscore, but...

  • Not that functional : currying ? order of arguments ? composition ? extensibility ? Check this talk (and the slides)
_.curry() // doesn't exist, even though _.partial exists
_.map(collection, fun) // why not the opposite order ?
_.compose(*functions) // not that useful because of previous points
					

CoffeeScript, maybe ?

# CoffeeScript
f = (a, b) ->
	a + b
// Resulting Javascript
var f = function(a, b) {
	return a + b;
};
					

Tries to push a more functional style, but doesn't go far enough...

Some CoffeeScript's features : - all functions return their last statement - almost everything is an expression - for comprehensions - default arguments - object desctructuring - sugar for classes (bof...)

Enter LiveScript

  • Child of CoffeeScript
  • Shares most of its syntax
  • Same semantics than Javascript as well
  • Inspired mainly by Haskell (yes!) and F# (yes!), with a bit of LISP (yes!)
  • Full of functional idioms

Features highlight

Personal selection. LiveScript has *A LOT*

  • Easy currying
  • Partial application
  • Function composition
  • Pipes ! Unix/F#-like
  • Pattern matching
  • Backcalls
  • Prelude.ls : an underscore-like shipped with LiveScript, very rich

Show me some code, dude !

Easy currying of user-defined functions

# No currying
f = (a, b) -> a + b

# Easy currying, with double arrow -->
f = (a, b) --> a + b
g = f 1 # curried f
g 2 # → 3

Partial application

A.k.a how to solve one of Underscore's problems

# using _ as a placeholder argument for data
# Operators as functions (yes!)
biggerThanThree = _.map _, (> 3)

# [1 2 3 4 5] will be used in place of _
biggerThanThree [1 2 3 4 5] // → [true,true,false,false,false]
					

Function composition

yes!

# Forward composition
add-two-times-two = (+ 2) >> (* 2)
add-two-times-two 3 # → (3+2)*2 → 10

# Backward composition
times-two-add-two = (+ 2) << (* 2)
times-two-add-two 3 # → (3*2)+2 → 8

# Haskell style, equivalent to <<
times-two-add-two = (+ 2) . (* 2)
					
Le highlighter ne fonctionne pas encore avec LiveScript, un patch est en cours

Pipes !

Unix/F# style. Avoids nesting calls

# (.length) is a shortcut to access property
[1 2 3] |> filter (> 2) |> (.length) # → 1
					

Pattern matching

# empty function from Prelude.ls (coming after)
# Guard syntax (|) translates to switch in JS
sum = ([x, ...xs]:list) ->
	| empty list => 0
	| empty xs   => x
	| otherwise  => x + sum xs
					
Est-ce vraiment du pattern matching ?

Backcalls

A way to avoid pyramid of callbacks in async programming

x <- map _, [1 to 3]
x * 2
# → [2,4,6]
# translates to
map(function(x){
  return x * 2;
}, [1, 2, 3]);

# the ! suppresses the return of the function
data <-! $.get 'my-ajax-url.com'
$ '.result' .html data
					

Prelude.ls

The missing Javascript API's, shipped with LiveScript

What you would expect from a functional language
# 60+ functions for arrays
head, first, tail, last, compact, partition, concat, intersection, scan, take-while, zip, ...

# ~15 functions for objects (maps)
keys, values, pairs-to-obj, reject, find, ...

# ~20 functions for strings
split, join, words, unwords, reverse, repeat, ...

# 30+ functions for Math (yes!)
min, max, tau, odd, even, quot, rem, sin, signum, is-it-NaN

# of course, functions functions
apply, curry, flip, fix
					

Prelude.ls (part 2)

  • Easy to install : npm install -g livescript
  • Has a CLI and a REPL : lsc or livescript
  • Has a watcher, that compiles .ls to .js on change : lsc --watch
  • Produces readable Javascript

That's all folks !

Questions ?

J'ai une question pour les Haskellers,F-shapriens,Scalariés : 1) Le manque de typage statique vous gène-t-il ? 2) Que pensez-vous de LiveScript wrt/Javascript ? 3) Fay ?