mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-04-11 03:00:13 -04:00
added a whole slew of nice Potion examples from the pamphlet -- CoffeeScript stacks up pretty well.
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
years_old: {max: 10, ida: 9, tim: 11}
|
years_old: {max: 10, ida: 9, tim: 11}
|
||||||
|
|
||||||
ages: child + " is " + age for child, age ino years_old
|
ages: for child, age ino years_old
|
||||||
|
child + " is " + age
|
||||||
205
examples/potion.coffee
Normal file
205
examples/potion.coffee
Normal file
@@ -0,0 +1,205 @@
|
|||||||
|
# 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.
|
||||||
|
|
||||||
|
while true
|
||||||
|
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 ino {dog: 'canine', cat: 'feline', fox: 'vulpine'}
|
||||||
|
print(key + ' is a ' + val)
|
||||||
|
|
||||||
|
|
||||||
|
# Person = class: /name, /age, /sex.
|
||||||
|
# Person print = ():
|
||||||
|
# ('My name is ', /name, '.') join print.
|
||||||
|
|
||||||
|
Person: =>
|
||||||
|
Person::print: =>
|
||||||
|
print('My name is ' + this.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
|
||||||
|
|
||||||
|
Policeman: rank => this.rank: rank
|
||||||
|
Policeman extends Person
|
||||||
|
Policeman::print: =>
|
||||||
|
print('My name is ' + this.name + " and I'm a " + this.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 if url.query?
|
||||||
|
|
||||||
|
|
||||||
|
# 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?
|
||||||
@@ -787,7 +787,8 @@ egg_delivery = function egg_delivery() {
|
|||||||
</p>
|
</p>
|
||||||
<div class='code'><pre class="idle"><span class="FunctionName">years_old</span><span class="Keyword">:</span> {<span class="FunctionName">max</span><span class="Keyword">:</span> <span class="Number">10</span>, <span class="FunctionName">ida</span><span class="Keyword">:</span> <span class="Number">9</span>, <span class="FunctionName">tim</span><span class="Keyword">:</span> <span class="Number">11</span>}
|
<div class='code'><pre class="idle"><span class="FunctionName">years_old</span><span class="Keyword">:</span> {<span class="FunctionName">max</span><span class="Keyword">:</span> <span class="Number">10</span>, <span class="FunctionName">ida</span><span class="Keyword">:</span> <span class="Number">9</span>, <span class="FunctionName">tim</span><span class="Keyword">:</span> <span class="Number">11</span>}
|
||||||
|
|
||||||
<span class="FunctionName">ages</span><span class="Keyword">:</span> child <span class="Keyword">+</span> <span class="String"><span class="String">"</span> is <span class="String">"</span></span> <span class="Keyword">+</span> age <span class="Keyword">for</span> child, age <span class="Keyword">ino</span> years_old
|
<span class="FunctionName">ages</span><span class="Keyword">:</span> <span class="Keyword">for</span> child, age <span class="Keyword">ino</span> years_old
|
||||||
|
child <span class="Keyword">+</span> <span class="String"><span class="String">"</span> is <span class="String">"</span></span> <span class="Keyword">+</span> age
|
||||||
</pre><pre class="idle"><span class="Storage">var</span> __a, __b, age, ages, child, years_old;
|
</pre><pre class="idle"><span class="Storage">var</span> __a, __b, age, ages, child, years_old;
|
||||||
years_old <span class="Keyword">=</span> {
|
years_old <span class="Keyword">=</span> {
|
||||||
max: <span class="Number">10</span>,
|
max: <span class="Number">10</span>,
|
||||||
|
|||||||
@@ -214,7 +214,7 @@
|
|||||||
</dict>
|
</dict>
|
||||||
<dict>
|
<dict>
|
||||||
<key>match</key>
|
<key>match</key>
|
||||||
<string>\b([a-zA-Z$_](\w|\$|:)*)(\:)\s</string>
|
<string>\b([a-zA-Z$_](\w|\$|:|\.)*)(\:)\s</string>
|
||||||
<key>name</key>
|
<key>name</key>
|
||||||
<string>variable.assignment.coffee</string>
|
<string>variable.assignment.coffee</string>
|
||||||
<key>captures</key>
|
<key>captures</key>
|
||||||
|
|||||||
Reference in New Issue
Block a user