Skip to content

Instantly share code, notes, and snippets.

@nilsnh
Last active December 14, 2015 13:59
Show Gist options
  • Save nilsnh/5097730 to your computer and use it in GitHub Desktop.
Save nilsnh/5097730 to your computer and use it in GitHub Desktop.
Just a simple Harry Potter inspired coffee-script to wow and excite. :)
#A function
cast = (spell) ->
console.log "Casting spell: #{spell}"
#Comprehensions. "Syntactic sugar"
mySpellList = ['Expelliarmus', 'Lumos', 'Tarantallegra']
cast spell for spell in mySpellList
#Same as
for spell in mySpellList
cast spell
#If checks as oneliners
spellCast = () -> 'Nox'
if spellCast() is 'Nox' then cast 'Lumos'
#A more expressive 'if not'
invisible = true
unless invisible is true then console.log 'Mrs. Norris will spot us.'
#Checking for null and undefined
if safe? then console.log 'Open the door' else console.log "Don't open it!"
#Functional fanciness
spellComb = (spell, callback) ->
cast = (spell) -> "#{spell}"
nextSpell = () -> if callback? then "-#{callback}" else ''
"#{cast(spell)}#{nextSpell()}"
console.log spellComb('Protego', spellComb('Incendio', spellComb('Tarantallegra')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment