haml-more 0.4.0.a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (225) hide show
  1. data/README.md +79 -0
  2. data/lib/haml/more/coffee_script.rb +137 -0
  3. data/lib/haml/more/content_for.rb +25 -0
  4. data/lib/haml/more.rb +45 -0
  5. data/lib/haml-more.rb +1 -0
  6. data/lib/sass/more.rb +16 -0
  7. data/lib/sass-more.rb +1 -0
  8. data/spec/sass/more_spec.rb +21 -0
  9. data/vendor/coffee-script/Cakefile +57 -0
  10. data/vendor/coffee-script/LICENSE +22 -0
  11. data/vendor/coffee-script/README +41 -0
  12. data/vendor/coffee-script/Rakefile +20 -0
  13. data/vendor/coffee-script/bin/cake +7 -0
  14. data/vendor/coffee-script/bin/coffee +7 -0
  15. data/vendor/coffee-script/documentation/coffee/aliases.coffee +9 -0
  16. data/vendor/coffee-script/documentation/coffee/arguments.coffee +4 -0
  17. data/vendor/coffee-script/documentation/coffee/array_comprehensions.coffee +7 -0
  18. data/vendor/coffee-script/documentation/coffee/assignment.coffee +2 -0
  19. data/vendor/coffee-script/documentation/coffee/cake_tasks.coffee +5 -0
  20. data/vendor/coffee-script/documentation/coffee/comparisons.coffee +5 -0
  21. data/vendor/coffee-script/documentation/coffee/conditionals.coffee +9 -0
  22. data/vendor/coffee-script/documentation/coffee/embedded.coffee +5 -0
  23. data/vendor/coffee-script/documentation/coffee/existence.coffee +8 -0
  24. data/vendor/coffee-script/documentation/coffee/expressions.coffee +9 -0
  25. data/vendor/coffee-script/documentation/coffee/expressions_assignment.coffee +1 -0
  26. data/vendor/coffee-script/documentation/coffee/expressions_comprehension.coffee +3 -0
  27. data/vendor/coffee-script/documentation/coffee/expressions_try.coffee +6 -0
  28. data/vendor/coffee-script/documentation/coffee/fat_arrow.coffee +6 -0
  29. data/vendor/coffee-script/documentation/coffee/functions.coffee +2 -0
  30. data/vendor/coffee-script/documentation/coffee/heredocs.coffee +5 -0
  31. data/vendor/coffee-script/documentation/coffee/multiple_return_values.coffee +5 -0
  32. data/vendor/coffee-script/documentation/coffee/object_comprehensions.coffee +4 -0
  33. data/vendor/coffee-script/documentation/coffee/object_extraction.coffee +13 -0
  34. data/vendor/coffee-script/documentation/coffee/objects_and_arrays.coffee +13 -0
  35. data/vendor/coffee-script/documentation/coffee/overview.coffee +29 -0
  36. data/vendor/coffee-script/documentation/coffee/parallel_assignment.coffee +4 -0
  37. data/vendor/coffee-script/documentation/coffee/range_comprehensions.coffee +6 -0
  38. data/vendor/coffee-script/documentation/coffee/scope.coffee +5 -0
  39. data/vendor/coffee-script/documentation/coffee/slices.coffee +6 -0
  40. data/vendor/coffee-script/documentation/coffee/soaks.coffee +1 -0
  41. data/vendor/coffee-script/documentation/coffee/splats.coffee +25 -0
  42. data/vendor/coffee-script/documentation/coffee/splices.coffee +5 -0
  43. data/vendor/coffee-script/documentation/coffee/strings.coffee +8 -0
  44. data/vendor/coffee-script/documentation/coffee/super.coffee +34 -0
  45. data/vendor/coffee-script/documentation/coffee/switch.coffee +10 -0
  46. data/vendor/coffee-script/documentation/coffee/try.coffee +7 -0
  47. data/vendor/coffee-script/documentation/coffee/while.coffee +10 -0
  48. data/vendor/coffee-script/documentation/css/docs.css +213 -0
  49. data/vendor/coffee-script/documentation/css/idle.css +63 -0
  50. data/vendor/coffee-script/documentation/css/logo.png +0 -0
  51. data/vendor/coffee-script/documentation/index.html.erb +967 -0
  52. data/vendor/coffee-script/documentation/js/aliases.js +14 -0
  53. data/vendor/coffee-script/documentation/js/arguments.js +8 -0
  54. data/vendor/coffee-script/documentation/js/array_comprehensions.js +26 -0
  55. data/vendor/coffee-script/documentation/js/assignment.js +5 -0
  56. data/vendor/coffee-script/documentation/js/cake_tasks.js +14 -0
  57. data/vendor/coffee-script/documentation/js/comparisons.js +5 -0
  58. data/vendor/coffee-script/documentation/js/conditionals.js +12 -0
  59. data/vendor/coffee-script/documentation/js/embedded.js +6 -0
  60. data/vendor/coffee-script/documentation/js/existence.js +7 -0
  61. data/vendor/coffee-script/documentation/js/expressions.js +13 -0
  62. data/vendor/coffee-script/documentation/js/expressions_assignment.js +4 -0
  63. data/vendor/coffee-script/documentation/js/expressions_comprehension.js +12 -0
  64. data/vendor/coffee-script/documentation/js/expressions_try.js +9 -0
  65. data/vendor/coffee-script/documentation/js/fat_arrow.js +15 -0
  66. data/vendor/coffee-script/documentation/js/functions.js +9 -0
  67. data/vendor/coffee-script/documentation/js/heredocs.js +4 -0
  68. data/vendor/coffee-script/documentation/js/intro.js +7 -0
  69. data/vendor/coffee-script/documentation/js/multiple_return_values.js +11 -0
  70. data/vendor/coffee-script/documentation/js/object_comprehensions.js +17 -0
  71. data/vendor/coffee-script/documentation/js/object_extraction.js +17 -0
  72. data/vendor/coffee-script/documentation/js/objects_and_arrays.js +10 -0
  73. data/vendor/coffee-script/documentation/js/overview.js +43 -0
  74. data/vendor/coffee-script/documentation/js/parallel_assignment.js +8 -0
  75. data/vendor/coffee-script/documentation/js/punctuation.js +8 -0
  76. data/vendor/coffee-script/documentation/js/range_comprehensions.js +21 -0
  77. data/vendor/coffee-script/documentation/js/scope.js +10 -0
  78. data/vendor/coffee-script/documentation/js/slices.js +6 -0
  79. data/vendor/coffee-script/documentation/js/soaks.js +4 -0
  80. data/vendor/coffee-script/documentation/js/splats.js +16 -0
  81. data/vendor/coffee-script/documentation/js/splices.js +5 -0
  82. data/vendor/coffee-script/documentation/js/strings.js +9 -0
  83. data/vendor/coffee-script/documentation/js/super.js +37 -0
  84. data/vendor/coffee-script/documentation/js/switch.js +18 -0
  85. data/vendor/coffee-script/documentation/js/try.js +10 -0
  86. data/vendor/coffee-script/documentation/js/while.js +22 -0
  87. data/vendor/coffee-script/documentation/underscore.html +627 -0
  88. data/vendor/coffee-script/examples/beautiful_code/binary_search.coffee +16 -0
  89. data/vendor/coffee-script/examples/beautiful_code/quicksort_runtime.coffee +13 -0
  90. data/vendor/coffee-script/examples/beautiful_code/regular_expression_matcher.coffee +34 -0
  91. data/vendor/coffee-script/examples/blocks.coffee +57 -0
  92. data/vendor/coffee-script/examples/code.coffee +173 -0
  93. data/vendor/coffee-script/examples/computer_science/README +4 -0
  94. data/vendor/coffee-script/examples/computer_science/binary_search.coffee +25 -0
  95. data/vendor/coffee-script/examples/computer_science/bubble_sort.coffee +11 -0
  96. data/vendor/coffee-script/examples/computer_science/linked_list.coffee +106 -0
  97. data/vendor/coffee-script/examples/computer_science/luhn_algorithm.coffee +36 -0
  98. data/vendor/coffee-script/examples/computer_science/merge_sort.coffee +19 -0
  99. data/vendor/coffee-script/examples/computer_science/selection_sort.coffee +23 -0
  100. data/vendor/coffee-script/examples/poignant.coffee +186 -0
  101. data/vendor/coffee-script/examples/potion.coffee +205 -0
  102. data/vendor/coffee-script/examples/underscore.coffee +603 -0
  103. data/vendor/coffee-script/examples/web_server.coffee +12 -0
  104. data/vendor/coffee-script/extras/CoffeeScript.tmbundle/Preferences/CoffeeScript.tmPreferences +24 -0
  105. data/vendor/coffee-script/extras/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage +361 -0
  106. data/vendor/coffee-script/extras/CoffeeScript.tmbundle/info.plist +10 -0
  107. data/vendor/coffee-script/extras/EXTRAS +20 -0
  108. data/vendor/coffee-script/extras/coffee.vim +117 -0
  109. data/vendor/coffee-script/index.html +1847 -0
  110. data/vendor/coffee-script/lib/bin/cake +7 -0
  111. data/vendor/coffee-script/lib/bin/coffee +7 -0
  112. data/vendor/coffee-script/lib/cake.js +80 -0
  113. data/vendor/coffee-script/lib/coffee-script.js +61 -0
  114. data/vendor/coffee-script/lib/command_line.js +201 -0
  115. data/vendor/coffee-script/lib/grammar.js +564 -0
  116. data/vendor/coffee-script/lib/lexer.js +405 -0
  117. data/vendor/coffee-script/lib/narwhal.js +44 -0
  118. data/vendor/coffee-script/lib/nodes.js +1328 -0
  119. data/vendor/coffee-script/lib/optparse.js +117 -0
  120. data/vendor/coffee-script/lib/parser.js +536 -0
  121. data/vendor/coffee-script/lib/repl.js +32 -0
  122. data/vendor/coffee-script/lib/rewriter.js +383 -0
  123. data/vendor/coffee-script/lib/scope.js +114 -0
  124. data/vendor/coffee-script/package.json +7 -0
  125. data/vendor/coffee-script/src/cake.coffee +45 -0
  126. data/vendor/coffee-script/src/coffee-script.coffee +45 -0
  127. data/vendor/coffee-script/src/command_line.coffee +130 -0
  128. data/vendor/coffee-script/src/grammar.coffee +456 -0
  129. data/vendor/coffee-script/src/lexer.coffee +327 -0
  130. data/vendor/coffee-script/src/narwhal.coffee +42 -0
  131. data/vendor/coffee-script/src/nodes.coffee +1045 -0
  132. data/vendor/coffee-script/src/optparse.coffee +79 -0
  133. data/vendor/coffee-script/src/repl.coffee +23 -0
  134. data/vendor/coffee-script/src/rewriter.coffee +253 -0
  135. data/vendor/coffee-script/src/scope.coffee +75 -0
  136. data/vendor/coffee-script/test/test_arguments.coffee +34 -0
  137. data/vendor/coffee-script/test/test_array_comprehension.coffee +42 -0
  138. data/vendor/coffee-script/test/test_assignment.coffee +26 -0
  139. data/vendor/coffee-script/test/test_blocks.coffee +4 -0
  140. data/vendor/coffee-script/test/test_calling_super.coffee +42 -0
  141. data/vendor/coffee-script/test/test_chained_calls.coffee +25 -0
  142. data/vendor/coffee-script/test/test_destructuring_assignment.coffee +62 -0
  143. data/vendor/coffee-script/test/test_everything.coffee +29 -0
  144. data/vendor/coffee-script/test/test_exceptions.coffee +2 -0
  145. data/vendor/coffee-script/test/test_existence.coffee +81 -0
  146. data/vendor/coffee-script/test/test_expressions.coffee +30 -0
  147. data/vendor/coffee-script/test/test_fancy_if_statement.coffee +26 -0
  148. data/vendor/coffee-script/test/test_functions.coffee +80 -0
  149. data/vendor/coffee-script/test/test_funky_comments.coffee +25 -0
  150. data/vendor/coffee-script/test/test_heredocs.coffee +46 -0
  151. data/vendor/coffee-script/test/test_lexical_scope.coffee +10 -0
  152. data/vendor/coffee-script/test/test_literals.coffee +56 -0
  153. data/vendor/coffee-script/test/test_nested_comprehensions.coffee +11 -0
  154. data/vendor/coffee-script/test/test_newline_escaping.coffee +6 -0
  155. data/vendor/coffee-script/test/test_operations.coffee +18 -0
  156. data/vendor/coffee-script/test/test_range_comprehension.coffee +20 -0
  157. data/vendor/coffee-script/test/test_ranges_and_slices.coffee +16 -0
  158. data/vendor/coffee-script/test/test_splats.coffee +47 -0
  159. data/vendor/coffee-script/test/test_splices.coffee +5 -0
  160. data/vendor/coffee-script/test/test_switch.coffee +64 -0
  161. data/vendor/coffee-script/test/test_while.coffee +30 -0
  162. data/vendor/coffee-script/vendor/jison/Jakefile +31 -0
  163. data/vendor/coffee-script/vendor/jison/README.md +347 -0
  164. data/vendor/coffee-script/vendor/jison/bin/jison +3 -0
  165. data/vendor/coffee-script/vendor/jison/bin/json2jison +3 -0
  166. data/vendor/coffee-script/vendor/jison/examples/ansic.jison +415 -0
  167. data/vendor/coffee-script/vendor/jison/examples/basic.json +8 -0
  168. data/vendor/coffee-script/vendor/jison/examples/basic2.json +9 -0
  169. data/vendor/coffee-script/vendor/jison/examples/basic2_lex.json +16 -0
  170. data/vendor/coffee-script/vendor/jison/examples/basic_lex.json +15 -0
  171. data/vendor/coffee-script/vendor/jison/examples/calculator.jison +38 -0
  172. data/vendor/coffee-script/vendor/jison/examples/calculator.jisonlex +14 -0
  173. data/vendor/coffee-script/vendor/jison/examples/calculator.json +42 -0
  174. data/vendor/coffee-script/vendor/jison/examples/classy.json +105 -0
  175. data/vendor/coffee-script/vendor/jison/examples/classy_ast.json +126 -0
  176. data/vendor/coffee-script/vendor/jison/examples/dism.json +25 -0
  177. data/vendor/coffee-script/vendor/jison/examples/dism_lr0.json +26 -0
  178. data/vendor/coffee-script/vendor/jison/examples/json.js +80 -0
  179. data/vendor/coffee-script/vendor/jison/examples/json_ast.js +83 -0
  180. data/vendor/coffee-script/vendor/jison/examples/precedence.json +26 -0
  181. data/vendor/coffee-script/vendor/jison/examples/reduce_conflict.json +13 -0
  182. data/vendor/coffee-script/vendor/jison/lib/jison/bnf.js +43 -0
  183. data/vendor/coffee-script/vendor/jison/lib/jison/jisonlex.js +18 -0
  184. data/vendor/coffee-script/vendor/jison/lib/jison/json2jison.js +146 -0
  185. data/vendor/coffee-script/vendor/jison/lib/jison/lexer.js +224 -0
  186. data/vendor/coffee-script/vendor/jison/lib/jison/util/bnf-parser.js +383 -0
  187. data/vendor/coffee-script/vendor/jison/lib/jison/util/lex-parser.js +407 -0
  188. data/vendor/coffee-script/vendor/jison/lib/jison/util/set.js +94 -0
  189. data/vendor/coffee-script/vendor/jison/lib/jison/util/typal.js +90 -0
  190. data/vendor/coffee-script/vendor/jison/lib/jison.js +1414 -0
  191. data/vendor/coffee-script/vendor/jison/package.json +14 -0
  192. data/vendor/coffee-script/vendor/jison/src/bnf.jison +110 -0
  193. data/vendor/coffee-script/vendor/jison/src/bnf.jisonlex +25 -0
  194. data/vendor/coffee-script/vendor/jison/src/bnf.lex.json +24 -0
  195. data/vendor/coffee-script/vendor/jison/src/jisonlex.jison +129 -0
  196. data/vendor/coffee-script/vendor/jison/src/jisonlex.jisonlex +31 -0
  197. data/vendor/coffee-script/vendor/jison/src/jisonlex.lex.json +30 -0
  198. data/vendor/coffee-script/vendor/jison/tests/all-tests.js +8 -0
  199. data/vendor/coffee-script/vendor/jison/tests/grammar/bnf.js +91 -0
  200. data/vendor/coffee-script/vendor/jison/tests/grammar/bnf_parse.js +65 -0
  201. data/vendor/coffee-script/vendor/jison/tests/grammar/grammar-tests.js +10 -0
  202. data/vendor/coffee-script/vendor/jison/tests/grammar/json2jison.js +24 -0
  203. data/vendor/coffee-script/vendor/jison/tests/grammar/lex/ansic.jisonlex +115 -0
  204. data/vendor/coffee-script/vendor/jison/tests/grammar/lex/bnf.jisonlex +25 -0
  205. data/vendor/coffee-script/vendor/jison/tests/grammar/lex/bnf.lex.json +24 -0
  206. data/vendor/coffee-script/vendor/jison/tests/grammar/lex/lex_grammar.jisonlex +31 -0
  207. data/vendor/coffee-script/vendor/jison/tests/grammar/lex/lex_grammar.lex.json +30 -0
  208. data/vendor/coffee-script/vendor/jison/tests/grammar/lex.jison +119 -0
  209. data/vendor/coffee-script/vendor/jison/tests/grammar/lex.js +58 -0
  210. data/vendor/coffee-script/vendor/jison/tests/grammar/lex_parse.js +117 -0
  211. data/vendor/coffee-script/vendor/jison/tests/lexer/lexer-tests.js +6 -0
  212. data/vendor/coffee-script/vendor/jison/tests/lexer/regexplexer.js +417 -0
  213. data/vendor/coffee-script/vendor/jison/tests/parser/actions.js +311 -0
  214. data/vendor/coffee-script/vendor/jison/tests/parser/api.js +236 -0
  215. data/vendor/coffee-script/vendor/jison/tests/parser/generator.js +196 -0
  216. data/vendor/coffee-script/vendor/jison/tests/parser/lalr.js +183 -0
  217. data/vendor/coffee-script/vendor/jison/tests/parser/lr0.js +72 -0
  218. data/vendor/coffee-script/vendor/jison/tests/parser/lr1.js +119 -0
  219. data/vendor/coffee-script/vendor/jison/tests/parser/parser-tests.js +14 -0
  220. data/vendor/coffee-script/vendor/jison/tests/parser/precedence.js +237 -0
  221. data/vendor/coffee-script/vendor/jison/tests/parser/slr.js +52 -0
  222. data/vendor/coffee-script/vendor/jison/tests/parser/tables.js +126 -0
  223. data/vendor/coffee-script/vendor/jison/tests/performance.js +110 -0
  224. data/vendor/coffee-script/vendor/jison/tests/setup.js +3 -0
  225. metadata +324 -0
@@ -0,0 +1,16 @@
1
+ # Beautiful Code, Chapter 6.
2
+ # The implementation of binary search that is tested.
3
+
4
+ # Return the index of an element in a sorted list. (or -1, if not present)
5
+ index: (list, target) ->
6
+ [low, high]: [0, list.length]
7
+ while low < high
8
+ mid: (low + high) >> 1
9
+ val: list[mid]
10
+ return mid if val is target
11
+ if val < target then low: mid + 1 else high: mid
12
+ return -1
13
+
14
+ puts 2 is index([10, 20, 30, 40, 50], 30)
15
+ puts 4 is index([-97, 35, 67, 88, 1200], 1200)
16
+ puts 0 is index([0, 45, 70], 0)
@@ -0,0 +1,13 @@
1
+ # Beautiful Code, Chapter 3.
2
+ # Produces the expected runtime of Quicksort, for every integer from 1 to N.
3
+
4
+ runtime: (N) ->
5
+ [sum, t]: [0, 0]
6
+ for n in [1..N]
7
+ sum += 2 * t
8
+ t: n - 1 + sum / n
9
+ t
10
+
11
+ puts runtime(3) is 2.6666666666666665
12
+ puts runtime(5) is 7.4
13
+ puts runtime(8) is 16.92142857142857
@@ -0,0 +1,34 @@
1
+ # Beautiful Code, Chapter 1.
2
+ # Implements a regular expression matcher that supports character matches,
3
+ # '.', '^', '$', and '*'.
4
+
5
+ # Search for the regexp anywhere in the text.
6
+ match: (regexp, text) ->
7
+ return match_here(regexp.slice(1), text) if regexp[0] is '^'
8
+ while text
9
+ return true if match_here(regexp, text)
10
+ text: text.slice(1)
11
+ false
12
+
13
+ # Search for the regexp at the beginning of the text.
14
+ match_here: (regexp, text) ->
15
+ [cur, next]: [regexp[0], regexp[1]]
16
+ if regexp.length is 0 then return true
17
+ if next is '*' then return match_star(cur, regexp.slice(2), text)
18
+ if cur is '$' and not next then return text.length is 0
19
+ if text and (cur is '.' or cur is text[0]) then return match_here(regexp.slice(1), text.slice(1))
20
+ false
21
+
22
+ # Search for a kleene star match at the beginning of the text.
23
+ match_star: (c, regexp, text) ->
24
+ while true
25
+ return true if match_here(regexp, text)
26
+ return false unless text and (text[0] is c or c is '.')
27
+ text: text.slice(1)
28
+
29
+ puts match("ex", "some text")
30
+ puts match("s..t", "spit")
31
+ puts match("^..t", "buttercup")
32
+ puts match("i..$", "cherries")
33
+ puts match("o*m", "vrooooommm!")
34
+ puts match("^hel*o$", "hellllllo")
@@ -0,0 +1,57 @@
1
+ # After wycats' http://yehudakatz.com/2010/02/07/the-building-blocks-of-ruby/
2
+
3
+ # Sinatra.
4
+ get '/hello', ->
5
+ 'Hello World'
6
+
7
+
8
+ # Append.
9
+ append: (location, data) ->
10
+ path: new Pathname location
11
+ throw new Error("Location does not exist") unless path.exists()
12
+
13
+ File.open path, 'a', (file) ->
14
+ file.puts YAML.dump data
15
+
16
+ data
17
+
18
+
19
+ # Rubinius' File.open implementation.
20
+ File.open: (path, mode, block) ->
21
+ io: new File path, mode
22
+
23
+ return io unless block
24
+
25
+ try
26
+ block io
27
+ finally
28
+ try
29
+ io.close() unless io.closed()
30
+ catch error
31
+ # nothing, just swallow them.
32
+
33
+
34
+ # Write.
35
+ write: (location, data) ->
36
+ path = new Pathname location
37
+ raise "Location does not exist" unless path.exists()
38
+
39
+ File.open path, 'w', (file) ->
40
+ return false if Digest.MD5.hexdigest(file.read()) is data.hash()
41
+ file.puts YAML.dump data
42
+ true
43
+
44
+
45
+ # Rails' respond_to.
46
+ index: ->
47
+ people: Person.find 'all'
48
+
49
+ respond_to (format) ->
50
+ format.html()
51
+ format.xml -> render { xml: people.xml() }
52
+
53
+
54
+ # Synchronization.
55
+ synchronize: (block) ->
56
+ lock()
57
+ try block() finally unlock()
@@ -0,0 +1,173 @@
1
+ # Functions:
2
+ square: (x) -> x * x
3
+
4
+ sum: (x, y) -> x + y
5
+
6
+ odd: (x) -> x % 2 isnt 0
7
+
8
+ even: (x) -> x % 2 is 0
9
+
10
+ run_loop: ->
11
+ fire_events((e) -> e.stopPropagation())
12
+ listen()
13
+ wait()
14
+
15
+ # Objects:
16
+ dense_object_literal: {one: 1, two: 2, three: 3}
17
+
18
+ spaced_out_multiline_object: {
19
+ pi: 3.14159
20
+ list: [1, 2, 3, 4]
21
+ regex: /match[ing](every|thing|\/)/gi
22
+ three: new Idea()
23
+
24
+ inner_obj: {
25
+ freedom: -> _.freedom()
26
+ }
27
+ }
28
+
29
+ # Arrays:
30
+ stooges: [{moe: 45}, {curly: 43}, {larry: 46}]
31
+
32
+ exponents: [(x) -> x, (x) -> x * x, (x) -> x * x * x]
33
+
34
+ empty: []
35
+
36
+ multiline: [
37
+ 'line one'
38
+ 'line two'
39
+ ]
40
+
41
+ # Conditionals and ternaries.
42
+ if submarine.shields_up
43
+ full_speed_ahead()
44
+ fire_torpedos()
45
+ else if submarine.sinking
46
+ abandon_ship()
47
+ else
48
+ run_away()
49
+
50
+ eldest: if 25 > 21 then liz else marge
51
+
52
+ decoration: medal_of_honor if war_hero
53
+
54
+ go_to_sleep() unless coffee
55
+
56
+ # Returning early:
57
+ race: ->
58
+ run()
59
+ walk()
60
+ crawl()
61
+ if tired then return sleep()
62
+ race()
63
+
64
+ # Conditional assignment:
65
+ good ||= evil
66
+ wine &&= cheese
67
+
68
+ # Nested property access and calls.
69
+ ((moon.turn(360))).shapes[3].move({x: 45, y: 30}).position['top'].offset('x')
70
+
71
+ a: b: c: 5
72
+
73
+ # Embedded JavaScript.
74
+ callback(
75
+ `function(e) { e.stop(); }`
76
+ )
77
+
78
+ # Try/Catch/Finally/Throw.
79
+ try
80
+ all_hell_breaks_loose()
81
+ dogs_and_cats_living_together()
82
+ throw "up"
83
+ catch error
84
+ print(error)
85
+ finally
86
+ clean_up()
87
+
88
+ try all_hell_breaks_loose() catch error then print(error) finally clean_up()
89
+
90
+ # While loops, break and continue.
91
+ while demand > supply
92
+ sell()
93
+ restock()
94
+
95
+ while supply > demand then buy()
96
+
97
+ while true
98
+ break if broken
99
+ continue if continuing
100
+
101
+ # Unary operators.
102
+ !!true
103
+
104
+ # Lexical scoping.
105
+ v_1: 5
106
+ change_a_and_set_b: ->
107
+ v_1: 10
108
+ v_2: 15
109
+ v_2: 20
110
+
111
+ # Array comprehensions.
112
+ supper: food.capitalize() for food in ['toast', 'cheese', 'wine']
113
+
114
+ drink(bottle) for bottle, i in ['soda', 'wine', 'lemonade'] when even(i)
115
+
116
+ # Switch statements ("else" serves as a default).
117
+ activity: switch day
118
+ when "Tuesday" then eat_breakfast()
119
+ when "Sunday" then go_to_church()
120
+ when "Saturday" then go_to_the_park()
121
+ when "Wednesday"
122
+ if day is bingo_day
123
+ go_to_bingo()
124
+ else
125
+ eat_breakfast()
126
+ go_to_work()
127
+ eat_dinner()
128
+ else go_to_work()
129
+
130
+ # Semicolons can optionally be used instead of newlines.
131
+ wednesday: -> eat_breakfast(); go_to_work(); eat_dinner()
132
+
133
+ # Array slice literals.
134
+ zero_to_nine: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
135
+ three_to_six: zero_to_nine[3..6]
136
+
137
+ # Multiline strings with inner quotes.
138
+ story: "Lorem ipsum dolor \"sit\" amet, consectetuer adipiscing elit,
139
+ sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
140
+ aliquam erat volutpat. Ut wisi enim ad."
141
+
142
+ # Inheritance and calling super.
143
+ Animal: ->
144
+ Animal::move: (meters) ->
145
+ alert(this.name + " moved " + meters + "m.")
146
+
147
+ Snake: (name) -> this.name: name
148
+ Snake extends Animal
149
+ Snake::move: ->
150
+ alert('Slithering...')
151
+ super(5)
152
+
153
+ Horse: (name) -> this.name: name
154
+ Horse extends Animal
155
+ Horse::move: ->
156
+ alert('Galloping...')
157
+ super(45)
158
+
159
+ sam: new Snake("Sammy the Snake")
160
+ tom: new Horse("Tommy the Horse")
161
+
162
+ sam.move()
163
+ tom.move()
164
+
165
+ # Numbers.
166
+ a_googol: 1e100
167
+ hex: 0xff0000
168
+ negative: -1.0
169
+ infinity: Infinity
170
+ nan: NaN
171
+
172
+ # Deleting.
173
+ delete secret.identity
@@ -0,0 +1,4 @@
1
+ Ported from Nicholas Zakas' collection of computer science fundamentals, written
2
+ in JavaScript. Originals available here:
3
+
4
+ http://github.com/nzakas/computer-science-in-javascript
@@ -0,0 +1,25 @@
1
+ # Uses a binary search algorithm to locate a value in the specified array.
2
+ binary_search: (items, value) ->
3
+
4
+ start: 0
5
+ stop: items.length - 1
6
+ pivot: Math.floor((start + stop) / 2)
7
+
8
+ while items[pivot] isnt value and start < stop
9
+
10
+ # Adjust the search area.
11
+ stop: pivot - 1 if value < items[pivot]
12
+ start: pivot + 1 if value > items[pivot]
13
+
14
+ # Recalculate the pivot.
15
+ pivot: Math.floor((stop + start) / 2)
16
+
17
+ # Make sure we've found the correct value.
18
+ if items[pivot] is value then pivot else -1
19
+
20
+
21
+ # Test the function.
22
+ puts(2 is binary_search([10, 20, 30, 40, 50], 30))
23
+ puts(4 is binary_search([-97, 35, 67, 88, 1200], 1200))
24
+ puts(0 is binary_search([0, 45, 70], 0))
25
+ puts(-1 is binary_search([0, 45, 70], 10))
@@ -0,0 +1,11 @@
1
+ # A bubble sort implementation, sorting the given array in-place.
2
+ bubble_sort: (list) ->
3
+ for i in [0...list.length]
4
+ for j in [0...list.length - i]
5
+ [list[j], list[j+1]]: [list[j+1], list[j]] if list[j] > list[j+1]
6
+ list
7
+
8
+
9
+ # Test the function.
10
+ puts(bubble_sort([3, 2, 1]).join(' ') is '1 2 3')
11
+ puts(bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9')
@@ -0,0 +1,106 @@
1
+ # "Classic" linked list implementation that doesn't keep track of its size.
2
+ LinkedList: ->
3
+ this._head: null # Pointer to the first item in the list.
4
+
5
+
6
+ # Appends some data to the end of the list. This method traverses the existing
7
+ # list and places the value at the end in a new node.
8
+ LinkedList::add: (data) ->
9
+
10
+ # Create a new node object to wrap the data.
11
+ node: {data: data, next: null}
12
+
13
+ current: this._head ||= node
14
+
15
+ if this._head isnt node
16
+ current: current.next while current.next
17
+ current.next: node
18
+
19
+ this
20
+
21
+
22
+ # Retrieves the data at the given position in the list.
23
+ LinkedList::item: (index) ->
24
+
25
+ # Check for out-of-bounds values.
26
+ return null if index < 0
27
+
28
+ current: this._head or null
29
+ i: -1
30
+
31
+ # Advance through the list.
32
+ current: current.next while current and index > (i += 1)
33
+
34
+ # Return null if we've reached the end.
35
+ current and current.data
36
+
37
+
38
+ # Remove the item from the given location in the list.
39
+ LinkedList::remove: (index) ->
40
+
41
+ # Check for out-of-bounds values.
42
+ return null if index < 0
43
+
44
+ current: this._head or null
45
+ i: -1
46
+
47
+ # Special case: removing the first item.
48
+ if index is 0
49
+ this._head: current.next
50
+ else
51
+
52
+ # Find the right location.
53
+ [previous, current]: [current, current.next] while index > (i += 1)
54
+
55
+ # Skip over the item to remove.
56
+ previous.next: current.next
57
+
58
+ # Return the value.
59
+ current and current.data
60
+
61
+
62
+ # Calculate the number of items in the list.
63
+ LinkedList::size: ->
64
+ current: this._head
65
+ count: 0
66
+
67
+ while current
68
+ count += 1
69
+ current: current.next
70
+
71
+ count
72
+
73
+
74
+ # Convert the list into an array.
75
+ LinkedList::toArray: ->
76
+ result: []
77
+ current: this._head
78
+
79
+ while current
80
+ result.push(current.data)
81
+ current: current.next
82
+
83
+ result
84
+
85
+
86
+ # The string representation of the linked list.
87
+ LinkedList::toString: -> this.toArray().toString()
88
+
89
+
90
+ # Tests.
91
+ list: new LinkedList()
92
+
93
+ list.add("Hi")
94
+ puts(list.size() is 1)
95
+ puts(list.item(0) is "Hi")
96
+ puts(list.item(1) is null)
97
+
98
+ list: new LinkedList()
99
+ list.add("zero").add("one").add("two")
100
+ puts(list.size() is 3)
101
+ puts(list.item(2) is "two")
102
+ puts(list.remove(1) is "one")
103
+ puts(list.item(0) is "zero")
104
+ puts(list.item(1) is "two")
105
+ puts(list.size() is 2)
106
+ puts(list.item(-10) is null)
@@ -0,0 +1,36 @@
1
+ # Use the Luhn algorithm to validate a numeric identifier, such as credit card
2
+ # numbers, national insurance numbers, etc.
3
+ # See: http://en.wikipedia.org/wiki/Luhn_algorithm
4
+
5
+ is_valid_identifier: (identifier) ->
6
+
7
+ sum: 0
8
+ alt: false
9
+
10
+ for i in [(identifier.length - 1)..0]
11
+
12
+ # Get the next digit.
13
+ num: parseInt(identifier.charAt(i), 10)
14
+
15
+ # If it's not a valid number, abort.
16
+ return false if isNaN(num)
17
+
18
+ # If it's an alternate number...
19
+ if alt
20
+ num *= 2
21
+ num: (num % 10) + 1 if num > 9
22
+
23
+ # Flip the alternate bit.
24
+ alt: !alt
25
+
26
+ # Add to the rest of the sum.
27
+ sum += num
28
+
29
+ # Determine if it's valid.
30
+ sum % 10 is 0
31
+
32
+
33
+ # Tests.
34
+ puts(is_valid_identifier("49927398716") is true)
35
+ puts(is_valid_identifier("4408041234567893") is true)
36
+ puts(is_valid_identifier("4408041234567890") is false)
@@ -0,0 +1,19 @@
1
+ # Sorts an array in ascending natural order using merge sort.
2
+ merge_sort: (list) ->
3
+
4
+ return list if list.length is 1
5
+
6
+ result: []
7
+ pivot: Math.floor(list.length / 2)
8
+ left: merge_sort(list.slice(0, pivot))
9
+ right: merge_sort(list.slice(pivot))
10
+
11
+ while left.length and right.length
12
+ result.push(if left[0] < right[0] then left.shift() else right.shift())
13
+
14
+ result.concat(left).concat(right)
15
+
16
+
17
+ # Test the function.
18
+ puts(merge_sort([3, 2, 1]).join(' ') is '1 2 3')
19
+ puts(merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9')
@@ -0,0 +1,23 @@
1
+ # An in-place selection sort.
2
+ selection_sort: (list) ->
3
+ len: list.length
4
+
5
+ # For each item in the list.
6
+ for i in [0...len]
7
+
8
+ # Set the minimum to this position.
9
+ min: i
10
+
11
+ # Check the rest of the array to see if anything is smaller.
12
+ (min: j if list[j] < list[min]) for j in [(i+1)...len]
13
+
14
+ # Swap if a smaller item has been found.
15
+ [list[i], list[min]]: [list[min], list[i]] if i isnt min
16
+
17
+ # The list is now sorted.
18
+ list
19
+
20
+
21
+ # Test the function.
22
+ puts(selection_sort([3, 2, 1]).join(' ') is '1 2 3')
23
+ puts(selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9')