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">
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
19 (ns ^{:doc "Conway's Game of Life."}
22 ;; Core game of life's algorithm functions
25 "Given a cell's coordinates, returns the coordinates of its neighbours."
27 (for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
31 "Given a set of living cells, computes the new set of living cells."
33 (set (for [[cell n] (frequencies (mapcat neighbours cells))
34 :when (or (= n 3) (and (= n 2) (cells cell)))]
37 ;; Utility methods for displaying game on a text terminal
40 "Prints a board on *out*, representing a step in the game."
42 (doseq [x (range (inc w)) y (range (inc h))]
43 (if (= y 0) (print "\n"))
44 (print (if (board [x y]) "[X]" " . "))))
47 "Prints a squence of boards on *out*, representing several steps."
50 (print-board board w h)
53 ;; Launches an example board
56 ^{:doc "board represents the initial set of living cells"}
57 board #{[2 1] [2 2] [2 3]})
59 (display-grids (take 3 (iterate step board)) 5 5)
61 ;; Let's play with characters
65 \tab \return \backspace
70 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
73 <p><strong>MIME types defined:</strong> <code>text/x-clojure</code>.</p>