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

/examples/potion.coffee

http://github.com/jashkenas/coffee-script
CoffeeScript | 206 lines | 85 code | 66 blank | 55 comment | 7 complexity | aea0c8abd37ed01d66e0877a5d2bb292 MD5 | raw file
  1# Examples from _why's Potion, the Readme and "Potion: A Short Pamphlet".
  2
  3# 5 times: "Odelay!" print.
  4
  5print "Odelay!" for i in [1..5]
  6
  7
  8# add = (x, y): x + y.
  9# add(2, 4) string print
 10
 11add = (x, y) -> x + y
 12print add 2, 4
 13
 14
 15# loop: 'quaff' print.
 16
 17loop print 'quaff'
 18
 19
 20# ('cheese', 'bread', 'mayo') at (1) print
 21
 22print ['cheese', 'bread', 'mayo'][1]
 23
 24
 25# (language='Potion', pointless=true) at (key='language') print
 26
 27print {language: 'Potion', pointless: true}['language']
 28
 29
 30# minus = (x, y): x - y.
 31# minus (y=10, x=6)
 32
 33minus = (x, y) -> x - y
 34minus 6, 10
 35
 36
 37# foods = ('cheese', 'bread', 'mayo')
 38# foods (2)
 39
 40foods = ['cheese', 'bread', 'mayo']
 41foods[2]
 42
 43
 44# (dog='canine', cat='feline', fox='vulpine') each (key, val):
 45#   (key, ' is a ', val) join print.
 46
 47for key, val of {dog: 'canine', cat: 'feline', fox: 'vulpine'}
 48  print "#{key} is a #{val}"
 49
 50
 51# Person = class: /name, /age, /sex.
 52# Person print = ():
 53#   ('My name is ', /name, '.') join print.
 54
 55class Person
 56  print: ->
 57    print "My name is #{@name}."
 58
 59
 60# p = Person ()
 61# p /name string print
 62
 63p = new Person
 64print p.name
 65
 66
 67# Policeman = Person class (rank): /rank = rank.
 68# Policeman print = ():
 69#   ('My name is ', /name, ' and I'm a ', /rank, '.') join print.
 70#
 71# Policeman ('Constable') print
 72
 73class Policeman extends Person
 74  (@rank) ->
 75
 76  print: ->
 77    print "My name is #{@name} and I'm a #{@rank}."
 78
 79print new Policeman 'Constable'
 80
 81
 82# app = [window (width=200, height=400)
 83#         [para 'Welcome.', button 'OK']]
 84# app first name
 85
 86app =
 87  window:
 88    width: 200
 89    height: 200
 90  para:    'Welcome.'
 91  button:  'OK'
 92
 93app.window
 94
 95
 96# x = 1
 97# y = 2
 98#
 99# x = 1, y = 2
100
101x = 1
102y = 2
103
104x = 1; y = 2
105
106
107# table = (language='Potion'
108#           pointless=true)
109
110table =
111  language: 'Potion'
112  pointless: yes
113
114
115# # this foul business...
116# String length = (): 10.
117
118# this foul business...
119String::length = -> 10
120
121
122# block = :
123#   'potion' print.
124
125block = ->
126  print 'potion'
127
128
129# if (age > 100): 'ancient'.
130
131if age > 100 then 'ancient'
132
133
134# author =
135#   if (title == 'Jonathan Strange & Mr. Norrell'):
136#     'Susanna Clarke'.
137#   elsif (title == 'The Star Diaries'):
138#     'Stanislaw Lem'.
139#   elsif (title == 'The Slynx'):
140#     'Tatyana Tolstaya'.
141#   else:
142#     '... probably Philip K. Dick'.
143
144switch author
145  when 'Jonathan Strange & Mr. Norrell'
146    'Susanna Clarke'
147  when 'The Star Diaries'
148    'Stanislaw Lem'
149  when 'The Slynx'
150    'Tatyana Tolstaya'
151  else
152    '... probably Philip K. Dick'
153
154
155# count = 8
156# while (count > 0):
157#   'quaff' print
158#   count--.
159
160count = 8
161while count > 0
162  print 'quaff'
163  count--
164
165
166# 1 to 5 (a):
167#   a string print.
168
169print a for a in [1..5]
170
171
172# if (3 ?gender):
173#   "Huh? Numbers are sexed? That's amazing." print.
174
175if 3.gender?
176  print "Huh? Numbers are sexed? That's amazing."
177
178
179# HomePage get = (url):
180#   session = url query ? at ('session').
181
182HomePage::get = (url) ->
183  session = url.query?.session
184
185
186# BTree = class: /left, /right.
187# b = BTree ()
188# b /left = BTree ()
189# b /right = BTree ()
190
191BTree   = ->
192b       = new BTree
193b.left  = new BTree
194b.right = new BTree
195
196
197# BTree = class: /left, /right.
198# b = BTree ()
199#
200# if (b ? /left):
201#   'left path found!' print.
202
203BTree = ->
204b = new BTree
205
206print 'left path found!' if b.left?