Created by Breno Ferreira / @breno_ferreira
Do I need one?
Of course not!
For that simple pleasure of being a superior programmer, and look down at normal developers in their caves banging their heads together with their for loops and mutable state just to create fire.
It will blow your mind!
var inc = function(x) {
return x + 1;
}
An expression is said to be referentially transparent if it can be replaced with its value without changing the behavior of a program (in other words, yielding a program that has the same effects and output on the same input).
var swap = function(ref x, ref y) {
var aux = x;
x = y;
y = x;
}
Obs: pseudo-code
var swap = function(x, y) {
return [y, x];
}
function f(obj){
obj.prop = "some different value";
}
Prefer immutability!
function f(obj){
var newFunction = function(){
....
};
obj.prototype.someFunction = newFunction;
}
Do I need to say why?
document.whatever();
$('element').whatever();
var ajax = new XMLHttpRequest();
console.log();
They are all undesirable side-effects!
Use when appropriate, in the right places!
In order to understand recursion, one must first understand recursion.
function factorial(n){
if(n === 0 || n === 1) return 1;
else return n * factorial(n-1);
}
function factorial(n){
var factImpl = function(x, result){
if(x === 0 || x === 1) return result;
else return factImpl(x-1, result * x);
};
return factImpl(n, 1);
}
Tzu-li and Tzu-ssu wereboasting about the size of their latest programs.
‘Two-hundred thousand lines,’ said Tzu-li, ‘not counting comments!’
Tzu-ssu responded, ‘Pssh, mine is almost a million lines already.’
Master Yuan-Ma said, ‘My best program has five hundred lines.’
Hearing this, Tzu-li and Tzu-ssu were enlightened.
f : A → B
That means: f is a function that takes a value of type A, and returns a value of type B
f : Number → String
function toString(n){
return n.toString();
}
toString(1);
f : (A → B) → B
map: (Array<A>, (A → B)) → Array<B>
var array = [1,2,3,4,5];
var result = [];
for(var i = 0; i < array.length; i++){
var n = array[i];
result.push(n * 2);
}
return result;
var array = [1,2,3,4,5];
array.map(function(n){ return n * 2; });
filter: (Array<A>, (A → Boolean)) → Array<A>
var array = [1,2,3,4,5];
var result = [];
for(var i = 0; i < array.length; i++){
var n = array[i];
if(n % 2 === 0){
result.push(n);
}
}
return result;
var array = [1,2,3,4,5];
array.filter(function(n){ return n % 2 === 0; });
reduce: (Array<A>, (A, B) → B, init:B) → B
var array = [1,2,3,4,5];
var result = 1;
for(var i = 0; i < array.length; i++){
var n = array[i];
result *= n;
}
return result;
var array = [1,2,3,4,5];
array.reduce(function(n, acc){ return n * acc; }, 1);
var sort = function(array){
if(array.length === 0 || array.length === 1) return array;
var head = array[0];
var tail = array.slice(1);
var smaller = sort(tail.filter(function(n){ return n <= head;}));
var bigger = sort(tail.filter(function(n){ return n > head;}));
return smaller.concat(head).concat(bigger);
}
October 2014
TBA
Available for free online