summaryrefslogtreecommitdiff
path: root/examples/basic.coffee
blob: d183fa6b75066760ddba5e363c2af0b372f3b37e (plain)
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
# These examples are taken from
# http://jashkenas.github.com/coffee-script/

song: ["do", "re", "mi", "fa", "so"]

ages: {
  max: 10
  ida: 9
  tim: 11
}

matrix: [
  1, 0, 1
  0, 0, 1
  1, 1, 0
]

eldest: if 24 > 21 then "Liz" else "Ike"

six: (one: 1) + (two: 2) + (three: 3)

My.mood: greatly_improved if true

# Unfancy JavaScript
if happy and knows_it
  cha_cha_cha()
  false

Account: (customer, cart) ->
  @customer: customer
  @cart: cart

  $('.shopping_cart').bind 'click', (event) =>
    @customer.purchase @cart

class Animal
  move: (meters) ->
    alert @name + " moved " + meters + "m."

  randomify: ->
    @name.replace(/^[\w_-]*$/g, "-")

class Snake extends Animal
  constructor: (name) ->
    @name: name

  move: ->
    alert "Slithering..."
    super 5

class Horse extends Animal
  constructor: (name) ->
    @name: name

  move: ->
    alert "Galloping..."
    super 45

sam: new Snake "Sammy the Python"
tom: new Horse "Tommy the Palomino"






sam.move()
tom.move()
if car.speed < speed_limit then accelerate()

print "My name is " + @name

gold: silver: the_field: "unknown"

award_medals: (first, second, rest...) ->
  gold:       first
  silver:     second
  the_field:  rest

contenders: [
  "Michael Phelps"
  "Liu Xiang"
]

award_medals contenders...

alert "Gold: " + gold
alert "Silver: " + silver
alert "The Field: " + the_field

# Eat lunch.
# what up
# love it.
lunch: eat food for food in ['toast', 'cheese', 'wine']

$('#demo').click ->
  asd
# sup
  # asd
  # asdasd
blah: true

okay


# Naive collision detection.
for roid in asteroids
  for roid2 in asteroids when roid isnt roid2
    roid.explode() if roid.overlaps roid2

years_old: {max: 10, ida: 9, tim: 11}

ages: for child, age of years_old
  child + " is " + age

grade: (student) ->
  if student.excellent_work
    "A+"
  else if student.okay_stuff
    if student.tried_hard then "B" else "B-"
  else
    "C"