-
-
Save jamesrcounts/2968512 to your computer and use it in GitHub Desktop.
NDC 2011 - Wake Up and Smell The Coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file contains brushed up version of all the examples [Anders Norås] programmed live during the talk. | |
# CoffeeScript compiles to JavaScript | |
numbers = [1,2,3,4] # No need for semicolons! | |
location = conference = "NDC 2011" # Multiple assigns. | |
awake = false | |
console.log location # No need for parens! | |
# Run, Build | |
print = console.log # Let's alias for convenience, shall we? | |
track = 6 | |
if track == 6 # == actually compiles to === to avoid that all too common JS mistake! | |
print "All is good" | |
else | |
print "You're missing out" | |
# If you like it both ways (if and unless) | |
# Semantic aliases | |
print "You gotta get over to room 6" unless track is 6 # unless complies to an if with !, is complies to === | |
# That might remind you of the ternary operator, here's how we do that with Coffee. | |
getting_this = if awake then "Yupp!" else "Nope!" | |
print yes isnt no # isnt compiles to !==, yes equals true and no equals false | |
print on isnt off | |
# not compiles to !, or compiles to ||, and compiles to && | |
if not awake or track is 6 and conference is "Angry Birds Fan Conference" | |
print "Guess you'll have to catch this on video" | |
# ...of course you can kick it old school if you dislike that amount of readability | |
if !awake || track == 6 && conference == "APL Symposium" # Remember == is CoffeeScript's === | |
print "Then this might be your cuppa:" | |
print "↑1 ⍵∨.^3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵" # Game of life as an APL one liner. | |
# Checking for existence. Compiles to: if (typeof aliens != "undefined" && aliens !== null) | |
print "Set phazers to stun" if aliens? | |
# Optional assignment - think defaults | |
this_is_null = null | |
this_is_null ||= "Default value" # or= can also be used. | |
print this_is_null | |
# Python-style chained comparisons | |
bpm = 120 | |
can_dance_to_this = 160 > bpm > 96 | |
print can_dance_to_this | |
# switch-case | |
switch Math.floor(Math.random() * 5) | |
when 1 | |
print "You're NDC lotto number is one." # No need to insert breaks! | |
when 2 | |
print "Two it is!" | |
when 3 or 4 | |
print "It was either 3 or 4" | |
else # aka default | |
print "It was something else." | |
# Loops | |
i = 0 | |
while i < 5 | |
print i++ | |
# ..or as a one-liner (refactor above example) | |
while i <5 then print i++ | |
# ..or or the other way around | |
print i++ until i is 10 | |
# for..in constructs work as expected in CoffeeScript and it all compiles into "best practice"-compliant JS code | |
letters = ['a','b','c','d','e','f','g'] | |
for letter in letters | |
print letter | |
# Works both was | |
print letter for letter in letters | |
# Iterating over hashes is just as simple | |
dictionary = "Horse" : "Hest", "Cow" : "Ku", "Goat" : "Geit" | |
for english_word, norwegian_word of dictionary | |
print "#{english_word} is #{norwegian_word} in Norwegian" # Ruby style string interpolation | |
# Let's take a closer look at that interpolation thingy! | |
print "4 * 10 + 2 = #{4 * 10 + 2}" # Expressions, not just variables. | |
print "Attendee: #{if awake then "YAY!" else "Zzzz..."}" # And even code! | |
# F is for Functional (Catch my talk yesterday?) | |
fib = (n) -> if n < 2 then n else fib(n-1) + fib(n-2) # No need for return, last statement gets returned - just like Ruby! | |
print fib 10 # Parens optional | |
# Variable arguments | |
jackson5song = (first,second,third,the_rest...) -> | |
print "#{first}, #{second}, #{third}, easy as 1,2,3" | |
print "Oh. And these were mentioned: #{the_rest}" | |
jackson5song 'q','r','s','t' | |
jackson5song letters... # without splats compiles to jackson5song(letters), with splats compiles to jackson5song.apply(null, letters) | |
# Ranges and arrays | |
numbers = [1..10] | |
print numbers | |
print numbers[3..6] # Gets slices the array at given indecies | |
numbers[3..6] = numbers[3..6].reverse() # Replaces a segemnt | |
print numbers | |
# Test value against all array elements. (Show source) | |
print "Gee is in there" if 'g' in letters | |
# Objects are similar to YAML | |
layout = | |
paper: "A4" | |
text: | |
typeface: "Helvetica" | |
color: "red" | |
size: 15 | |
# Soaking up null references with the existential operator. Similar to the "andand" gem in Ruby. This is a version of the null-object pattern. | |
print layout.text?.color # Delete layout.text to demo, the delete .? operator we get exception instead of undefined | |
print layout.text?.color ? "black" # Supplying default values. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Got class? | |
class Nameable | |
constructor: (@firstName, @lastName) -> | |
fullName: -> | |
"#{@firstName} #{@lastName}" # @ is shorthand for "this." | |
sayName: -> | |
print(@fullName()) | |
speak: (line) -> | |
print("#{@fullName()}: #{line}") | |
class Robot extends Nameable | |
sayName: -> | |
print("HELL0. I AM Y0UR ELECTR0NIC FRIEND #{@modelName().toUpperCase()} #{@modelNumber()} AND I AM READY T0 C0MPUTE. PLEASE ENTER Y0UR C0MMAND...") | |
modelName: -> | |
@firstName | |
modelNumber: -> | |
@lastName | |
execute: (command) -> | |
print("CALCULATING #{command.toUpperCase()}...") | |
print("EXECUTING #{command.toUpperCase()}...") | |
print("EXECUTI0N C0MPLETE!") | |
class Person extends Nameable | |
greeting: -> | |
"Hi" | |
sayName: -> | |
print("#{@greeting()}, my name is #{@fullName()}") | |
class Knight extends Person | |
fullName: -> | |
"Sir #{super()}" | |
class RoyalPerson extends Person | |
constructor: (name) -> | |
super(name, "Rex") | |
greeting: -> | |
"Hello dear pesants" | |
knight: (person) -> | |
if person instanceof Knight | |
print("But dear Sir, you're already a knight.") | |
return person | |
else if person instanceof Person | |
print("#{person.fullName()} I herby knight thee.") | |
return new Knight(person.firstName, person.lastName) | |
else | |
print("I'm terribly sorry, but I only knight people.") | |
ordinaryJoe = new Person("Joe", "Schomoo") | |
ordinaryJoe.sayName() | |
theKing = new RoyalPerson("Charles") | |
theKing.sayName() | |
sirSchmoo = theKing.knight(ordinaryJoe) | |
sirSchmoo.sayName() | |
theKing.knight(sirSchmoo) | |
robovac2000 = new Robot("Robovac", 2000) | |
robovac2000.sayName() | |
theKing.knight(robovac2000) | |
robovac2000.execute("vacuum livingroom") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Compiling Coffee Scripts in the Browser</title> | |
<!-- The JavaScript CoffeeScript compiler compiles all "text/coffeescript blocks when the page loads. --> | |
<script type="text/javascript" src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script> | |
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script> | |
<script type="text/coffeescript"> | |
$(window).load -> | |
$("#content").text("Hello NDC!") | |
</script> | |
</head> | |
<body> | |
<h1 id="content"></h1> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Wake Up and Smell the Coffee</title> | |
<link rel='stylesheet' type='text/css' href='@Url.Content("~/Content/site.css")' /> | |
<script type='text/javascript' src='@Url.Content("~/Scripts/jquery-1.6.1.js")'></script> | |
<script type='text/javascript' src='@Url.Content("~/Scripts/jquery.easing.js")'></script> | |
<script type='text/javascript' src='@Url.Content("~/Scripts/jquery.circulate.js")'></script> | |
<!-- "~/Scripts/demo.js" doesn't exist, but SassAndCoffee (https://github.com/xpaulbettsx/SassAndCoffee) will compile demo.coffee at runtime. --> | |
<script type='text/javascript' src='@Url.Content("~/Scripts/demo.js")'></script> | |
</head> | |
<body> | |
<div id="page-wrap"> | |
Hello | |
<div id="cups"> | |
<img src="@Url.Content("~/Content/Images/cup-coffee-icon.png")" id="cup1" /> | |
<img src="@Url.Content("~/Content/Images/cup-coffee-icon.png")" id="cup2" /> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Notice how clean this is compared to the JavaScript equivalent. | |
$(window).load -> | |
$("#cup1").circulate | |
speed: 4000, | |
height: 120, | |
width: -700, | |
sizeAdjustment: 35, | |
loop: yes, | |
zIndexValues: [1,1,3,3] | |
$("#cup2").circulate | |
speed: 4000, | |
height: 120, | |
width: -700, | |
sizeAdjustment: 35, | |
loop: true, | |
zIndexValues: [2, 2, 2, 2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This Rakefile was used to compile CoffeeScripts at build time. | |
# It depends on a few things being installed: Cygwin, Node.js, CoffeeScript through NPM and Ruby | |
# Albacore (http://albacorebuild.net/) is a set of Rake tasks enabling you to use | |
# Ruby's Rake build system in place of MSBuild. | |
require "rubygems" | |
require "albacore" | |
namespace :coffee do | |
task :compile do | |
puts "Compiling *.coffee in project" | |
Dir["**/*.coffee"].each do |file| # This finds all CoffeeScripts in the project and iterates through them,,, | |
puts "Compiling #{file}..." | |
`coffee -c #{file}` # The commandline CoffeeScript compiler is used here | |
end | |
puts "Coffee is served!" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment