Who knows/doesn't know it ?
Used it in prod, I mean
What do you think about it ?
this
is complex and causes many bugsThe World's Most Misunderstood Programming LanguageDouglas Crockford
map
, reduce
and filter
only recently arrived in the language"is a utility-belt library for JavaScript that provides a lot of the functional programming support [...] "
_.map, _.reduce, _.pluck, _.debounce, _.memoize
// lot of goodies inside
* : Almost, its fork Lo-Dash is better.
_.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
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...
# 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
# 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]
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)
# (.length) is a shortcut to access property
[1 2 3] |> filter (> 2) |> (.length) # → 1
# 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
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
# 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
npm install -g livescript
lsc
or livescript
lsc --watch