Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 167 lines (127 sloc) 3.069 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
# Functions:
square = (x) -> x * x

sum = (x, y) -> x + y

odd = (x) -> x % 2 isnt 0

even = (x) -> x % 2 is 0

run_loop = ->
  fire_events((e) -> e.stopPropagation())
  listen()
  wait()

# Objects:
dense_object_literal = {one: 1, two: 2, three: 3}

spaced_out_multiline_object =
  pi: 3.14159
  list: [1, 2, 3, 4]
  regex: /match[ing](every|thing|\/)/gi
  three: new Idea

  inner_obj:
    freedom: -> _.freedom()

# Arrays:
stooges = [{moe: 45}, {curly: 43}, {larry: 46}]

exponents = [((x) -> x), ((x) -> x * x), ((x) -> x * x * x)]

empty = []

multiline = [
  'line one'
  'line two'
]

# Conditionals and ternaries.
if submarine.shields_up
  full_speed_ahead()
  fire_torpedos()
else if submarine.sinking
  abandon_ship()
else
  run_away()

eldest = if 25 > 21 then liz else marge

decoration = medal_of_honor if war_hero

go_to_sleep() unless coffee

# Returning early:
race = ->
  run()
  walk()
  crawl()
  if tired then return sleep()
  race()

# Conditional assignment:
good or= evil
wine and= cheese

# Nested property access and calls.
((moon.turn(360))).shapes[3].move({x: 45, y: 30}).position['top'].offset('x')

a = b = c = 5

# Embedded JavaScript.
callback(
  `function(e) { e.stop(); }`
)

# Try/Catch/Finally/Throw.
try
  all_hell_breaks_loose()
  dogs_and_cats_living_together()
  throw "up"
catch error
  print(error)
finally
  clean_up()

try all_hell_breaks_loose() catch error then print(error) finally clean_up()

# While loops, break and continue.
while demand > supply
  sell()
  restock()

while supply > demand then buy()

loop
  break if broken
  continue if continuing

# Unary operators.
!!true

# Lexical scoping.
v_1 = 5
change_a_and_set_b = ->
  v_1 = 10
  v_2 = 15
v_2 = 20

# Array comprehensions.
supper = food.capitalize() for food in ['toast', 'cheese', 'wine']

drink bottle for bottle, i in ['soda', 'wine', 'lemonade'] when even i

# Switch statements ("else" serves as a default).
activity = switch day
  when "Tuesday" then eat_breakfast()
  when "Sunday" then go_to_church()
  when "Saturday" then go_to_the_park()
  when "Wednesday"
    if day is bingo_day
      go_to_bingo()
    else
      eat_breakfast()
      go_to_work()
      eat_dinner()
  else go_to_work()

# Semicolons can optionally be used instead of newlines.
wednesday = -> eat_breakfast(); go_to_work(); eat_dinner()

# Multiline strings with inner quotes.
story = "Lorem ipsum dolor \"sit\" amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
aliquam erat volutpat. Ut wisi enim ad."

# Inheritance and calling super.
class Animal
  (@name) ->

  move: (meters) ->
    alert this.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 Snake"
tom = new Horse "Tommy the Horse"

sam.move()
tom.move()

# Numbers.
a_googol = 1e100
hex = 0xff0000
negative = -1.0
infinity = Infinity
nan = NaN

# Deleting.
delete secret.identity
Something went wrong with that request. Please try again.