1
0

index.html 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeMirror: Clojure mode</title>
  6. <link rel="stylesheet" href="../../lib/codemirror.css">
  7. <script src="../../lib/codemirror.js"></script>
  8. <script src="clojure.js"></script>
  9. <style>.CodeMirror {background: #f8f8f8;}</style>
  10. <link rel="stylesheet" href="../../doc/docs.css">
  11. </head>
  12. <body>
  13. <h1>CodeMirror: Clojure mode</h1>
  14. <form><textarea id="code" name="code">
  15. ; Conway's Game of Life, based on the work of:
  16. ;; Laurent Petit https://gist.github.com/1200343
  17. ;; Christophe Grand http://clj-me.cgrand.net/2011/08/19/conways-game-of-life
  18. (ns ^{:doc "Conway's Game of Life."}
  19. game-of-life)
  20. ;; Core game of life's algorithm functions
  21. (defn neighbours
  22. "Given a cell's coordinates, returns the coordinates of its neighbours."
  23. [[x y]]
  24. (for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
  25. [(+ dx x) (+ dy y)]))
  26. (defn step
  27. "Given a set of living cells, computes the new set of living cells."
  28. [cells]
  29. (set (for [[cell n] (frequencies (mapcat neighbours cells))
  30. :when (or (= n 3) (and (= n 2) (cells cell)))]
  31. cell)))
  32. ;; Utility methods for displaying game on a text terminal
  33. (defn print-board
  34. "Prints a board on *out*, representing a step in the game."
  35. [board w h]
  36. (doseq [x (range (inc w)) y (range (inc h))]
  37. (if (= y 0) (print "\n"))
  38. (print (if (board [x y]) "[X]" " . "))))
  39. (defn display-grids
  40. "Prints a squence of boards on *out*, representing several steps."
  41. [grids w h]
  42. (doseq [board grids]
  43. (print-board board w h)
  44. (print "\n")))
  45. ;; Launches an example board
  46. (def
  47. ^{:doc "board represents the initial set of living cells"}
  48. board #{[2 1] [2 2] [2 3]})
  49. (display-grids (take 3 (iterate step board)) 5 5)
  50. ;; Let's play with characters
  51. (println \1 \a \# \\
  52. \" \( \newline
  53. \} \" \space
  54. \tab \return \backspace
  55. \u1000 \uAaAa \u9F9F)
  56. </textarea></form>
  57. <script>
  58. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
  59. </script>
  60. <p><strong>MIME types defined:</strong> <code>text/x-clojure</code>.</p>
  61. </body>
  62. </html>