Sie sind auf Seite 1von 36

Coffeescript

Premiere

Introduction to Coffeescript

Samstag, 16. Oktober 2010

What is Coffescript?

Preprocessor for Javascript Alternative Syntax

Embraces expressiveness

Samstag, 16. Oktober 2010

What is Coffescript?

Coffeescript is a compiler

A small DSL on Jison

Available as Node.js Utility

Samstag, 16. Oktober 2010

Prequerities

npm install coffee-script

brew install coffee-script

coffee --version

Samstag, 16. Oktober 2010

The Ingredients

Samstag, 16. Oktober 2010

Significant Whitespace

Blocks = Indentation

last evaluated object will be returned

Samstag, 16. Oktober 2010

Significant Whitespace

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

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

};

Samstag, 16. Oktober 2010

Significant Whitespace

Samstag, 16. Oktober 2010

square = (x) -> x * x

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

Assignment

number

= 42

opposite = true

Samstag, 16. Oktober 2010

Conditions

number = -42 if opposite number = 1337 unless noHacker

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

Samstag, 16. Oktober 2010

Switch

switch 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

Functions

square = (x) -> x * x

Samstag, 16. Oktober 2010

Arrays

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

]

Samstag, 16. Oktober 2010

Objects

singers = { Jagger: "Rock", Elvis: "Roll" } math =

root:

Math.sqrt

square: square

cube:

(x) -> x * square x

Samstag, 16. Oktober 2010

Array comprehension

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

Samstag, 16. Oktober 2010

_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

Loops

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

Samstag, 16. Oktober 2010

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

Try Catch

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

Samstag, 16. Oktober 2010

Samstag, 16. Oktober 2010

The Spice

Class

class 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

Open Classes

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

Samstag, 16. Oktober 2010

Static Variables

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

Samstag, 16. Oktober 2010

Fat Arrow

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

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

Samstag, 16. Oktober 2010

Samstag, 16. Oktober 2010

The Sugar

String Interpolation

author = "Wittgenstein"

quote

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

Samstag, 16. Oktober 2010

RegExp Interpolation

Samstag, 16. Oktober 2010

= "[.\\/\\- ]"

sep

dates = /\d+#{sep}\d+#{sep}\d+/g

Splats

race = (winner, runners print winner, runners

Samstag, 16. Oktober 2010

) ->

Existence

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

Samstag, 16. Oktober 2010

Aliases

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

Samstag, 16. Oktober 2010

The Toolchain

Samstag, 16. Oktober 2010

Editor

TextMate bundle

VIM

Samstag, 16. Oktober 2010

Cake

sys = require 'sys' fs = require 'fs' exec = require('child_process').exec spawn = 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

Rails3 Integration

Barrista

Samstag, 16. Oktober 2010

Resources

IRC Channel #coffeescript on Freenode

Samstag, 16. Oktober 2010

Thank you

Sebastian Deutsch

9elements.com

@sippndipp on Twitter

Samstag, 16. Oktober 2010