A presentation on CoffeeScript given at ChiPy's 2013 Ultimate Language Shootout on June 13, 2013.
Video:
http://pyvideo.org/video/1864/ultimate-language-shootout-iv-coffeescript
28. Main function
main = ->
array = (c.toUpperCase() for c in 'abcdefg')
display array
$(document.body).append '<hr>'
for i in [1..6]
shuffle array
display array
29. Main function notes
• Whitespace delimits blocks
• Define function using -> operator
• Array expression uses parentheses
• Parentheses not required for function
invocation
• Ranges specified using [a..b]
• No colon at end of looping statement
30. Shuffle function
shuffle = (array) ->
i = array.length
j = null
if i == 0
return
while i -= 1
j = Math.floor(Math.random() * (i+1))
[array[i], array[j]] = [array[j], array[i]]
31. Shuffle function notes
• Parentheses somewhat advised when
complex expressions are passed into a
function
• No colon at end of conditional statement
• Destructuring assignment requires square
brackets
32. Display function
display = (array) ->
para = $('<p></p>')
$(document.body).append para
for char in array
val = 60 # simplified
style = """
background: rgb(#{val}, #{val}, #{val});
border: 1px solid black;
padding: 5px; margin: 5px;
"""
h = "<span style="#{style}">#{el}</span>"
para.append h
33. Display function notes
• Multiline string takes indentation into
account (resulting string doesn’t contain
extra spaces).
• String interpolation requires double-quoted
strings
35. Chrome to the rescue!
• Open Developer Tools
• Click gear icon in lower-right corner
• Check “Enable source maps”
• Compile your .coffee files using the -m
option