index.html 2.5 KB

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