var f = function(x) { return x + 1; }
f(3);
//=> 4
f(x) = x + 1 #=> f(3) = 4
var arr = [1,2,3];
for (var i=0; i < arr.length; i++) {
arr[i] += 1;
}
// Arrays (native js)
[1, 2, 3].map(function(num) { return num + 1 });
//=> [2, 3, 4]
// Objects (lodash)
_.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
//=> [3, 6, 9]
var arr = [1,2,3];
var results = 0;
for (var i=0; i < arr.length; i++) {
results += arr[i];
}
// Array (native JS)
[1, 2, 3].reduce(function(memo, num) { return memo + num });
//=> 6
// Objects (lodash)
_.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
result[key] = num * 3;
return result;}, {});
//=> { 'a': 3, 'b': 6, 'c': 9 }
var list = [[0, 1], [2, 3], [4, 5]];
list.reduceRight(function(a, b){ return a.concat(b); });
//=> [4, 5, 2, 3, 0, 1]
var arr = [1,2,3,4,5];
var evens = []
for (var i=0; i<arr.length; i++) {
if (arr[i] & 2) {
evens.push(arr[i]);
}
}
// get evens
var arr = [1,2,3,4,5];
arr.filter(function(num){ return (num % 2 == 0) });
var arr = [1, 2, 3];
arr.forEach(function(x){
// do something outside of me
console.log(x);
});
var isOdd = true;
var arr = [1,2,5,7,9];
for (var i=0; i<arr.length; i++) {
if ((arr[i] & 1) === 0) {
isOdd = false;
break;
}
}
[1, 2, 3, 4, 5].every(function(item){ return item >= 3; });
//=> false
var hasOdd = false;
var arr = [1,2,5,7,9];
for (var i=0; i<arr.length; i++) {
if ((arr[i] & 1) === 1) {
hasOdd = true
break;
}
}
[1, 2, 3, 4].some(function(item){
return item >= 3;
});
var hello = function(name){ return "hi" + name; }
var place = function(place){ return place + "!"; }
var hi = _.compose(hello, place);
hi("world");
//=> hi world!
var book = function(name) {
console.log(name);
}
angular = book.bind(null, "AngularJS In Action");
angular();
var book = function(review) {
console.log(this.name+" is "+review);
}
var mybook = book.bind({name:'foo'}, "good!");
mybook();
var flow = [
getUrls,
getDocs,
clearUnlessES,
findUrls,
filterDupes,
saveUrls
].map(function(task){ return task.bind(null, stats); });
waterfall(flow, function(err) {
stats.end = Date.now();
stats.duration = stats.end-stats.start;
cb(err, stats);
});