/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. # 5 times: "Odelay!" print.
  3. print "Odelay!" for i in [1..5]
  4. # add = (x, y): x + y.
  5. # add(2, 4) string print
  6. add = (x, y) -> x + y
  7. print add 2, 4
  8. # loop: 'quaff' print.
  9. loop print 'quaff'
  10. # ('cheese', 'bread', 'mayo') at (1) print
  11. print ['cheese', 'bread', 'mayo'][1]
  12. # (language='Potion', pointless=true) at (key='language') print
  13. print {language: 'Potion', pointless: true}['language']
  14. # minus = (x, y): x - y.
  15. # minus (y=10, x=6)
  16. minus = (x, y) -> x - y
  17. minus 6, 10
  18. # foods = ('cheese', 'bread', 'mayo')
  19. # foods (2)
  20. foods = ['cheese', 'bread', 'mayo']
  21. foods[2]
  22. # (dog='canine', cat='feline', fox='vulpine') each (key, val):
  23. # (key, ' is a ', val) join print.
  24. for key, val of {dog: 'canine', cat: 'feline', fox: 'vulpine'}
  25. print "#{key} is a #{val}"
  26. # Person = class: /name, /age, /sex.
  27. # Person print = ():
  28. # ('My name is ', /name, '.') join print.
  29. class Person
  30. print: ->
  31. print "My name is #{@name}."
  32. # p = Person ()
  33. # p /name string print
  34. p = new Person
  35. print p.name
  36. # Policeman = Person class (rank): /rank = rank.
  37. # Policeman print = ():
  38. # ('My name is ', /name, ' and I'm a ', /rank, '.') join print.
  39. #
  40. # Policeman ('Constable') print
  41. class Policeman extends Person
  42. (@rank) ->
  43. print: ->
  44. print "My name is #{@name} and I'm a #{@rank}."
  45. print new Policeman 'Constable'
  46. # app = [window (width=200, height=400)
  47. # [para 'Welcome.', button 'OK']]
  48. # app first name
  49. app =
  50. window:
  51. width: 200
  52. height: 200
  53. para: 'Welcome.'
  54. button: 'OK'
  55. app.window
  56. # x = 1
  57. # y = 2
  58. #
  59. # x = 1, y = 2
  60. x = 1
  61. y = 2
  62. x = 1; y = 2
  63. # table = (language='Potion'
  64. # pointless=true)
  65. table =
  66. language: 'Potion'
  67. pointless: yes
  68. # # this foul business...
  69. # String length = (): 10.
  70. # this foul business...
  71. String::length = -> 10
  72. # block = :
  73. # 'potion' print.
  74. block = ->
  75. print 'potion'
  76. # if (age > 100): 'ancient'.
  77. if age > 100 then 'ancient'
  78. # author =
  79. # if (title == 'Jonathan Strange & Mr. Norrell'):
  80. # 'Susanna Clarke'.
  81. # elsif (title == 'The Star Diaries'):
  82. # 'Stanislaw Lem'.
  83. # elsif (title == 'The Slynx'):
  84. # 'Tatyana Tolstaya'.
  85. # else:
  86. # '... probably Philip K. Dick'.
  87. switch author
  88. when 'Jonathan Strange & Mr. Norrell'
  89. 'Susanna Clarke'
  90. when 'The Star Diaries'
  91. 'Stanislaw Lem'
  92. when 'The Slynx'
  93. 'Tatyana Tolstaya'
  94. else
  95. '... probably Philip K. Dick'
  96. # count = 8
  97. # while (count > 0):
  98. # 'quaff' print
  99. # count--.
  100. count = 8
  101. while count > 0
  102. print 'quaff'
  103. count--
  104. # 1 to 5 (a):
  105. # a string print.
  106. print a for a in [1..5]
  107. # if (3 ?gender):
  108. # "Huh? Numbers are sexed? That's amazing." print.
  109. if 3.gender?
  110. print "Huh? Numbers are sexed? That's amazing."
  111. # HomePage get = (url):
  112. # session = url query ? at ('session').
  113. HomePage::get = (url) ->
  114. session = url.query?.session
  115. # BTree = class: /left, /right.
  116. # b = BTree ()
  117. # b /left = BTree ()
  118. # b /right = BTree ()
  119. BTree = ->
  120. b = new BTree
  121. b.left = new BTree
  122. b.right = new BTree
  123. # BTree = class: /left, /right.
  124. # b = BTree ()
  125. #
  126. # if (b ? /left):
  127. # 'left path found!' print.
  128. BTree = ->
  129. b = new BTree
  130. print 'left path found!' if b.left?