Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Demo for Coffeescript presentation
# String interpolation
result = "The result is #{3}"
console.log result
movieId = 452
url = "http://movies/#{movieId}"
console.log url
# Functions
square = (x) -> x * x
console.log "The square of 2 is #{square(2)}"
getMovieUrl = (movie, showReviews) -> "http://movies/#{movie}?#{showReviews}"
console.log "To get the movie with id #{1} the url is #{getMovieUrl 1, false}"
# Functions II
getMovieUrl = (id, baseUrl = '/movies') ->
"#{baseUrl}/#{id}"
console.log "The url for movie 1 is #{getMovieUrl 1}"
# Objects
config =
local:
user: 'dev'
pwd: 'dev123'
remote:
user: 'superdev'
pwd: "impossibleToGuess"
console.log "For the config local we have #{config.local.user} and #{config.local['pwd']}"
# Maps and arrays
deploy = ['local', 'remote', 'uat']
fib = [1, 2, 3, 5, 8, 13, 21]
console.log "We choose to deploy on #{deploy[0]}"
first = fib[0..3]
console.log "First 3 Fibonnaci: #{first}"
noLast = fib[0..-2]
console.log "Everything but the last: #{noLast}"
# Destructuring assignment
[firstName, nick, lastName] = ['D\'Arcy', 'Baconator', 'Lussier']
console.log "Had a great time with #{firstName} '#{nick}' #{lastName}"
reviews = [45, 29, 21, 10, 8, 4]
[best, secondBest, theRest...] = reviews
console.log "Best review #{best}"
console.log "Second best review #{secondBest}"
console.log "All the rest #{theRest}"
# if
isJson = true
callIndex = -> console.log 'Index called!'
showMessage = -> console.log 'The request is not JSON....'
if isJson
callIndex()
else
showMessage()
# Unless
unless isJson
showMessage()
# Modifiers
showMessage() unless isJson
callIndex() if isJson
# Switch
critics =
Darcy: "D'Arcy Lussier"
Jay: 'Jay Sherman'
movieReview = (critic, movie) ->
switch critic
when 'Jay'
'It Stinks!'
when 'Darcy'
if movie.match(/Bacon/) then 'Awesome!' else 'It needs more Bacon!'
else
throw new Error('Invalid critic name!')
console.log "Canadian Bacon -> #{critics.Darcy}: #{movieReview 'Darcy', 'Canadian Bacon'}"
console.log "Canadian Bacon -> #{critics.Jay}: #{movieReview 'Jay', 'Canadian Bacon'}"
# Comprehension
negativeNumbers = (-num for num in [1, 2, 3, 4])
console.log negativeNumbers
evens = (x for x in [2..10] by 2)
isInteger = (num) -> num is Math.round(num)
numsThatDivide960 = (num for num in [1..960] when isInteger(960 / num))
console.log numsThatDivide960
deploy = (env) -> console.log "Application deployed to #{env}"
deploy env for env in ['local', 'uat', 'prod']
# Existential Operator
question = paragraph? and not createdDate?
defaultValue = null
defaultValue ?= 5
console.log "Default value #{defaultValue}"
console.log "The question is: #{question}"
currentUser = -> null
console.log "The current user: #{currentUser()?.name}"
console.log "The actual user: #{actualUser?().name}"
# Classes
ajaxCaller = (options) ->
console.log "Calling the URL #{options.url}"
options.success "<h1>Movie form created!</h1>"
class MovieRepository
constructor: (@baseUrl) ->
newMovie: ->
ajaxCaller
url: "#{@baseUrl}/movies/create"
success: (data) -> console.log "Result data of calling: #{data}"
new MovieRepository("http://movies").newMovie()
# Inheritance
class Shape
constructor: (@width) ->
class Square extends Shape
computeArea: -> Math.pow @width, 2
class Circle extends Shape
radius: -> @width / 2
computeArea: -> Math.PI * Math.pow @radius(), 2
showArea = (shape) ->
console.log shape.computeArea()
showArea new Square(2) # 4
showArea new Circle(2) # pi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment