You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.7 KiB

5 months ago
  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="../../addon/edit/closebrackets.js"></script>
  8. <script src="../../addon/edit/matchbrackets.js"></script>
  9. <script src="clojure.js"></script>
  10. <style>.CodeMirror {background: #f8f8f8;}</style>
  11. <div id=nav>
  12. <a href="https://codemirror.net/5"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png" alt=""></a>
  13. <ul>
  14. <li><a href="../../index.html">Home</a>
  15. <li><a href="../../doc/manual.html">Manual</a>
  16. <li><a href="https://github.com/codemirror/codemirror5">Code</a>
  17. </ul>
  18. <ul>
  19. <li><a href="../index.html">Language modes</a>
  20. <li><a class=active href="#">Clojure</a>
  21. </ul>
  22. </div>
  23. <article>
  24. <h2>Clojure mode</h2>
  25. <form><textarea id="code" name="code">
  26. (ns game-of-life
  27. "Conway's Game of Life, based on the work of
  28. Christophe Grand (http://clj-me.cgrand.net/2011/08/19/conways-game-of-life)
  29. and Laurent Petit (https://gist.github.com/1200343).")
  30. ;;; Core game of life's algorithm functions
  31. (defn neighbors
  32. "Given a cell's coordinates `[x y]`, returns the coordinates of its
  33. neighbors."
  34. [[x y]]
  35. (for [dx [-1 0 1]
  36. dy (if (zero? dx)
  37. [-1 1]
  38. [-1 0 1])]
  39. [(+ dx x) (+ dy y)]))
  40. (defn step
  41. "Given a set of living `cells`, computes the new set of living cells."
  42. [cells]
  43. (set (for [[cell n] (frequencies (mapcat neighbors cells))
  44. :when (or (= n 3)
  45. (and (= n 2)
  46. (cells cell)))]
  47. cell)))
  48. ;;; Utility methods for displaying game on a text terminal
  49. (defn print-grid
  50. "Prints a `grid` of `w` columns and `h` rows, on *out*, representing a
  51. step in the game."
  52. [grid w h]
  53. (doseq [x (range (inc w))
  54. y (range (inc h))]
  55. (when (= y 0) (println))
  56. (print (if (grid [x y])
  57. "[X]"
  58. " . "))))
  59. (defn print-grids
  60. "Prints a sequence of `grids` of `w` columns and `h` rows on *out*,
  61. representing several steps."
  62. [grids w h]
  63. (doseq [grid grids]
  64. (print-grid grid w h)
  65. (println)))
  66. ;;; Launches an example grid
  67. (def grid
  68. "`grid` represents the initial set of living cells"
  69. #{[2 1] [2 2] [2 3]})
  70. (print-grids (take 3 (iterate step grid)) 5 5)</textarea></form>
  71. <script>
  72. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  73. autoCloseBrackets: true,
  74. lineNumbers: true,
  75. matchBrackets: true,
  76. mode: 'text/x-clojure'
  77. });
  78. </script>
  79. <p><strong>MIME types defined:</strong> <code>text/x-clojure</code>.</p>
  80. </article>