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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
# Examples from _why's Potion, the Readme and "Potion: A Short Pamphlet".
# 5 times: "Odelay!" print.
print "Odelay!" for i in [1..5]
# add = (x, y): x + y.
# add(2, 4) string print
add = (x, y) -> x + y
print add 2, 4
# loop: 'quaff' print.
loop print 'quaff'
# ('cheese', 'bread', 'mayo') at (1) print
print ['cheese', 'bread', 'mayo'][1]
# (language='Potion', pointless=true) at (key='language') print
print {language: 'Potion', pointless: true}['language']
# minus = (x, y): x - y.
# minus (y=10, x=6)
minus = (x, y) -> x - y
minus 6, 10
# foods = ('cheese', 'bread', 'mayo')
# foods (2)
foods = ['cheese', 'bread', 'mayo']
foods[2]
# (dog='canine', cat='feline', fox='vulpine') each (key, val):
# (key, ' is a ', val) join print.
for key, val of {dog: 'canine', cat: 'feline', fox: 'vulpine'}
print "#{key} is a #{val}"
# Person = class: /name, /age, /sex.
# Person print = ():
# ('My name is ', /name, '.') join print.
class Person
print: ->
print "My name is #{@name}."
# p = Person ()
# p /name string print
p = new Person
print p.name
# Policeman = Person class (rank): /rank = rank.
# Policeman print = ():
# ('My name is ', /name, ' and I'm a ', /rank, '.') join print.
#
# Policeman ('Constable') print
class Policeman extends Person
(@rank) ->
print: ->
print "My name is #{@name} and I'm a #{@rank}."
print new Policeman 'Constable'
# app = [window (width=200, height=400)
# [para 'Welcome.', button 'OK']]
# app first name
app =
window:
width: 200
height: 200
para: 'Welcome.'
button: 'OK'
app.window
# x = 1
# y = 2
#
# x = 1, y = 2
x = 1
y = 2
x = 1; y = 2
# table = (language='Potion'
# pointless=true)
table =
language: 'Potion'
pointless: yes
# # this foul business...
# String length = (): 10.
# this foul business...
String::length = -> 10
# block = :
# 'potion' print.
block = ->
print 'potion'
# if (age > 100): 'ancient'.
if age > 100 then 'ancient'
# author =
# if (title == 'Jonathan Strange & Mr. Norrell'):
# 'Susanna Clarke'.
# elsif (title == 'The Star Diaries'):
# 'Stanislaw Lem'.
# elsif (title == 'The Slynx'):
# 'Tatyana Tolstaya'.
# else:
# '... probably Philip K. Dick'.
switch author
when 'Jonathan Strange & Mr. Norrell'
'Susanna Clarke'
when 'The Star Diaries'
'Stanislaw Lem'
when 'The Slynx'
'Tatyana Tolstaya'
else
'... probably Philip K. Dick'
# count = 8
# while (count > 0):
# 'quaff' print
# count--.
count = 8
while count > 0
print 'quaff'
count--
# 1 to 5 (a):
# a string print.
print a for a in [1..5]
# if (3 ?gender):
# "Huh? Numbers are sexed? That's amazing." print.
if 3.gender?
print "Huh? Numbers are sexed? That's amazing."
# HomePage get = (url):
# session = url query ? at ('session').
HomePage::get = (url) ->
session = url.query?.session
# BTree = class: /left, /right.
# b = BTree ()
# b /left = BTree ()
# b /right = BTree ()
BTree = ->
b = new BTree
b.left = new BTree
b.right = new BTree
# BTree = class: /left, /right.
# b = BTree ()
#
# if (b ? /left):
# 'left path found!' print.
BTree = ->
b = new BTree
print 'left path found!' if b.left?
|