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