2009-12-13 22:23:10 +00:00
|
|
|
# Functions:
|
2010-07-29 04:03:42 +00:00
|
|
|
square = (x) -> x * x
|
2009-12-17 02:01:47 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
sum = (x, y) -> x + y
|
2009-12-17 02:01:47 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
odd = (x) -> x % 2 isnt 0
|
2009-12-17 02:01:47 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
even = (x) -> x % 2 is 0
|
2009-12-17 02:01:47 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
run_loop = ->
|
2010-01-26 15:52:05 +00:00
|
|
|
fire_events((e) -> e.stopPropagation())
|
2009-12-17 02:01:47 +00:00
|
|
|
listen()
|
2009-12-30 23:09:43 +00:00
|
|
|
wait()
|
2009-12-17 02:01:47 +00:00
|
|
|
|
|
|
|
# Objects:
|
2010-07-29 04:03:42 +00:00
|
|
|
dense_object_literal = {one: 1, two: 2, three: 3}
|
2009-12-17 01:48:37 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
spaced_out_multiline_object =
|
2009-12-13 23:37:29 +00:00
|
|
|
pi: 3.14159
|
2009-12-18 02:00:31 +00:00
|
|
|
list: [1, 2, 3, 4]
|
2009-12-17 13:29:19 +00:00
|
|
|
regex: /match[ing](every|thing|\/)/gi
|
2010-06-30 01:03:50 +00:00
|
|
|
three: new Idea
|
2009-12-17 01:48:37 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
inner_obj:
|
2010-01-26 15:52:05 +00:00
|
|
|
freedom: -> _.freedom()
|
2009-12-13 23:37:29 +00:00
|
|
|
|
|
|
|
# Arrays:
|
2010-07-29 04:03:42 +00:00
|
|
|
stooges = [{moe: 45}, {curly: 43}, {larry: 46}]
|
2009-12-13 23:37:29 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
exponents = [((x) -> x), ((x) -> x * x), ((x) -> x * x * x)]
|
2009-12-13 23:37:29 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
empty = []
|
2009-12-17 02:01:47 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
multiline = [
|
2009-12-18 03:13:29 +00:00
|
|
|
'line one'
|
|
|
|
'line two'
|
|
|
|
]
|
|
|
|
|
2009-12-13 23:37:29 +00:00
|
|
|
# Conditionals and ternaries.
|
|
|
|
if submarine.shields_up
|
|
|
|
full_speed_ahead()
|
2009-12-16 01:49:24 +00:00
|
|
|
fire_torpedos()
|
2009-12-18 05:49:23 +00:00
|
|
|
else if submarine.sinking
|
|
|
|
abandon_ship()
|
2009-12-13 23:37:29 +00:00
|
|
|
else
|
2009-12-30 23:09:43 +00:00
|
|
|
run_away()
|
2009-12-13 23:37:29 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
eldest = if 25 > 21 then liz else marge
|
2009-12-13 23:37:29 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
decoration = medal_of_honor if war_hero
|
2009-12-15 13:53:21 +00:00
|
|
|
|
|
|
|
go_to_sleep() unless coffee
|
|
|
|
|
2009-12-13 23:37:29 +00:00
|
|
|
# Returning early:
|
2010-07-29 04:03:42 +00:00
|
|
|
race = ->
|
2009-12-13 23:37:29 +00:00
|
|
|
run()
|
|
|
|
walk()
|
|
|
|
crawl()
|
2009-12-30 23:09:43 +00:00
|
|
|
if tired then return sleep()
|
|
|
|
race()
|
2009-12-13 22:07:16 +00:00
|
|
|
|
2009-12-18 02:46:12 +00:00
|
|
|
# Conditional assignment:
|
2010-07-30 01:23:49 +00:00
|
|
|
good or= evil
|
|
|
|
wine and= cheese
|
2009-12-14 01:29:44 +00:00
|
|
|
|
2009-12-14 05:06:08 +00:00
|
|
|
# Nested property access and calls.
|
2009-12-18 03:13:29 +00:00
|
|
|
((moon.turn(360))).shapes[3].move({x: 45, y: 30}).position['top'].offset('x')
|
2009-12-14 05:06:08 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
a = b = c = 5
|
2009-12-15 15:35:17 +00:00
|
|
|
|
2009-12-15 14:11:27 +00:00
|
|
|
# Embedded JavaScript.
|
|
|
|
callback(
|
|
|
|
`function(e) { e.stop(); }`
|
|
|
|
)
|
|
|
|
|
2009-12-15 04:03:51 +00:00
|
|
|
# Try/Catch/Finally/Throw.
|
2009-12-14 15:00:31 +00:00
|
|
|
try
|
|
|
|
all_hell_breaks_loose()
|
|
|
|
dogs_and_cats_living_together()
|
2009-12-15 04:03:51 +00:00
|
|
|
throw "up"
|
2009-12-14 15:00:31 +00:00
|
|
|
catch error
|
2009-12-30 23:09:43 +00:00
|
|
|
print(error)
|
2009-12-15 04:03:51 +00:00
|
|
|
finally
|
2009-12-30 23:09:43 +00:00
|
|
|
clean_up()
|
2009-12-13 22:07:16 +00:00
|
|
|
|
2009-12-30 23:09:43 +00:00
|
|
|
try all_hell_breaks_loose() catch error then print(error) finally clean_up()
|
2009-12-15 05:27:34 +00:00
|
|
|
|
2009-12-18 02:10:49 +00:00
|
|
|
# While loops, break and continue.
|
2009-12-15 05:27:34 +00:00
|
|
|
while demand > supply
|
|
|
|
sell()
|
2009-12-30 23:09:43 +00:00
|
|
|
restock()
|
2009-12-15 05:27:34 +00:00
|
|
|
|
2009-12-30 23:09:43 +00:00
|
|
|
while supply > demand then buy()
|
2009-12-15 05:27:34 +00:00
|
|
|
|
2010-06-12 16:15:53 +00:00
|
|
|
loop
|
2009-12-18 02:10:49 +00:00
|
|
|
break if broken
|
2009-12-30 23:09:43 +00:00
|
|
|
continue if continuing
|
2009-12-18 02:10:49 +00:00
|
|
|
|
2009-12-15 05:27:34 +00:00
|
|
|
# Unary operators.
|
2009-12-24 05:37:33 +00:00
|
|
|
!!true
|
2009-12-15 05:27:34 +00:00
|
|
|
|
2009-12-15 15:07:10 +00:00
|
|
|
# Lexical scoping.
|
2010-07-29 04:03:42 +00:00
|
|
|
v_1 = 5
|
|
|
|
change_a_and_set_b = ->
|
|
|
|
v_1 = 10
|
|
|
|
v_2 = 15
|
|
|
|
v_2 = 20
|
2009-12-15 15:07:10 +00:00
|
|
|
|
2009-12-15 15:28:54 +00:00
|
|
|
# Array comprehensions.
|
2010-07-29 04:03:42 +00:00
|
|
|
supper = food.capitalize() for food in ['toast', 'cheese', 'wine']
|
2009-12-15 15:28:54 +00:00
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
drink bottle for bottle, i in ['soda', 'wine', 'lemonade'] when even i
|
2009-12-16 03:30:27 +00:00
|
|
|
|
2009-12-18 02:14:36 +00:00
|
|
|
# Switch statements ("else" serves as a default).
|
2010-07-29 04:03:42 +00:00
|
|
|
activity = switch day
|
2009-12-30 23:09:43 +00:00
|
|
|
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()
|
2009-12-17 01:19:52 +00:00
|
|
|
|
|
|
|
# Semicolons can optionally be used instead of newlines.
|
2010-07-29 04:03:42 +00:00
|
|
|
wednesday = -> eat_breakfast(); go_to_work(); eat_dinner()
|
2009-12-17 02:43:26 +00:00
|
|
|
|
2009-12-17 13:26:46 +00:00
|
|
|
# Multiline strings with inner quotes.
|
2010-07-29 04:03:42 +00:00
|
|
|
story = "Lorem ipsum dolor \"sit\" amet, consectetuer adipiscing elit,
|
2009-12-17 13:23:07 +00:00
|
|
|
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
|
2009-12-17 14:07:42 +00:00
|
|
|
aliquam erat volutpat. Ut wisi enim ad."
|
|
|
|
|
2009-12-21 16:41:45 +00:00
|
|
|
# Inheritance and calling super.
|
2010-02-28 00:40:53 +00:00
|
|
|
class Animal
|
2010-11-13 20:22:18 +00:00
|
|
|
(@name) ->
|
2010-07-29 04:03:42 +00:00
|
|
|
|
2010-02-28 00:40:53 +00:00
|
|
|
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
|
|
|
|
|
2010-07-29 04:03:42 +00:00
|
|
|
sam = new Snake "Sammy the Snake"
|
|
|
|
tom = new Horse "Tommy the Horse"
|
2009-12-21 16:41:45 +00:00
|
|
|
|
|
|
|
sam.move()
|
|
|
|
tom.move()
|
2009-12-18 02:00:31 +00:00
|
|
|
|
|
|
|
# Numbers.
|
2010-07-29 04:03:42 +00:00
|
|
|
a_googol = 1e100
|
|
|
|
hex = 0xff0000
|
|
|
|
negative = -1.0
|
|
|
|
infinity = Infinity
|
|
|
|
nan = NaN
|
2009-12-18 02:21:07 +00:00
|
|
|
|
|
|
|
# Deleting.
|
|
|
|
delete secret.identity
|