Coffee Script Premiere

36
Coffeescript Premiere Introduction to Coffeescript Samstag, 16. Oktober 2010

description

A short introductions of coffeescript, an expressive alternative to javascript. Some basic coffeescript examples and a nice Cakefile can be found here: http://github.com/9elements/jsconf.eu-2010/tree/master/canvas-vs-svg-vs-matrix3d/

Transcript of Coffee Script Premiere

Page 1: Coffee Script Premiere

CoffeescriptPremiere

Introduction to Coffeescript

Samstag, 16. Oktober 2010

Page 2: Coffee Script Premiere

What is Coffescript?

• Preprocessor for Javascript

• Alternative Syntax

• Embraces expressiveness

Samstag, 16. Oktober 2010

Page 3: Coffee Script Premiere

What is Coffescript?

• Coffeescript is a compiler

• A small DSL on Jison

• Available as Node.js Utility

Samstag, 16. Oktober 2010

Page 4: Coffee Script Premiere

Prequerities

• npm install coffee-script

• brew install coffee-script

• coffee --version

Samstag, 16. Oktober 2010

Page 5: Coffee Script Premiere

The Ingredients

Samstag, 16. Oktober 2010

Page 6: Coffee Script Premiere

Significant Whitespace

• Blocks = Indentation

• last evaluated object will be returned

Samstag, 16. Oktober 2010

Page 7: Coffee Script Premiere

Significant Whitespace

var cube, square;square = function(x) { return x * x;};cube = function(x) { return square(x) * x;};

Samstag, 16. Oktober 2010

Page 8: Coffee Script Premiere

Significant Whitespace

square = (x) -> x * x

cube = (x) -> square(x) * x

Samstag, 16. Oktober 2010

Page 9: Coffee Script Premiere

Assignment

number = 42opposite = true

Samstag, 16. Oktober 2010

Page 10: Coffee Script Premiere

Conditions

number = -42 if oppositenumber = 1337 unless noHacker

if happy and knowsIt clapsHands() chaChaCha()else showIt()

Samstag, 16. Oktober 2010

Page 11: Coffee Script Premiere

Switchswitch day when "Mon" then go work when "Tue" then go relax when "Thu" then go iceFishing when "Fri", "Sat" if day is bingoDay go bingo go dancing when "Sun" then go church else go work

Samstag, 16. Oktober 2010

Page 12: Coffee Script Premiere

Functions

square = (x) -> x * x

Samstag, 16. Oktober 2010

Page 13: Coffee Script Premiere

Arrays

list = [1, 2, 3, 4, 5]matrix = [ 1, 0, 1 0, 0, 1 1, 1, 0]

Samstag, 16. Oktober 2010

Page 14: Coffee Script Premiere

Objects

singers = { Jagger: "Rock", Elvis: "Roll" }math = root: Math.sqrt square: square cube: (x) -> x * square x

Samstag, 16. Oktober 2010

Page 15: Coffee Script Premiere

Array comprehension

for roid in asteroids for roid2 in asteroids when roid isnt roid2 roid.explode() if roid.overlaps roid

Samstag, 16. Oktober 2010

Page 16: Coffee Script Premiere

_ref = asteroids;for (_i = 0, _len = _ref.length; _i < _len; _i++) { roid = _ref[_i]; _ref2 = asteroids; for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { roid2 = _ref2[_j]; if (roid !== roid2) { if (roid.overlaps(roid2)) { roid.explode(); } } }}

Samstag, 16. Oktober 2010

Page 17: Coffee Script Premiere

Loops

if this.studyingEconomics buy() while supply > demand sell() until supply > demand

Samstag, 16. Oktober 2010

Page 18: Coffee Script Premiere

deliverEggs = function() { var _ref, _result2, dozen, i; _result2 = []; _ref = eggs.length; for (i = 0; (0 <= _ref ? i < _ref : i > _ref); i += 12) { _result2.push((function() { dozen = eggs.slice(i, i + 12); return deliver(new eggCarton(dozen)); })()); } return _result2;};

Samstag, 16. Oktober 2010

Page 19: Coffee Script Premiere

Try Catch

try allHellBreaksLoose() catsAndDogsLivingTogether()catch error print errorfinally cleanUp()

Samstag, 16. Oktober 2010

Page 20: Coffee Script Premiere

The Spice

Samstag, 16. Oktober 2010

Page 21: Coffee Script Premiere

Classclass Animal constructor: (@name) ->

move: (meters) -> alert @name + " moved " + meters + "m."

class Snake extends Animal move: -> alert "Slithering..." super 5

class Horse extends Animal move: -> alert "Galloping..." super 45

sam = new Snake "Sammy the Python"tom = new Horse "Tommy the Palomino"

sam.move()tom.move()

Samstag, 16. Oktober 2010

Page 22: Coffee Script Premiere

Open Classes

String::dasherize = -> @replace /_/g, "-"

Samstag, 16. Oktober 2010

Page 23: Coffee Script Premiere

Static Variables

String::dasherize = -> @replace /_/g, "-"

Samstag, 16. Oktober 2010

Page 24: Coffee Script Premiere

Fat Arrow

Account = (customer, cart) -> @customer = customer @cart = cart

$('.shopping_cart').bind 'click', (event) => @customer.purchase @cart

Samstag, 16. Oktober 2010

Page 25: Coffee Script Premiere

The Sugar

Samstag, 16. Oktober 2010

Page 26: Coffee Script Premiere

author = "Wittgenstein"quote = "A picture is a fact. -- #{author}"

String Interpolation

Samstag, 16. Oktober 2010

Page 27: Coffee Script Premiere

RegExp Interpolation

sep = "[.\\/\\- ]"dates = /\d+#{sep}\d+#{sep}\d+/g

Samstag, 16. Oktober 2010

Page 28: Coffee Script Premiere

Splats

race = (winner, runners...) -> print winner, runners

Samstag, 16. Oktober 2010

Page 29: Coffee Script Premiere

Existence

alert "I knew it!" if elvis?speed ?= 140lottery.drawWinner()?.address?.zipcode

Samstag, 16. Oktober 2010

Page 30: Coffee Script Premiere

Aliases

is compiles to ===isnt compiles to !==not compiles to !and compiles to &&or compiles to ||on, yes compile to trueoff, no compile to false

Samstag, 16. Oktober 2010

Page 31: Coffee Script Premiere

The Toolchain

Samstag, 16. Oktober 2010

Page 33: Coffee Script Premiere

Cakesys = require 'sys'fs = require 'fs'exec = require('child_process').execspawn = require('child_process').spawn

task 'watch', 'watches and compiles coffee', -> puts "Spawning coffee watcher" coffee = spawn 'coffee', ['-cwl', '-o', 'javascripts', 'coffeescripts']

[coffee].forEach (child) -> child.stdout.on 'data', (data) -> sys.print data exec "growlnotify -m \"#{data}\" -t \"Cakefile\"" child.stderr.on 'data', (data) -> sys.print data exec "growlnotify -m \"#{data}\" -t \"Cakefile\""

Samstag, 16. Oktober 2010

Page 34: Coffee Script Premiere

Rails3 Integration

• Barristahttp://github.com/Sutto/barista

• Bistro Carhttp://github.com/jnicklas/bistro_car

Samstag, 16. Oktober 2010

Page 35: Coffee Script Premiere

Resources

• IRC Channel#coffeescript on Freenode

• Official Websitehttp://jashkenas.github.com/coffee-script/

Samstag, 16. Oktober 2010

Page 36: Coffee Script Premiere

Thank you

• Sebastian Deutsch

• 9elements.com

• @sippndipp on Twitter

Samstag, 16. Oktober 2010