4. dean@hdh:~/Devel/coffee$ coffee --help
Usage: coffee [options] path/to/script.coffee -- [args]
[...]
-c, --compile compile to JavaScript and save as .js files
-p, --print print out the compiled JavaScript
[...]
Wednesday, June 5, 13
6. qsort = (ar) ->
return ar unless ar.length > 1
pivot = ar.pop()
less = []
more = []
for val in ar
if val < pivot
less.push(val)
else
more.push(val)
qsort(less).concat([pivot], qsort(more))
module.exports = qsort
Wednesday, June 5, 13
20. # what's wrong with this code?
_ = require 'underscore'
ar = [1,2,3,4,5,6,7,8,9]
doubled = _.map(ar, (x) -> x * 2)
Wednesday, June 5, 13
21. doubled = i * 2 for i in ar
YOU DON’T NEED
A LIBRARY!!!
Wednesday, June 5, 13
22. // Generated by CoffeeScript 1.4.0
(function() {
var ar, i, map, _i, _len;
ar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (_i = 0, _len = ar.length; _i < _len; _i++) {
i = ar[_i];
map = i * 2;
}
}).call(this);
Wednesday, June 5, 13
23. // Generated by CoffeeScript 1.4.0
(function() {
var ar, i, map, _i, _len;
ar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (_i = 0, _len = ar.length; _i < _len; _i++) {
i = ar[_i];
map = i * 2;
}
}).call(this);
This is also faster.
Wednesday, June 5, 13
24. ar = [1,2,3,4,5,6,7,8,9,10]
aSelect = i for i in ar when (i % 2) == 0
aMap = i * 2 for i in ar
aFind = (i for i in ar when i == 0)[0]
Similarly...
Wednesday, June 5, 13
28. dontRunInATightLoop = (object) ->
# I meant to mutate this object and return void...
object[k] = "#{v}-derp" for k, v of object
This...
Wednesday, June 5, 13
29. dontRunInATightLoop = function(object) {
var k, v, _results;
_results = [];
for (k in object) {
v = object[k];
_results.push(object[k] = "" + v + "-derp");
}
return _results;
};
...compiles to this.
Wednesday, June 5, 13