coffee script premiere

Post on 07-Apr-2015

906 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

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

CoffeescriptPremiere

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

square = (x) -> x * x

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

Samstag, 16. Oktober 2010

Assignment

number = 42opposite = true

Samstag, 16. Oktober 2010

Conditions

number = -42 if oppositenumber = 1337 unless noHacker

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

Samstag, 16. Oktober 2010

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

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 errorfinally cleanUp()

Samstag, 16. Oktober 2010

The Spice

Samstag, 16. Oktober 2010

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

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

The Sugar

Samstag, 16. Oktober 2010

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

String Interpolation

Samstag, 16. Oktober 2010

RegExp Interpolation

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

Samstag, 16. Oktober 2010

Splats

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

Samstag, 16. Oktober 2010

Existence

alert "I knew it!" if elvis?speed ?= 140lottery.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 trueoff, no compile to false

Samstag, 16. Oktober 2010

The Toolchain

Samstag, 16. Oktober 2010

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

Rails3 Integration

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

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

Samstag, 16. Oktober 2010

Resources

• IRC Channel#coffeescript on Freenode

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

Samstag, 16. Oktober 2010

Thank you

• Sebastian Deutsch

• 9elements.com

• @sippndipp on Twitter

Samstag, 16. Oktober 2010

top related