/examples/code.coffee

http://github.com/jashkenas/coffee-script · CoffeeScript · 167 lines · 108 code · 40 blank · 19 comment · 16 complexity · 0c42ff4e2b1e788ee76b4ca33753ef0e MD5 · raw file

  1. # Functions:
  2. square = (x) -> x * x
  3. sum = (x, y) -> x + y
  4. odd = (x) -> x % 2 isnt 0
  5. even = (x) -> x % 2 is 0
  6. run_loop = ->
  7. fire_events((e) -> e.stopPropagation())
  8. listen()
  9. wait()
  10. # Objects:
  11. dense_object_literal = one: 1, two: 2, three: 3
  12. spaced_out_multiline_object =
  13. pi: 3.14159
  14. list: [1, 2, 3, 4]
  15. regex: /match[ing](every|thing|\/)/gi
  16. three: new Idea
  17. inner_obj:
  18. freedom: -> _.freedom()
  19. # Arrays:
  20. stooges = [{moe: 45}, {curly: 43}, {larry: 46}]
  21. exponents = [((x) -> x), ((x) -> x * x), ((x) -> x * x * x)]
  22. empty = []
  23. multiline = [
  24. 'line one'
  25. 'line two'
  26. ]
  27. # Conditionals and ternaries.
  28. if submarine.shields_up
  29. full_speed_ahead()
  30. fire_torpedos()
  31. else if submarine.sinking
  32. abandon_ship()
  33. else
  34. run_away()
  35. eldest = if 25 > 21 then liz else marge
  36. decoration = medal_of_honor if war_hero
  37. go_to_sleep() unless coffee
  38. # Returning early:
  39. race = ->
  40. run()
  41. walk()
  42. crawl()
  43. return sleep() if tired
  44. race()
  45. # Conditional assignment:
  46. good or= evil
  47. wine and= cheese
  48. # Nested property access and calls.
  49. (moon.turn 360).shapes[3].move(x: 45, y: 30).position['top'].offset('x')
  50. a = b = c = 5
  51. # Embedded JavaScript.
  52. callback(
  53. `function(e) { e.stop(); }`
  54. )
  55. # Try/Catch/Finally/Throw.
  56. try
  57. all_hell_breaks_loose()
  58. dogs_and_cats_living_together()
  59. throw "up"
  60. catch error
  61. print error
  62. finally
  63. clean_up()
  64. try all_hell_breaks_loose() catch error then print(error) finally clean_up()
  65. # While loops, break and continue.
  66. while demand > supply
  67. sell()
  68. restock()
  69. while supply > demand then buy()
  70. loop
  71. break if broken
  72. continue if continuing
  73. # Unary operators.
  74. !!true
  75. # Lexical scoping.
  76. v_1 = 5
  77. change_a_and_set_b = ->
  78. v_1 = 10
  79. v_2 = 15
  80. v_2 = 20
  81. # Array comprehensions.
  82. supper = food.capitalize() for food in ['toast', 'cheese', 'wine']
  83. drink bottle for bottle, i in ['soda', 'wine', 'lemonade'] when even i
  84. # Switch statements ("else" serves as a default).
  85. activity = switch day
  86. when "Tuesday" then eat_breakfast()
  87. when "Sunday" then go_to_church()
  88. when "Saturday" then go_to_the_park()
  89. when "Wednesday"
  90. if day is bingo_day
  91. go_to_bingo()
  92. else
  93. eat_breakfast()
  94. go_to_work()
  95. eat_dinner()
  96. else go_to_work()
  97. # Semicolons can optionally be used instead of newlines.
  98. wednesday = -> eat_breakfast(); go_to_work(); eat_dinner()
  99. # Multiline strings with inner quotes.
  100. story = "Lorem ipsum dolor \"sit\" amet, consectetuer adipiscing elit,
  101. sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
  102. aliquam erat volutpat. Ut wisi enim ad."
  103. # Inheritance and calling super.
  104. class Animal
  105. (@name) ->
  106. move: (meters) ->
  107. alert this.name + " moved " + meters + "m."
  108. class Snake extends Animal
  109. move: ->
  110. alert 'Slithering...'
  111. super 5
  112. class Horse extends Animal
  113. move: ->
  114. alert 'Galloping...'
  115. super 45
  116. sam = new Snake "Sammy the Snake"
  117. tom = new Horse "Tommy the Horse"
  118. sam.move()
  119. tom.move()
  120. # Numbers.
  121. a_googol = 1e100
  122. hex = 0xff0000
  123. negative = -1.0
  124. infinity = Infinity
  125. nan = NaN
  126. # Deleting.
  127. delete secret.identity