Seattle.rb 6.4

468 views
406 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
468
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Seattle.rb 6.4

  1. 1. 5 things I learnedabout coffeescript(in my year with it)Wednesday, June 5, 13
  2. 2. Dean Hudson, @deaneroSenior Engineer, Super Secret Music Start Up(and hiring!)Who Am I?Wednesday, June 5, 13
  3. 3. 1. coffee -cpWednesday, June 5, 13
  4. 4. dean@hdh:~/Devel/coffee$ coffee --helpUsage: 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
  5. 5. to see that this...Wednesday, June 5, 13
  6. 6. qsort = (ar) ->return ar unless ar.length > 1pivot = ar.pop()less = []more = []for val in arif val < pivotless.push(val)elsemore.push(val)qsort(less).concat([pivot], qsort(more))module.exports = qsortWednesday, June 5, 13
  7. 7. ...compiles to this:Wednesday, June 5, 13
  8. 8. dean@hdh:~/Devel/coffee$ coffee -cp qsort.coffee// Generated by CoffeeScript 1.4.0(function() {var qsort;qsort = function(ar) {var less, more, pivot, val, _i, _len;if (!(ar.length > 1)) {return ar;}pivot = ar.pop();less = [];more = [];for (_i = 0, _len = ar.length; _i < _len; _i++) {val = ar[_i];if (val < pivot) {less.push(val);} else {more.push(val);}}return qsort(less).concat([pivot], qsort(more));};module.exports = qsort;}).call(this);dean@hdh:~/Devel/coffee$Wednesday, June 5, 13
  9. 9. 2. Program withclassWednesday, June 5, 13
  10. 10. Wednesday, June 5, 13
  11. 11. (me, 16 months ago)Wednesday, June 5, 13
  12. 12. There’s only one way to do it.It reduces mental overhead for devs.It just works.Because! Withclass...Wednesday, June 5, 13
  13. 13. __extends = function(child, parent) {for (var key in parent) {if (__hasProp.call(parent, key))child[key] = parent[key];}function ctor() {this.constructor = child;}ctor.prototype = parent.prototype;child.prototype = new ctor();child.__super__ = parent.prototype;return child;};Wednesday, June 5, 13
  14. 14. __extends = function(child, parent) {for (var key in parent) {if (__hasProp.call(parent, key))child[key] = parent[key];}function ctor() {this.constructor = child;}ctor.prototype = parent.prototype;child.prototype = new ctor();child.__super__ = parent.prototype;return child;};Tricksy! And...Wednesday, June 5, 13
  15. 15. // Generated by CoffeeScript 1.4.0(function() {var Greet, Hello,__hasProp = {}.hasOwnProperty;Greet = (function() {function Greet() {}Greet.prototype.greet = function() {return hi;};return Greet;})();Hello = (function(_super) {__extends(Hello, _super);function Hello() {return Hello.__super__.constructor.apply(this, arguments);}Hello.prototype.hello = function() {return hello;};return Hello;})(Greet);}).call(this);Wednesday, June 5, 13
  16. 16. // Generated by CoffeeScript 1.4.0(function() {var Greet, Hello,__hasProp = {}.hasOwnProperty;Greet = (function() {function Greet() {}Greet.prototype.greet = function() {return hi;};return Greet;})();Hello = (function(_super) {__extends(Hello, _super);function Hello() {return Hello.__super__.constructor.apply(this, arguments);}Hello.prototype.hello = function() {return hello;};return Hello;})(Greet);}).call(this);...verboseWednesday, June 5, 13
  17. 17. vs.Wednesday, June 5, 13
  18. 18. class Greetgreet: -> hiclass Hello extends Greethello: -> helloWednesday, June 5, 13
  19. 19. 3. Usecomprehensionidioms!!!!!Wednesday, June 5, 13
  20. 20. # whats wrong with this code?_ = require underscorear = [1,2,3,4,5,6,7,8,9]doubled = _.map(ar, (x) -> x * 2)Wednesday, June 5, 13
  21. 21. doubled = i * 2 for i in arYOU DON’T NEEDA LIBRARY!!!Wednesday, June 5, 13
  22. 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. 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. 24. ar = [1,2,3,4,5,6,7,8,9,10]aSelect = i for i in ar when (i % 2) == 0aMap = i * 2 for i in araFind = (i for i in ar when i == 0)[0]Similarly...Wednesday, June 5, 13
  25. 25. 4. One class per fileWednesday, June 5, 13
  26. 26. Node.js provides a synchronous requireStitch and browserify use node semanticsRequire.js does AMDHow? A library.Wednesday, June 5, 13
  27. 27. 5. Watch yourreturn values.Wednesday, June 5, 13
  28. 28. dontRunInATightLoop = (object) -># I meant to mutate this object and return void...object[k] = "#{v}-derp" for k, v of objectThis...Wednesday, June 5, 13
  29. 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
  30. 30. Comprehensions+Implicit returnsWednesday, June 5, 13
  31. 31. =many allocationsWednesday, June 5, 13
  32. 32. okayToRunInATightLoop = (object) ->object[k] = "#{v}-derp" for k, v of object# I need to do it explicitlyreturnWednesday, June 5, 13
  33. 33. Questions?Wednesday, June 5, 13
  34. 34. Thanks!@deanerohttp://ranch.ero.com(I’m hiring Rubyists!)Wednesday, June 5, 13

×