PageRenderTime 31ms CodeModel.GetById 9ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/code.coffee

http://github.com/jashkenas/coffee-script
CoffeeScript | 167 lines | 108 code | 40 blank | 19 comment | 16 complexity | 0c42ff4e2b1e788ee76b4ca33753ef0e MD5 | raw file
  1# Functions:
  2square = (x) -> x * x
  3
  4sum = (x, y) -> x + y
  5
  6odd = (x) -> x % 2 isnt 0
  7
  8even = (x) -> x % 2 is 0
  9
 10run_loop = ->
 11  fire_events((e) -> e.stopPropagation())
 12  listen()
 13  wait()
 14
 15# Objects:
 16dense_object_literal = one: 1, two: 2, three: 3
 17
 18spaced_out_multiline_object =
 19  pi: 3.14159
 20  list: [1, 2, 3, 4]
 21  regex: /match[ing](every|thing|\/)/gi
 22  three: new Idea
 23
 24  inner_obj:
 25    freedom: -> _.freedom()
 26
 27# Arrays:
 28stooges = [{moe: 45}, {curly: 43}, {larry: 46}]
 29
 30exponents = [((x) -> x), ((x) -> x * x), ((x) -> x * x * x)]
 31
 32empty = []
 33
 34multiline = [
 35  'line one'
 36  'line two'
 37]
 38
 39# Conditionals and ternaries.
 40if submarine.shields_up
 41  full_speed_ahead()
 42  fire_torpedos()
 43else if submarine.sinking
 44  abandon_ship()
 45else
 46  run_away()
 47
 48eldest = if 25 > 21 then liz else marge
 49
 50decoration = medal_of_honor if war_hero
 51
 52go_to_sleep() unless coffee
 53
 54# Returning early:
 55race = ->
 56  run()
 57  walk()
 58  crawl()
 59  return sleep() if tired
 60  race()
 61
 62# Conditional assignment:
 63good or= evil
 64wine and= cheese
 65
 66# Nested property access and calls.
 67(moon.turn 360).shapes[3].move(x: 45, y: 30).position['top'].offset('x')
 68
 69a = b = c = 5
 70
 71# Embedded JavaScript.
 72callback(
 73  `function(e) { e.stop(); }`
 74)
 75
 76# Try/Catch/Finally/Throw.
 77try
 78  all_hell_breaks_loose()
 79  dogs_and_cats_living_together()
 80  throw "up"
 81catch error
 82  print error
 83finally
 84  clean_up()
 85
 86try all_hell_breaks_loose() catch error then print(error) finally clean_up()
 87
 88# While loops, break and continue.
 89while demand > supply
 90  sell()
 91  restock()
 92
 93while supply > demand then buy()
 94
 95loop
 96  break if broken
 97  continue if continuing
 98
 99# Unary operators.
100!!true
101
102# Lexical scoping.
103v_1 = 5
104change_a_and_set_b = ->
105  v_1 = 10
106  v_2 = 15
107v_2 = 20
108
109# Array comprehensions.
110supper = food.capitalize() for food in ['toast', 'cheese', 'wine']
111
112drink bottle for bottle, i in ['soda', 'wine', 'lemonade'] when even i
113
114# Switch statements ("else" serves as a default).
115activity = switch day
116  when "Tuesday"   then eat_breakfast()
117  when "Sunday"    then go_to_church()
118  when "Saturday"  then go_to_the_park()
119  when "Wednesday"
120    if day is bingo_day
121      go_to_bingo()
122    else
123      eat_breakfast()
124      go_to_work()
125      eat_dinner()
126  else go_to_work()
127
128# Semicolons can optionally be used instead of newlines.
129wednesday = -> eat_breakfast(); go_to_work(); eat_dinner()
130
131# Multiline strings with inner quotes.
132story = "Lorem ipsum dolor \"sit\" amet, consectetuer adipiscing elit,
133         sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
134         aliquam erat volutpat. Ut wisi enim ad."
135
136# Inheritance and calling super.
137class Animal
138  (@name) ->
139
140  move: (meters) ->
141    alert this.name + " moved " + meters + "m."
142
143class Snake extends Animal
144  move: ->
145    alert 'Slithering...'
146    super 5
147
148class Horse extends Animal
149  move: ->
150    alert 'Galloping...'
151    super 45
152
153sam = new Snake "Sammy the Snake"
154tom = new Horse "Tommy the Horse"
155
156sam.move()
157tom.move()
158
159# Numbers.
160a_googol =  1e100
161hex      =  0xff0000
162negative =  -1.0
163infinity =  Infinity
164nan      =  NaN
165
166# Deleting.
167delete secret.identity