aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/mode/clojure/index.html
blob: 9762d5892f2cc5b0d3af8631aea9ea2b5271fc49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!doctype html>
<html>
  <head>
    <title>CodeMirror: Clojure mode</title>
    <link rel="stylesheet" href="../../lib/codemirror.css">
    <script src="../../lib/codemirror.js"></script>
    <script src="clojure.js"></script>
    <style>.CodeMirror {background: #f8f8f8;}</style>
    <link rel="stylesheet" href="../../doc/docs.css">
  </head>
  <body>
    <h1>CodeMirror: Clojure mode</h1>
    <form><textarea id="code" name="code">
; Conway's Game of Life, based on the work of:
;; Laurent Petit https://gist.github.com/1200343
;; Christophe Grand http://clj-me.cgrand.net/2011/08/19/conways-game-of-life

(ns ^{:doc "Conway's Game of Life."}
 game-of-life)

;; Core game of life's algorithm functions

(defn neighbours 
  "Given a cell's coordinates, returns the coordinates of its neighbours."
  [[x y]]
  (for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
    [(+ dx x) (+ dy y)]))

(defn step 
  "Given a set of living cells, computes the new set of living cells."
  [cells]
  (set (for [[cell n] (frequencies (mapcat neighbours cells))
             :when (or (= n 3) (and (= n 2) (cells cell)))]
         cell)))

;; Utility methods for displaying game on a text terminal

(defn print-board 
  "Prints a board on *out*, representing a step in the game."
  [board w h]
  (doseq [x (range (inc w)) y (range (inc h))]
    (if (= y 0) (print "\n")) 
    (print (if (board [x y]) "[X]" " . "))))

(defn display-grids 
  "Prints a squence of boards on *out*, representing several steps."
  [grids w h]
  (doseq [board grids]
    (print-board board w h)
    (print "\n")))

;; Launches an example board

(def 
  ^{:doc "board represents the initial set of living cells"}
   board #{[2 1] [2 2] [2 3]})

(display-grids (take 3 (iterate step board)) 5 5) </textarea></form>
    <script>
      var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
    </script>

    <p><strong>MIME types defined:</strong> <code>text/x-clojure</code>.</p>

  </body>
</html>